Page 1 of 1

Paging search results

Posted: Fri Jul 03, 2009 1:42 pm
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
'

Re: Paging search results

Posted: Fri Jul 03, 2009 2:04 pm
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.

Re: Paging search results

Posted: Fri Jul 03, 2009 2:36 pm
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.

Re: Paging search results

Posted: Fri Jul 03, 2009 3:48 pm
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.

Re: Paging search results

Posted: Sat Jul 04, 2009 10:23 am
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 :.';
 
 
 

Re: Paging search results

Posted: Sat Jul 04, 2009 1:50 pm
by Eric!
Yes either a session variable OR post it again.

Re: Paging search results

Posted: Sun Jul 05, 2009 3:52 am
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.

Re: Paging search results

Posted: Sun Jul 05, 2009 11:20 am
by wandergeek
Yeah, that's not a bad idea, actually-- I'll give it a shot. Thanks!

Re: Paging search results

Posted: Sun Jul 05, 2009 10:51 pm
by jackpf
Lol no problem.

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