Jump to content

Recommended Posts

Hello,

 

I am very close to getting this pagination to work.  The first page is fine, but then when I click on the hyperlinks for the next pages, the results are blank.  After reading other threads where people have had the same problem, it appears that it is a result of a variable not being passed through to the next pages.

 

However, I have the variable "find" as both a session variable and an addition to the URL of the next pages, and the next pages are still blank.  Any ideas why it might not be working?  Is "find" the right variable for me to be passing through to the next pages?

 

Thanks,

 

John

 

 

<?php
session_start();
$find = strip_tags($find);
$find = trim ($find);
$find = strtolower($find);
$_SESSION['find'] = $find;

?>

<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
unset($_SESSION['find']);	

}

// Otherwise we connect to our Database
mysql_connect("mysqlv3", "username", "password") or die(mysql_error());
mysql_select_db("sand2") or die(mysql_error());

// We preform a bit of filtering


$find = strip_tags($find);
$find = trim ($find);
$find = strtolower($find);


$result=mysql_query("SHOW TABLES FROM sand2 LIKE '%$find%'")
or die(mysql_error());



if(mysql_num_rows($result)>0){
while($table=mysql_fetch_row($result)){

$presult = mysql_query("SELECT COUNT(*) FROM `$table[0]`") or die(mysql_error());

$rr = mysql_fetch_row($presult);  
$numrows = $rr[0]; 
$rowsperpage = 20; 
$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; 


print "<p class=\"topic\">$table[0]</p>\n";
$r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC LIMIT $offset, $rowsperpage");



print "<table class=\"navbar\">\n";
while($row=mysql_fetch_array($r)){


$effective_vote = $row['votes_up'] - $row['votes_down']; 

print "<tr>";

print "<td>".'<a href="http://'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>";
print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>";
print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Vote.'</a>'.'</span>'."</td>";
}
print "</tr>\n";
}
print "</table>\n";


$range = 3;  
  
/******  build the pagination links ******/  
// range of num links to show    
  
// 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&find={$find}'><<</a> ";  
   // get previous page num  
   $prevpage = $currentpage - 1;  
   // show < link to go back to 1 page  
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&find={$find}'><</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&find={$find}'>$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&find={$find}'>></a> ";  
   // echo forward link for lastpage  
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&find={$find}'>>></a> ";  
} // end if  
/****** end build pagination links ******/  


}


else{
print "None found";
}



//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($result);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
unset($_SESSION['find']);	

}


}
?> 

When you load the page and look at the links, do they actually pass the number within the URL correctly?

 

If so, you'd be best running some debugging on your variables. For example check each variable one by one, at the right point in the code, by adding an easy to find statement like:

 

die('foo ' . $var_name);

 

My bet is one of your variables isn't what you expect it to be...

At a quick glance, is there definitely records on the next pages? For your loop you seem to be manually setting the "$range" variable you use to 3 - although I don't full understand the logic behind that loop it would seem it will display several pages even if there isn't any.

 

Try the debugging as well..

Yes, there are records on the next page for the table I keep testing. 

 

The $range variable equals how many page links are to be displayed on each side of the current page, and it seems to be working just fine.  It all works great-- it's just that the next page results are blank. 

 

My problem seems rather common, and people are told to simply pass a variable to the next page.  I think because of the way my code is set up, it's a little challenging to figure out what exactly I should pass on as a variable.

The "currentpage" var looks to be getting passed along to the pages just fine, so does the "find" var.

 

You need to DEBUG your script!

 

you'd be best running some debugging on your variables. For example check each variable one by one, at the right point in the code, by adding an easy to find statement like:

 

die('foo ' . $var_name);

 

My bet is one of your variables isn't what you expect it to be...

die() is a function that simply kills the script and optionally outputs a message - it's very useful for debugging.

 

The idea behind foo is you can then go "Ctrl + F" type in 'foo' and you'll find the output of the variable when you run the script. $var_name was just an example though, go through your variables, think about what the output should be and then check the output when you run the script. If it's not what you expect, or it's blank, you've probably found your problem.

 

PHP Debugging Tips.

 

Good luck!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.