Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

Problem displaying results on more than one page

I have a page and I display the results of a query on that page.  I am doing it on more than one page, 9 results per page.  I have code that does just that, but it doesnt' work 100%... My problem is as follow : The fist time I display the page, it lists ALL the results, with a link to page 2 -3-4 etc... at the bottom of my page and when I click these links, then it does display 9 per page.  Only the first time does it list it all... I looked at my code over and over and can't find where I messed up.  I do ask for 9 to be displayed, so why does it print it all?

Here is the page : http://www.artizangifts.com/display.php?category=necklaces

you'll see on the first page, there are 18 results.  There should be only 9.  But then, if you click on the link at the bottom of the page, it goes fine.

Here is my code :


//here is the connection string to the database not displayed for security

$category = $_GET[category];

$display_number = 9;

// If we dont know how many pages there are, make that calculation.
if (!isset($num_pages)) {

  // Determine the query.
     if (isset($letter)) { // Browsing a particular letter or the whole thing?
          $query1 = "SELECT * FROM $category WHERE Short_Desc LIKE '$letter%' ORDER BY whatever ASC";
     } else {
          $query1 = "Select * from $category";
     }

     // Query the database.
     $query_result1 = mysql_query ($query1) or die (mysql_error());

     // Calculate the number of pages required.
     $num_results = @mysql_num_rows ($query_result1);
     if ($num_results > $display_number) {
          $num_pages = ceil ($num_results/$display_number);
     } elseif ($num_results > 0) {
          $num_pages = 1;
     } else {
          echo 'There are no things in this category.';
     }

     $start = 0; // Currently at item 0.
}


// Make the new, limited query.
if (isset($letter)) {
          $query = "SELECT * FROM $category WHERE Short_Desc LIKE '$letter%' ORDER BY Short_Desc ASC LIMIT $start, $display_number";
     } else {
          $query = $query = "Select * from $category";
}

$result = mysql_query($query);

if ($code != '') {
$code = $code; }
else {
$code = 'A';
}

echo "<table width=100% cellpadding=\"10\" cellspacing=\"10\">";
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr > ";
    echo "<td bgcolor=#7C0000 height = 100 width=33%>
      
      <table><tr><td>
       <img src=http://www.artizangifts.com/admin/pictures/{$row['Picture']} width= 75></td><td bgcolor=#7C0000 height = 125>Item #{$row['Item_Number']}<br><br>{$row['Short_Desc']}<br><br><br><b>\${$row[$code]}</b><br><br><a onclick=\"window.open('description.php?itemnumber=$row[ID]&category=$category', 'popup', 'width=600, height=600, menubar=no, scrollbars=no, toolbar=no, location=no, resizable=no, top=50, left=50')\" style='cursor:pointer;cursor:hand'><img src=http://www.artizangifts.com/images/btn_0_1.gif border=0></a>
       </td></tr></table>

      
       </td>";
    for ($i=0;$i<2;$i++){
    echo ($row = mysql_fetch_assoc($result))?"<td bgcolor=#7C0000 height = 125 width=33%>
      <table><tr><td>
      
      <img src=http://www.artizangifts.com/admin/pictures/{$row['Picture']} width = 75></td><td>{$row['Item_Number']}<br><br>{$row['Short_Desc']}<br><br><br><b>\${$row[$code]}</b><br><br><a onclick=\"window.open('description.php?itemnumber=$row[ID]&category=$category', 'popup', 'width=600, height=600, menubar=no, scrollbars=no, toolbar=no, location=no, resizable=no, top=50, left=50')\" style='cursor:pointer;cursor:hand'><img src=http://www.artizangifts.com/images/btn_0_1.gif border=0></a>
      </td></tr></table>
      </td>":'';
    }
    echo "</tr> ";
}
echo "</table>";

// Make the links to other pages, if necessary.
if ($num_pages > 1) {

     echo '<hr width="50%" align="left" />';

     // Determine what page the script is on.
     if ($start == 0) {
          $current_page = 1;
     } else {
          $current_page = ($start/$display_number) + 1;
     }

     // If it's not the first page, make a Previous button.
     if ($start != 0) {
          echo '<a href="displaytest.php?category='; echo "$category"; echo '&start=' . ($start - $display_number) . '&num_pages=' . $num_pages . '&letter=' . $letter . '">Previous</a> ';
     }

     // Make all the numbered pages.
     for ($i = 1; $i <= $num_pages; $i++) {
          $next_start = $start + $display_number;
          if ($i != $current_page) {
               echo '<a href="displaytest.php?category='; echo "$category"; echo '&start=' . (($display_number * ($i - 1))) . '&num_pages=' . $num_pages . '&letter=' . $letter . '">' . $i . '</a> ';
          } else {
               echo $i . ' ';
          }
     }

     // If it's not the last page, make a Next button.
     if ($current_page != $num_pages) {
          echo '<a href="displaytest.php?category='; echo "$category"; echo '&start=' . ($start + $display_number) . '&num_pages=' . $num_pages . '&letter=' . $letter . '">Next</a> ';
     }

}




?>                                            
0
John Account
Asked:
John Account
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
         $query = $query = "Select * from $category";

should be rather:

          $query = $query = "Select * from $category LIMIT $display_number ";
0
 
John AccountAuthor Commented:
Great!  That fixed it!  Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now