Jump to content


pagination with mysql


5 replies to this topic

#1 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 04 January 2006 - 12:42 PM

allright this is my code, i left irrelevant code away.
<?php
include("db.inc.php");

$page = $_GET['page'];

$countbedrijven = mysql_query("SELECT * FROM bedrijven");
$count = mysql_num_rows($countbedrijven);
$max = 8;
$nopaginas = $count / $max;
$nopaginas = ceil($nopaginas);

$first = $page * $max - $max;
$last = $page * $max;

$getbedrijven = mysql_query("SELECT * FROM bedrijven ORDER BY id ASC LIMIT $first , $last ");
while($r=mysql_fetch_array($getbedrijven)){
// THE CODE TO ECHO ALL THAT STUFF GOES HERE
}
?>
Im trying to show 8 results on each page. On page one it work great, it shows 8 results, on page 2 it suddenly shows 10 results!!! wth? on page three it shows the last two results of page 2 + another one. I currently have 17 rows in my database. Im completly stuck, confused and pissed.
plz help, ty

Edited by Avalanche, 04 January 2006 - 02:13 PM.


#2 Squid

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:Netherlands
  • Interests:Webdesign, php coding, visual basic programming

Posted 04 January 2006 - 03:55 PM

I'd suggest you would try out the following:

<?
include("db.inc.php");

// the below makes sure the page is set to [b]1[/b] if there is no page submitted
if(!isset($_GET['page'])){ 
	$page = 1; 
} else { 
	$page = $_GET['page']; 
} 

$first = 8;
$last = (($page * $first) - $first); 

$getbedrijven = mysql_query("SELECT * FROM bedrijven ORDER BY id ASC LIMIT $first, $last"); 

while($r=mysql_fetch_assoc($getbedrijven)){
	echo $row['id']."<br />"; 
} 
?>

Still having problems? Just reply again.

Edited by Squid, 04 January 2006 - 03:55 PM.


#3 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 04 January 2006 - 04:28 PM

nvm, figured it out already, thanks to squid who pm'ed me some usefull links :)

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 04 January 2006 - 04:35 PM

Actually squid, your code still has a problem i'm noticing. $last needs to stay the same. $first needs to be the one that changes.
<?php
include('db.inc.php');

$page = ($_GET['page'] ? $_GET['page'] : 1);

$total = mysql_query("SELECT * FROM bedrijven");
$total = mysql_num_rows($total);
$max = 8;
$total_pages = ceil($total/$max);

$from = ($page*$max)-$max;

$sql = mysql_query("SELECT * FROM bedrijven ORDER BY id ASC LIMIT $from,$max");
while($r=mysql_fetch_array($sql)){
// THE CODE TO ECHO ALL THAT STUFF GOES HERE
}
Hopefully that should work as expected.

note: i did change some of your variable names, just to give a better sense of what they are.

Edited by rc69, 04 January 2006 - 04:37 PM.


#5 Squid

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:Netherlands
  • Interests:Webdesign, php coding, visual basic programming

Posted 04 January 2006 - 05:13 PM

Yup you're right, I was too fast with replying so I made a mistake. Anyways, I left the variable names the same as Avalanche had them, so he didn't have to wonder what everything meant since he already made his own code. Thanks for pointing that out though.

Edited by Squid, 04 January 2006 - 05:16 PM.


#6 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 04 January 2006 - 06:40 PM

i first thought i had to do it like this
page 1: $first = 0 - $last = 8
page 2: $first = 8 - $last = 16
etc.
thats why it was all screwed up

(o, and i prefer to write my variables in my native language ;) )

now i got this cleared out, i gotta get started coding a simple forum, so be prepared for a lot of questions from me :)

Edited by Avalanche, 04 January 2006 - 06:42 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users