Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> [PHP + MySQL] Separating The Results By Pages, Simple code
Alexandre Cisnei...
post Feb 15 2006, 11:11 PM
Post #1


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 24
Joined: 22-January 06
Member No.: 10,833



Hi! I will post here a code for separating the results of MySQL in pages.
You ask: Why separete?
I answer: Imagin that you have 1523 results to display. I dont have to say anything. =P
Here is it.
-------------------------------------------------------------------
CODE
<?php
$conect = mysql_connect("host","user","password");
$select_db = mysql_select_db("database");
$query = "SELECT * FROM mytable";
$results = "15"; //Number of results displayed per page.

if (!$page) {
    $counter = "1";
} else {
    $pcounter = $page;
}

$start = $counter - 1;
$start = $counter * $results;

$limit = mysql_query("$query LIMIT $start,$results");
$all = mysql_query("$query");

$total_registers = mysql_num_rows($all);
$total_pages = $total_registers / $results;

//---------NOW LETS SHOW EVERYTHING!! =D

while ($data = mysql_fetch_array($limit)) {
    $name = $data["name"];
    echo "Name: $name<br>";
}


$prev = $counter -1;
next = $counter +1;
if ($counter>1) {
    echo " <a href='?page=$prev'><- Previous</a> ";
}
echo "|";
if ($counter<$total_pages) {
    echo " <a href='?pagina=$next'>Next -></a>";
}
?>


[[MODERATOR PLEASE DELETE THIS POST AND EDIT MY FIRST POST]]
CODE
<?php
$conect = mysql_connect("host","user","password");
$select_db = mysql_select_db("database");
$query = "SELECT * FROM mytable";
$results = "15"; //Number of results displayed per page.

if (!$page) {
    $counter = "1";
} else {
    $counter = $page; //CHANGE 'pcounter' to 'counter'
}
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Need Help With A PHP - MySQL Registration Script(13)
  2. [PHP + MySQL] Encrypting Data(11)
  3. How Do You Create A Secure Loging?(4)
  4. How Can I Write PHP Code By This Formmail Html(5)
  5. Important: Basics Of Using PHP And MySQL(10)
  6. Need Help With Php/mysql And Web Servers Such As Asta's.(4)
  7. User Authentication Session Handling Problems(14)
  8. Code Snippets Repository(0)
  9. Need MySQL Alternative To The Syntax "or die()"(8)
  10. Re-order MySQL Table(11)
  11. PHP & MySQL: Displaying Content From A Given ID(6)
  12. How To Show Serial Nums In PHP Table For Contents Of MySQL DB(4)
  13. Php Mysql Errors(2)
  14. Sql Injection Prevention (passing Numerical Data Across Pages).(9)
  15. Php/mysql And Manual Page Caching?(4)
  1. Too Many Connections?(4)
  2. Extracting Mysql Maths Using Php(2)
  3. Anyone Know Of A Really Good Mysql Class?(4)
  4. What's Wrong With My Php Webpage?(2)
  5. Warning: Mysql_num_rows()(1)
  6. Warning: Mysql_result(): Supplied Argument Is Not A Valid Mysql Result Resource In ...(4)
  7. Making A Link = Mysql_query(8)
  8. Making Something In Mysql Happen Only Once(10)
  9. Mysql Question(inserting Number From A Textfield)(3)
  10. Letting Users Add Mysql Data With Php(1)
  11. Dynamic Php Image And Better Php Code Question(10)
  12. Php Random Selector(2)
  13. Activation Code(7)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 04:34 PM