Paging search results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wandergeek
Forum Newbie
Posts: 3
Joined: Fri Jul 03, 2009 1:29 pm

Paging search results

Post by wandergeek »

Hey guys,

I'm writing a script take takes some postdata from a search form, builds an array via a SQL query, and then paginates the results. The problem I'm experiencing right now stems from the array of results-- it disappears when the page is changed. I've been banging my head against this problem for a while now and I can't seem to figure it out.


I've uploaded the code here: http://snipt.org/knli, but here are the relevant snippets....

Code: Select all

$page=$_GET['page'];
$selectedgenre = $_POST['selectedgenre'];
$selecteddate = $_POST['selecteddate'];
$selectedvenue = $_POST['selectedvenue'];
if (($selectedgenre) || ($selecteddate) || ($selectedvenue)) 
{
    $page=1;
}
$numperpage=2;
 
 
 
function showResults($page,$results,$numperpage) {
    
    if($page==1) {
        $offset=0;
    } else {
        $offset=($page-1)*$numperpage+1;
    }
    
    $BandstoDisplay=array_slice($results,$offset,$numperpage);
    //print_r($BandstoDisplay);
    echo "<br><br><br>offset is " . $offset ."<br><br><br>";
    foreach($BandstoDisplay as $Band)
    {
        $Band=trim($Band);
        $SQL= "SELECT SHOWS.Date, SHOWS.Band, SHOWS.Venue, SHOWS.Allbands, BANDS.EmbedCode, SHOWS.Genre FROM SHOWS, BANDS WHERE BANDS.Band='$Band' AND SHOWS.Band='$Band'";
        //echo $SQL;
        $result=mysql_query($SQL) or die('A error occured: ' . mysql_error());
        while($row=mysql_fetch_assoc($result)) {
        
        $code=$row['EmbedCode'];
        $allbands=$row['Allbands'];
        $venue=$row['Venue'];
        $date=$row['Date'];
        echo "<font size =4><i>" . $allbands ." <font size=6>::<font size=4> " . $venue . "<font size=6> :: <font size=4>" . $date . "<br>";
        echo $code . "<br><br>";
    }
    }
}
 

Code: Select all

//build query
 
if (($selectedgenre) || ($selecteddate) || ($selectedvenue)) {
    $i = 0;
    foreach ($_POST['selectedgenre'] as $a => $b) {
        if ($i == 0) {
            $query .= "SELECT Band from SHOWS WHERE Genre LIKE '%$b%'";
            $i++;
        } else {
            $query .= " || Genre LIKE '%$b%'";
        }
    }
    
echo "postdata still active on page " . $page;
 
if($selecteddate) {
    $query.=" AND Date='$selecteddate'";
 
}
 
if($selectedvenue) {
 
    $query.=" AND Venue='$selectedvenue'";
 
}
    $Bands=array();
    $theresult = mysql_query($query) or die('A error occured: ' . mysql_error());
    while ($row = mysql_fetch_assoc($theresult)) {
            $Band=trim($row['Band']);
            array_push($Bands,$Band);
        }
                //print_r($Bands);
                echo "<br><br>";
 
    }
    
    
    
echo "page is " . $page . "<br><bR><br>";
 
showResults($page,$Bands,$numperpage);
 
showSearchForm();
 
print_r($Bands);
 
if($page>1) {
echo '</i><div id="backbutton" style="position:fixed; bottom:5px; left:5px;"><font size=6><a href="'.$_SERVER['PHP_SELF'].'?page='.($page-1).'"></i>.: Back';
}
echo '</i><div id="nextbutton" style="position:fixed; bottom:5px; right:5px;"><font size=6><a href="'.$_SERVER['PHP_SELF'].'?page='.($page+1).'"> Next :.';
 
 

The postdata comes from an index page and is submitted to search.php (which is where the code above comes from). How do I pass the results array to the next page??? I'm sure this code looks like spaghetti to all you leet PHPers out there, but any help you can provide would be much appreciated!!

Thanks!

Nick
'
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Paging search results

Post by Eric! »

wandergeek wrote:The postdata comes from an index page and is submitted to search.php (which is where the code above comes from). How do I pass the results array to the next page??? I'm sure this code looks like spaghetti to all you leet PHPers out there, but any help you can provide would be much appreciated!!
Sorry if I'm missing your point. But I think this is what you're asking about...You can $_POST['results']=$rows the results array to the next page. Then paginate them.
User avatar
emix
Forum Newbie
Posts: 8
Joined: Mon Jun 22, 2009 10:32 am
Location: Poland

Re: Paging search results

Post by emix »

HTTP is a stateless protocol, each session is different.
You should pass page number within the url and fetch/recalculate the data again.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Paging search results

Post by jackpf »

So wait...your post data isn't travelling across pages?

If you want it to do so you'll actually have to send it via javascript or a form.
wandergeek
Forum Newbie
Posts: 3
Joined: Fri Jul 03, 2009 1:29 pm

Re: Paging search results

Post by wandergeek »

I finally figured out a solution for this. I needed to store my results in a session variable and pass the session ID via my next button like so:

Code: Select all

 
session_start(); 
 
...Parse, build query, return results....
 
$_SESSION['results'] = $Bands;
$Bands=$_SESSION['results'];
 
Buttons
if($page>1) {
echo '</i><div id="backbutton" style="position:fixed; bottom:5px; left:5px;"><font size=6><a href="'.$_SERVER['PHP_SELF'].'?'.session_id().'&page='.($page-1).'"></i>.: Back';
}
echo '</i><div id="nextbutton" style="position:fixed; bottom:5px; right:5px;"><font size=6><a href="'.$_SERVER['PHP_SELF'].'?'.session_id().'&page='.($page+1).'"></i>Next :.';
 
 
 
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Paging search results

Post by Eric! »

Yes either a session variable OR post it again.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Paging search results

Post by jackpf »

Wouldn't it be better to paginate the results in the query, using LIMIT?

I thought that was the whole point of pagination - to speed up the query when potentially fetching a lot of rows.
wandergeek
Forum Newbie
Posts: 3
Joined: Fri Jul 03, 2009 1:29 pm

Re: Paging search results

Post by wandergeek »

Yeah, that's not a bad idea, actually-- I'll give it a shot. Thanks!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Paging search results

Post by jackpf »

Lol no problem.

If you get stuck, I'll be happy to help.
Post Reply