0

I am new to PHP programming i have coded a pagination script in php to show page number when the values exceeds a particular limit. I have tested my script locally (in Wamp) it is working as expected on local server. but when it comes to the live/prod version the same code is not working there.

Adding the details of the code, Please help me with your knowledge

<div class="postarea">

<?php 
include('includes/db.php');
$noresults =8;
$stmt= $connection->prepare( "select j_id, gender, class, subject, location, cname, cno, time, pay, skill, experience, cdate from tutorjob order by 1 desc");
$stmt->execute();


$stmt->bind_result($j_id,$gender, $class, $subject, $location, $cname, $cno, $time, $pay, $skill, $experience, $cdate);
while($stmt->fetch())

{


    //$description=substr($row['content'],0,200);
    //$image=$row['image'];
    ?>



<?php
}
$count = $stmt->num_rows;

?>

<?php 

if (!isset($_GET['page'])) {
    $page = 1;
}else { 
$page= $_GET['page'];
}

$tpr = ($page-1)*$noresults;
 $nopages = ceil($count/$noresults);


 $stmt1= $connection->prepare( "select j_id, gender, class, subject, location, cname, cno, time, pay, skill, experience, cdate from tutorjob order by 1 desc limit ?,?");
 $stmt1->bind_param("ss", $tpr, $noresults);
$stmt1->execute();

$stmt1->bind_result($j_id, $gender, $class, $subject, $location, $cname, $cno, $time, $pay, $skill, $experience, $cdate);
while($stmt1->fetch())

{


    //$description=substr($row['content'],0,200);
    //$image=$row['image'];
    ?>

    <div class="entry">


    <table>
<tr>

   <th colspan="4"><i class="fas fa-paperclip"></i><b> Job Expiry Date</b> &nbsp; <?php echo $cdate; ?></th>

     </tr>
      <tr><td class="hd">
     Gender </td><td> <?php echo $gender; ?>
     </td><td class="hd">
     Class </td><td><?php echo $class; ?> 
     </td></tr><tr><td class="hd">
     Subject </td><td><?php echo $subject; ?> 
     </td><td class="hd">
     Location </td><td><?php echo $location; ?> 
     </td></tr><tr><td class="hd">
     Contact Name </td><td><?php echo $cname; ?> 
      </td><td class="hd">
     Contact Number </td><td><?php echo $cno; ?> 
     </td></tr><tr><td class="hd">
     Experience </td><td><?php echo $experience; ?> 
     </td><td class="hd">
     Pay </td><td><?php echo $pay; ?> 
      </td></tr>
      <tr>
     <td class="hd">
    <b>Skills Required </b> </td><td  colspan="3"><?php echo $skill; ?>  
      </td>
     </tr>
       <th colspan="4"><div class="link"><i class="fas fa-envelope-open"></i><a href="tutorregister.php">Register/Apply for the job</a></div></th> 
</table>
</div> 
 <?php
}
?>
<div class="pga">
<?php
?>
 <?php
for ($page=1;$page<=$nopages;$page++) {
  echo  '<div class="pg"> <a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'">' . $page. '</a> </div> ' ;
?>

<?php
}
?>
</div>
</div>
  • 1
    What specifically goes wrong? And do you get an errors? – showdev Mar 10 '19 at 06:59
  • I was not able to get the pagination in live site, also it is not showing any errors!. What i can see over there is as per the sql limit function my posts are getting limited(i.e) if i want display 8 posts per page means it is showing only 8, but the post pagination is not showing or visible in the live version. Link : http://tutorforpupil.com/tutorjobs.php – Basith Mohamed Mar 10 '19 at 07:04
  • To troubleshoot, you might try outputting the `$count` and `$nopages` variables to see if they are what you expect. – showdev Mar 10 '19 at 07:08
  • Hi mate, i have tried to output the variables they are showing expected outputs properly. – Basith Mohamed Mar 10 '19 at 07:14
  • Maybe `$_SERVER['PHP_SELF']` (or something else) is causing an error. Try enabling PHP's [error output](https://stackoverflow.com/a/5438125/924299). – showdev Mar 10 '19 at 07:18
  • I'm now seeing the pagination on your live page. Were you able to find the problem? – showdev Mar 10 '19 at 07:20
  • i think i got a track on this issue, this section in my code is supposed to send the number of pages for pagination it is retreving 0 in prod site, but in local version it is showing correct values. $page = 1; if (!isset($_GET['page'])) { $page = 1; }else { $page= $_GET['page']; } $tpr = ($page-1)*$noresults; $nopages = ceil($count/$noresults); – Basith Mohamed Mar 10 '19 at 07:21
  • i have manually provided input to variable $nopages to debug (once after that pagination is showing), but the section that i have mentioned is not working properly in prod version. $page = 1; if (!isset($_GET['page'])) { $page = 1; }else { $page= $_GET['page']; } $tpr = ($page-1)*$noresults; $nopages = ceil($count/$noresults); – Basith Mohamed Mar 10 '19 at 07:23
  • Do you mean that `$nopages` is `0` on your production site? What is the value of `$count`? – showdev Mar 10 '19 at 07:28
  • 1
    Hi buddy, thanks a lot for your help i figured out the issue, it is because of the fact that $count = $stmt->num_rows; is returning 0 rows, i have used this code($stmt->store_result();) to store result before getting number of rows and now it is working fine, Thanks a lot for your help !!!!!!!!!!! – Basith Mohamed Mar 10 '19 at 07:44
  • Oh, I see. You are counting the rows of the statement rather than its resulting resource. Related: [Cannot get the number of rows and fetch when using MySQLi prepared statement](https://stackoverflow.com/questions/46262828/cannot-get-the-number-of-rows-and-fetch-when-using-mysqli-prepared-statement). You might consider posting and accepting your own answer to show what you found. It might help other coders in the future. – showdev Mar 10 '19 at 08:04

0 Answers0