473,387 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with Pagination code

The only problem I'm having with this code is when there are no values returned for the query, it is still allowing you to see and click the 'Next>>'. It disappears when there is at least one return on the search, but when there is none, it shows up. Here is where I got the source code from and of course the forum is 404'd. They're code has the exact same problem as my modified code.

http://www.designplace.org/scripts.php?page=1&c_id=25

my code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.   // Get the search variable from URL
  4.  
  5.   $var = @$_GET['KEYWORDS'] ;
  6.   $trimmed = trim($var); //trim whitespace from the stored variable
  7.  
  8. // rows to return
  9. $limit=8; 
  10.  
  11.  
  12. // check for a search parameter
  13. if (!isset($var))
  14.   {
  15.   echo "<p>Our online inventory doesn't currently contain what you're looking for. Please visit a store for complete selection.</p>";
  16.   exit;
  17.   }
  18.  
  19. mysql_connect("sql002.choiceone.net","roomfulexpress","x47rkv7N"); //(host, username, password)
  20. mysql_select_db("roomfulexpress") or die("Unable to select database"); //select which database we're using
  21.  
  22. // Build SQL Query  
  23. $query = "select * from tblIndividualItem where KEYWORDS like \"%$trimmed%\"  
  24.   order by ITEM_NAME"; // EDIT HERE and specify your table and field names for the SQL query
  25.  
  26.  $numresults=mysql_query($query);
  27.  $numrows=mysql_num_rows($numresults);
  28.  
  29. // If we have no results, offer a google search as an alternative
  30.  
  31. if ($numrows == 0)
  32.   {
  33.   echo "<p>Our online inventory doesn't currently contain what you're looking for. Please visit a store for complete selection.</p>";
  34.   }
  35.  
  36. // next determine if s has been passed to script, if not use 0
  37.   if (empty($s)) {
  38.   $s=0;
  39.   }
  40.  
  41. // get results
  42.   $query .= " limit $s,$limit";
  43.   $result = mysql_query($query) or die("Couldn't execute query");
  44.  
  45. // display what the person searched for
  46. echo "<p>Search for:&nbsp;" . $var . "</p>";
  47.  
  48. // begin to show results set
  49. echo "<p>Results</p>";
  50. $count = 1 + $s ;
  51.  
  52.  
  53. // now you can display the results returned    
  54.   while ($row = mysql_fetch_array($result)) 
  55.  
  56.  
  57. {
  58. echo("<table>");
  59. echo("<tr>");
  60.     echo("<TD class=noOverflow width=150px><a href=itemprofile.php?SKU=". $row["SKU"] . " target=content> <img src= " . $row["TN"] . " alt=click to view profile /></a></TD>");
  61. echo("</tr>");
  62. echo("<tr>");
  63.     echo("<TD width=150px><h1> " . $row["ITEM_NAME"] . "</h1></TD>");
  64. echo("</tr>");
  65. echo("<tr>");
  66.     echo("<TD width=150px> $" . $row["ITEM_PRICE"] . "</TD>");
  67. echo("</tr>");
  68.     }
  69.  
  70.  
  71.     echo("</table>");
  72.  
  73.  
  74. $currPage = (($s/$limit) + 1);
  75.  
  76.  
  77.  
  78.   //Link to other results
  79.   echo("<div class=nextPrev>");
  80.   if ($s>=1) { // bypass PREV link if s is 0
  81.   $prevs=($s-$limit);
  82.   print "<p>&nbsp;<a href=\"$PHP_SELF?s=$prevs&KEYWORDS=$var\">&lt;&lt; 
  83.   Prev</a></p>";
  84.   }
  85.  
  86. // calculate number of pages needing links
  87.   $pages=intval($numrows/$limit);
  88.  
  89. // $pages now contains int of pages needed unless there is a remainder from division
  90.  
  91.   if ($numrows%$limit) {
  92.   // has remainder so add one page
  93.   $pages++;
  94.   }
  95.  
  96. // check to see if last page
  97.   if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
  98.  
  99.   // not last page so give NEXT link
  100.   $news=$s+$limit;
  101.  
  102.   echo "<p>&nbsp;<a href=\"$PHP_SELF?s=$news&KEYWORDS=$var\">Next &gt;&gt;</a></p>";
  103.   }
  104.  
  105. $a = $s + ($limit) ;
  106.   if ($a > $numrows) { $a = $numrows ; }
  107.   $b = $s + 1 ;
  108.   echo "<p>Showing items $b - $a of $numrows items</p>";
  109.   echo "</div>";
  110. ?>
Jul 27 '07 #1
2 2158
http://www.roomfulexpress.com/newsite/php/item.php?KEYWORDS=LIGHTING - this link shows you how it should work.

http://www.roomfulexpress.com/newsite/php/item.php?KEYWORDS=BROKEN -
this shows you the 'Next >>'
Jul 27 '07 #2
kovik
1,044 Expert 1GB
Until you get the CODE tags into your post, I'm afraid that I'm not willing to waste time trying to read it. Sorry. However, I have written a tutorial on basic pagination in PHP that you may be interested in. I've also written a much more advanced class, but I've yet to write a tutorial for how it's made.
Jul 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Faree | last post by:
I am workign on a news portal which needs paginaiton as usual.but all the code i got depends on the number of records that will come from database :( .it is working well too. But i need...
4
by: Fernando Chilvarguer | last post by:
Hi, I've been trying to figure this one out for a while and I'm stalled. Here's my scenario: - a Parent window with a datagrid on it. The datagrid has pagination enabled and has an "Edit"...
2
by: Chris H | last post by:
I am having a problem with pagination, basically the problem is happening in the "PREV / NUMBERS / NEXT" links, it appears as if the reason is becasue the increment and decrement operators aren't...
11
by: ste | last post by:
Hi there, Further to my recent posts where I've received excellent help from Rik and Jerry, I've ended up with an image gallery on my website that displays images in a table, 3 images per row. ...
0
by: Sheau Wei | last post by:
I am using php version 4.3.3. The below was my pagination code. My problem is that when i run this code in my computer, i wall fail because of the hyperlink of the pagination not function when i...
1
by: OceanBreeze | last post by:
I am new to .Net. I am using ASP 2.0 and C#. I want to pupolate a data grid programatically using the values obtained from a list conating domain objects. E.g., DAL.GetEmployee() returns a...
0
by: OceanBreeze | last post by:
I am using C# Page_Laod function to populate a web page with a table populated with several records. The table, rows (containing values) are popuated programatically with the values obtained from...
1
by: shalini jain | last post by:
Hi, I want to know how can we do pagination using XSL. There are number of tutorials available on pagination using PHP but nothing with XSL. i am really stuck with my code. Below is the code that...
3
oll3i
by: oll3i | last post by:
echo "<div id=\"join\">"; echo "<div id=\"main\">"; echo "<div id=\"contents\">"; echo "<div id=\"left_strip\">"; echo "<div id=\"content_table\">"; content here echo "</div>";
0
by: George Ter-Saakov | last post by:
Trying to quickly create paginated grid. So i am using ListView. Unfortunatelly pagination works a little strange. Pagination control appears correctly. But when i click "2" page postback...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.