Link to home
Create AccountLog in
Avatar of Toube
Toube

asked on

Php mysql paging problem

Hello,

I recently asked a similar question but I didn't get any answers so here it goes again.

I have paging class that handles the paging for my database queries and all is working well except for when I try do handle the paging through a search.. then the paging brakes down and doesn't work any more.

the search is not passed to the next page and so on, how can I fix this?

Regards,

-T
// This is used for getting the reult in to the pager class
 
   class Pager
   {
       function getPagerData($numHits, $limit, $page)
       {
           $numHits  = (int) $numHits;
           $limit    = max((int) $limit, 1);
           $page     = (int) $page;
           $numPages = ceil($numHits / $limit);
 
           $page = max($page, 1);
           $page = min($page, $numPages);
 
           $offset = ($page - 1) * $limit;
 
           $ret = new stdClass;
 
           $ret->offset   = $offset;
           $ret->limit    = $limit;
           $ret->numPages = $numPages;
           $ret->page     = $page;
 
           return $ret;
       }
   }
 
$pageid = $_GET['id'];
$search = $_POST['search'];
$page = $_GET['main_page'];
require ('paging_function.php'); // get pager function
 
//Search query for the messages.
$sqlquery = "SELECT p.ID as postid, p.topicid, DATE_FORMAT(p.timestamp, '%d.%m.%Y %H:%i') AS insertdate,
DATE_FORMAT(p.timestamp, '%d.%m.%Y') AS time, DATE_FORMAT(p.timestamp,'%H:%i') AS minutes, p.timestamp as sent, p.post as post, DATE_FORMAT(t.topictime, '%d.%m.%Y %H:%i') AS topict, t.topicname as topicname, t.userlevel_topic as userlevel, t.ID as topicid, u.username as username, (SELECT count(*) FROM posts WHERE posts.topicid=t.id) as count,(select count(ID) as num from posts where topicID=t.id and ID <p.id) as msgbefore, u.users_id as usersid, c.lastread as userlastread FROM users u, posts p, topics t LEFT OUTER JOIN forum_read c ON (c.theader_=t.id AND c.user_ = '$userid') WHERE p.topicid=t.ID and t.deleted ='0' AND u.users_id = p.user_ and u.users_id=t.user_ and MATCH(p.post) AGAINST('%$search%' IN BOOLEAN MODE) group by postid order by p.timestamp desc";
 
 
$result2 = mysql_query($sqlquery);
 
$total = mysql_num_rows($result2);
//$amount1 = mysql_result($useramount, 0, 0);
$limit = 10;
$pager  = Pager::getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit  = $pager->limit;
$page   = $pager->page;
 
$numpages = $pager->numPages;
 
if (($page != $_GET['main_page']) && ($pager->numPages > 1))
{
    $pager  = Pager::getPagerData($total, $limit, $pager->numPages);
    $offset = $pager->offset;
    $page   = $pager->page;
}
 
//call the pager function:
page_count_search($total, $page, $limit, $numpages, $offset, $pager, $pageid);
 
// paging
 
function page_count_search($total, $page, $limit, $numpages, $offset, $pager, $pageid)
{
 
if ($total >= 20)
{
echo "<div class=\"member_links\" style=\"margin-bottom: 3px; font-size: 9px;\">";
 
if ($page == 1)
    echo "<img style=\"border: none;\" src=\"../images/red_arrow_back.gif\" alt=\"Previous page\"/>";
 
    else
    echo "<a href=\"index.php?id=$pageid&amp;main_page=" . ($page - 1) . "\"><img style=\"border: none;\" src=\"../images/red_arrow_back.gif\" alt=\"edellinen sivu\" title=\"edellinen sivu\" /></a>";
$ingap = false;
for ($i = 1; $i <= $numpages; $i++)
{
      if ($i <= 3 || $i > ($numpages -3) || abs($page - $i) < 2)
      {
            $ingap = false;
            echo "  ";
            if ($i == $page)
            {
                  echo "<span style=\"font-weight: bold; color: #000000; background-color: #e4900d; padding-left: 2px; padding-right: 2px;\">$i</span>";
            }
            else
            {
                  echo "<a href=\"index.php?id=$pageid&amp;main_page=$i\">$i</a>";
            }
      }
      else if (!$ingap)
      {
            $ingap = true;
            echo "  ... ";
      }
}
echo "  ";
    if ($page == $pager->numPages)
    echo "<img style=\"border: none;\" src=\"../images/red_arrow_front.gif\" alt=\"Next page\" />";
 
    else
    echo "<a href=\"index.php?id=$pageid&amp;main_page=" . ($page + 1) . "\"><img style=\"border: none;\" src=\"../images/red_arrow_front.gif\" alt=\"seuraava sivu\" title=\"seuraava sivu\" /></a>";
    echo "</div>";
    }
   else
   {
        echo "";
   }
 
}
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MatthewP
MatthewP
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Also, when I've done things like this in the past, I've kept to using form POSTs rather than using GETs. I've done this by creating a form at the top of the page:

<form action="mySearchCode.php" method="post" name="searchform">
<input type="hidden" name="search" value="$search">
<input type="hidden" name="load_page" value="">
</form>

Then i'll call javascript functions with my links:

// example javascript function
function goToPage(page){
document.forms['searchform'].elements['load_page'].value=page;
document.forms['searchform'].submit();
}

Your links would then become like:

<a href="Javascript:goToPage(" . $page_to_load . ")">Next Page</a>

As long as you can keep track of which page you want to call in your PHP $page_to_load variable, i've had good results with this as it keeps things nice and clean on the front end.

Of course, it depends on exactly what you're doing as to whether this is of any benefit, but POST is slightly more secure than GET. I think the real reason i started doing this was because my browser was caching my URLS with GET and the results were changing very frequently.

Matt

Avatar of Toube
Toube

ASKER

Easy.. got it thanks :)