Jump to content

Paginator help - displays ALL records instead of number requested


scmeeker

Recommended Posts

I'm trying to add the Paginator script to my web page and it's almost there BUT it's bringing up all the records from my database instead of the select number of records.  I'm posting my PHP code below (purposely leaving out my MySql connections), then I've attached the PHP reference file: paginator.class.php that's referenced in the code.

 

Thanks for your help!

 

<?php
    include('paginator.class.php');
            
$mysqli = mysqli_connect("", "", "", "");


if (mysqli_connect_errno()) {
    printf("Connect falied: %s\n", mysqli_connect_error());
    exit ();
} else {
    $sql = "SELECT * FROM product";
    $res = mysqli_query($mysqli, $sql);
    $num_rows = mysqli_fetch_array($res);
    $pages = new Paginator;
    $pages->items_total = $num_rows[0];
    $pages->mid_range = 9; // Number of pages to display. Must be odd and > 3
    $pages->paginate();
  
    if ($res) {
        while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
        $detail = $newArray['id'];
        $photo = $newArray['photo'];
        $id = $newArray['title'];
        $price = $newArray['price'];
        
          
  list($width) = getimagesize($photo);
  // set the maximum width of the image here
  $maxWidth = 100;
  if ($width > $maxWidth)
  
      echo "<p><a href=\"painting.php\"><img alt=\"Image\" width=\"{$maxWidth}\" src=\"{$photo}\" /></a>";      
      echo "   Title:   ".$id." Price:   ".$price."<br/";
      
       
	}
   }else {
        printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
    }
    
    mysqli_free_result($res);
    mysqli_close($mysqli);
}
?>
<br />
  <br /> 
      
       <?php 
  echo $pages->display_pages();
  echo "<span class=\"\">".$pages->display_jump_menu().$pages->display_items_per_page()."</span>";
  echo "<p class=\"paginate\">Page: $pages->current_page of $pages->num_pages</p>\n";
  ?>
  <br />

 

[attachment deleted by admin]

Hi there scmeeker,

 

one thing jumps out:-

 

$res = mysqli_query($mysqli, $sql);

    $num_rows = mysqli_fetch_array($res);

 

change to:-

 

$res = mysqli_query($mysqli, $sql);

    $num_rows = mysqli_num_rows($res);

$pages->items_total = $num_rows;// then know off the array reference

 

this will just return what you want, and should be a little bit better.

 

Cheers,

Rw

Thanks Rw.  I tried what you suggested but all the records are still coming up. 

 

I was also confused when you said "//then know off the array reference."  What did you mean by that?

 

$res = mysqli_query($mysqli, $sql);

    $num_rows = mysqli_num_rows($res);

$pages->items_total = $num_rows;// then know off the array reference

 

Any other suggestions? Thanks for your help. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.