Jump to content
xisto Community
Sign in to follow this  
Alexandre Cisneiros

[PHP + MySQL] Separating The Results By Pages Simple code

Recommended Posts

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.
-------------------------------------------------------------------

<?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!! =Dwhile ($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]]
<?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'}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.