Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am writing a pagination as I am new to php I have a problem in that. When give a constant to a variable inside the code the pagination works properly wen I pass it via the client side it just loads the first page and the second and other pages will not appear,
What is wrong with that?

here is the code

PHP
$sqlcount=("SELECT * FROM job_results where Title LIKE '%$kw%'");
    $row_result = mysql_query($sqlcount) or die(mysql_error());
    $rowspage = mysql_num_rows($row_result);
    $numrows =$rowspage;
    // number of rows to show per page
     $rowsperpage = 10;
   // find out total pages
     $totalpages = ceil($numrows / $rowsperpage);
     // get the current page or set a default
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
    $sqlcount=("SELECT * FROM job_results where Title LIKE '%$kw%' LIMIT $offset, $rowsperpage");
    $row_result = mysql_query($sqlcount) or die(mysql_error());
    //echo $sqlcount;
//end of pagination
    while($row = mysql_fetch_array($row_result, MYSQL_ASSOC)){
echo(...);
}

<pre lang="xml">$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
Posted
Updated 24-Aug-10 9:04am
v2

Can you pass your code, please? I don't see it on the other side of Ethernet wire.
 
Share this answer
 
hello,

The code is fine, which variable are you referring to? If you are referring to $kw, make sure to include it in the all the links. Example

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&kw=$kw'>$x</a> ";

Thanks,
Bryian Tan
 
Share this answer
 
Comments
nicolasssssss 25-Aug-10 12:12pm    
Hi Bryian,

Thanks for your answer,
I added to my code..but it doesnt work:(
I also added a hidden field to keep the value again nothing happened

This is what I added in html part: just take a look at hidden file:


Job Search :




Company Search :









and this is what I added to pagination part:

echo " > ";

Please help me on this

thanks
Bryian Tan 25-Aug-10 19:48pm    
I can't see the HTML code you pasted here. Can you re-post the HTML markup code again? replace the "<" with <
Other thing. Don't select all data from database. Why you need pagination if you always select all the data? Use LIMIT:
$query = "SELECT * FROM `table` LIMIT " . $_GET['currentpage'] * $items_per_page . ", " . $items_per_page;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900