Jump to content

Pagination with a search engine


Ganondorf

Recommended Posts

Hello. 

 

Im fairly new to php but i like to think im reasonably competent. To set the scene, ive got a website that displays, for simplicity, images. I also have some scripts that show this in a paginated form when they get above x number. If i merely had a blank page(s) with lists of images that broke into  new page every 25th image then my script could handle that without an issue. 

 

However, as you may have already guessed, it is not just quite as simple as that. I also have a search function on this page. As a separate entity it works very well, and its able to search the database correctly for the search terms. It can also show the data in paginated form - ie, if i have 24 images selected from the search terms then it will show that there is only 1 page, if my settings dictate a page break on the 25th image. 

 

The issue arises in a few scenarios:

 

a) if someone has searched, is given a page of results, and then clicks the "1st page" link the pagination scripts create, then the search is lost and it returns to the complete list of unfiltered images. 

 

b) If I have more than, for example 27 images (ie a need for TWO pages post search), then when someone clicks the "2nd page" link, instead of seeing page 2 of the searched for images, they see page two of the unfiltered image, ie, it seems that the script is unable to carry the search terms across pages and act upon them.

 

So, in simple form, I have a pagination script that paginates, and a search function that searches, but im having trouble integrating them both. 

 

Below are my scripts: 

 

This creates the page numbers at the bottom: (NB i have tried to alter the link url by appending both page number and the "lookup" (search terms). 

<?php
                     
if ($totalcountplus >= 1 && $page <= $totalcountplus) {
for ($x = 1; $x<=$totalcountplus; $x++) {
echo ($x == $page) ? '<strong><a href="?page='.$x.'&?lookup='.$searchword.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'&?lookup='.$searchword.'">'.$x.'</a> ';}}

?>

Here we have the actual script that sorts the pagination:

 

<?php


$searchword = $_GET['lookup'];
$expsearchword = explode(" ", $searchword);




$searchquery = "SELECT DISTINCT user_id FROM `images` WHERE ";
$searchquerytwo = "SELECT COUNT(DISTINCT user_id) FROM images WHERE ";


foreach ($expsearchword as $item) {
    $t++;
    if ($t == 1) 
        $searchquery .= "`description` LIKE '%$item%' OR `keywords` LIKE '%$item%'";
    else
        $searchquery .= "OR `description` LIKE '%$item%' OR `keywords` LIKE '%$item%'";
}


foreach ($expsearchword as $item) {
    $r++;
    if ($r == 1) 
        $searchquerytwo .= "`description` LIKE '%$item%' OR `keywords` LIKE '%$item%'";
    else
        $searchquerytwo .= "OR `description` LIKE '%$item%' OR `keywords` LIKE '%$item%'";
}


$per_page = 25;
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$totalcount = mysql_query($searchquerytwo);
$totalcountarray = mysql_fetch_array($totalcount);


$totalcountplus = ceil(mysql_result($totalcount, 0) / $per_page);
$totalcountone = mysql_query("$searchquery LIMIT $start, $per_page");
while($totalcountplusone = mysql_fetch_array($totalcountone))  {  ;
$usercountone  =$totalcountplusone['0'];
}


?>

I also have this while loop running on another page. its here for info only, and i dont think its contributing to my issues:

 

$allimagesone = mysql_query("$searchquery LIMIT $start, $per_page");
while($airesult = mysql_fetch_assoc($allimagesone)) {  ETC ETC ETC ; } ?>

I know this is pretty long winded but i hope you guys can help me out because im a real loss as to how to combine the two functions successfully. Basically i think it boils down to getting the url to "remember" both the page number AND the search terms, and im not quite sure how to do that successfully. All advice is welcome, thanks in advice.

Edited by Ganondorf
Link to comment
Share on other sites

I noticed this, should only be one question mark to start the query, the rest should start with only &

<a href="?page='.$x.'&?lookup='.$searchword.'">

 

You should filter/sanitize anything being inserted into mysql, is unsafe the way you have it.

try mysql_real_escape_string() , and as can see on the page there this is a deprecated function, so should be using mysqli related functions versus mysql

 

I made 2 pagination examples a while ago can look over if would help at all

http://dyna.dyndns.tv/paginate/ and http://dynaindex.com/paginate/

 

 

 

 

 

 

 
Link to comment
Share on other sites

 

I noticed this, should only be one question mark to start the query, the rest should start with only &

<a href="?page='.$x.'&?lookup='.$searchword.'">

 

You should filter/sanitize anything being inserted into mysql, is unsafe the way you have it.

try mysql_real_escape_string() , and as can see on the page there this is a deprecated function, so should be using mysqli related functions versus mysql

 

I made 2 pagination examples a while ago can look over if would help at all

http://dyna.dyndns.tv/paginate/ and http://dynaindex.com/paginate/

 

Well....believe it or not removing the extra "?" has fixed the entire issues. Thanks for the links anyway guys. 

Link to comment
Share on other sites

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.