mysql - PHP Pagination Issue -
i have search trying have pagination with. found cool php script, at phpeasystep (yeah, i'm new @ php, , programming.) implemented it, , worked display pagination , go forward page( page 1 page 2), when go page 2 page 1, search query gone , displays every single database entry (but paginated). search query must getting lost somewhere?

the code ( when pasted got compacted i've done best indent i'm super sorry if still hard read. if hard read on website got in better indentation...there link above!)
<?php $q = mysql_real_escape_string(ucfirst(trim($_get['searchquery']))); ?> <?php require('config.php'); $tbl_name="companies"; $adjacents = 3; $query = "select count(*) num $tbl_name company_name '%$q%' or company_description '%$q%' or cat1 '%$q' or cat2 '%$q' or cat3 '%$q' or company_phone '%$q'"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[num]; $targetpage = "search.php"; $limit = 10; $page = $_get['page']; if($page){ $start = ($page - 1) * $limit; } else{ $start = 0; } $sql = "select * $tbl_name company_name '%$q%' or company_description '%$q%' or cat1 '%$q' or cat2 '%$q' or cat3 '%$q' or company_phone '%$q' order company_name limit $start, $limit"; $result = mysql_query($sql); if ($page == 0) $page = 1; $prev = $page - 1; $next = $page + 1; $lastpage = ceil($total_pages/$limit); $lpm1 = $lastpage - 1; $pagination = ""; if($lastpage > 1) { $pagination .= "<div class=\"pagination\">"; //previous button if ($page > 1) $pagination.= "<a href=\"$targetpage?page=$prev+searchquery=$q\">� previous</a>"; else $pagination.= "<span class=\"disabled\">� previous</span>"; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages bother breaking { ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter+searchquery=$q\">$counter</a>"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages hide { //close beginning; hide later pages if($page < 1 + ($adjacents * 2)) { ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter+searchquery=$q\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1+searchquery=$q\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage+searchquery=$q\">$lastpage</a>"; } //in middle; hide front , elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage?page=1+searchquery=$q\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2+searchquery=$q\">2</a>"; $pagination.= "..."; ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter+searchquery=$q\">$counter</a>"; } $pagination.= "..."; $pagination.= "<a href=\"$targetpage?page=$lpm1+searchquery=$q\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage?page=$lastpage+searchquery=$q\">$lastpage</a>"; } //close end; hide pages else { $pagination.= "<a href=\"$targetpage?page=1+searchquery=$q\">1</a>"; $pagination.= "<a href=\"$targetpage?page=2+searchquery=$q\">2</a>"; $pagination.= "..."; ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage?page=$counter+searchquery=$q\">$counter</a>"; } } } //next button if ($page < $counter - 1) $pagination.= "<a href=\"$targetpage?page=$next+searchquery=$q\">next �</a>"; else $pagination.= "<span class=\"disabled\">next �</span>"; $pagination.= "</div>\n"; } ?> if need more code, can provide! , help!
oh forgot tried fix adding +searchquery=$q end of pagination urls thats why there.
the line:
if ($page > 1) $pagination.= "<a href=\"$targetpage?page=$prev+searchquery=$q\">� previous</a>"; should be:
if ($page > 1) $pagination.= "<a href=\"$targetpage?page=$prev&searchquery=$q\">� previous</a>";
Comments
Post a Comment