0

I am trying to do something like when you press next button in my php file it will give you next record from database and display it. So I have the solution which is following:

$next_id = q("select * from sites where id > ".$siteid.
" and com_id = ".$current_company_id." and deleted = 0 order by id ASC LIMIT 1");
//print_r($next_id);
$tot_id = getNumberRows($next_id);


if($tot_id){
   while($next_site = $next_id->fetch(PDO::FETCH_ASSOC)) {
   $next_site_id = $next_site['id'];
  //echo $next_site['id'];
}   
} else {
   $next_site_id = 0;
}

if($next_site_id == 0) {
   //don't give button any more as last record......
} else {
   //echo $next_site_id;
?>
   <div class="pull-right" style="margin-left:10px !important;">
        <a href="sites.php?id=<?php echo $next_site_id; ?>">
            <button class="btn btn-info btn-s">Next</button>
        </a>
   </div> 


<?php   
}?>

Above solution is display one by one record and when there is no record next button will not display but everything is working so fine but somehow when it's reach to 10th record or 12 record or any number of record and then I press next it move to 1st record. So crazy I don't know what the hell is happening there.

So if anyone have better solution to this.?

Michael
  • 3,308
  • 5
  • 24
  • 36
Jimil
  • 650
  • 2
  • 14
  • 37

1 Answers1

0

Are you sure you have more than 10-12 records to show? Make a query without the limit and check the records.

codegaze
  • 755
  • 7
  • 15
  • Yes I have more than 20 records – Jimil Apr 10 '15 at 10:11
  • Then what is the url or the next_id you are getting before returning to 0 again? A possible solution would be If you have a caching engine in your site you can store your ids in an array for each company and use this array to move to next pages – codegaze Apr 10 '15 at 10:16
  • No I don't think so that would be the best thing to do and regarding url I already given in my code href="sites.php?id="> so weird I don't know why it;s behave like that and it's the same page where the url is given so for ex. if I am in www.site.com/sites.php?id=1 then when i press next it will reload the page with www.site.com/sites.php?id=2 – Jimil Apr 10 '15 at 10:22
  • what i meant was that you need to echo in the last page before returning to 0 your $next_id, $tot_id to see why if($tot_id){ returns false – codegaze Apr 10 '15 at 10:33
  • I tried that and I also found something when i move record from 1062 to 1071 works fine but in 1071 when I press next it put me back to 1053 so I found pattern in issue that when number have 1 at the end it's give me 1053 so when I reach to 1081 then it again give me 1053 becuse 1081 contain 1 so do you know how I can put that number fix – Jimil Apr 10 '15 at 11:33