Page 1 of 1

simple pagination problem...

Posted: Tue Nov 17, 2009 8:30 am
by psaha
While searching the URL displays http://mysite.com/search.php?q=Hello+World
and I have the relevant output with pagination. That is good news. But the bad news is
When I am going to the next page It should hold the URL with search item.
Example:
The next page URL will be http://mysite.com/search.php?[u]q=Hello+World[/u]&page=2
And This is same for prev page also.
But It could not hold the searched value.
I have tried $PHP_SELF but it is not working
My PHP code chunk looks like:

Code: Select all

echo '<a href="&page=',($page-1),'"><img src="previous.gif"></a>' ;
        if($page<$totalpages)
     echo ' <a href="'&page=',($page+1),'"><img src="next.gif"></a>';
It displays http://mysite.com/&page=2 or http://mysite.com/search.php&page=2
But I need http://mysite.com/search.php?q=[b]Hello+World[/b] the searched item....

Please help..

Re: simple pagination problem...

Posted: Tue Nov 17, 2009 8:33 am
by iankent
You just need to add in the value of $_GET['q'] to the new URL, e.g.

Code: Select all

echo '<a href="?q={$_GET['q']}&page=',($page-1),'"><img src="previous.gif"></a>' ;
if($page<$totalpages)
echo ' <a href="'?q={$_GET['q']}&page=',($page+1),'"><img src="next.gif"></a>';
edit:
or to make it a bit easier to read :)

Code: Select all

 
$q = isset($_GET['q']) ? $_GET['q'] : '';
$p = isset($_GET['p']) ? $_GET['page'] : 1;
if($p > 1) { ?>
    <a href="?q=<?=$q?>&page=<?=$p-1?>"><img src="previous.gif"></a>
<? }
if($p < $totalpages) { ?>
    <a href="?q=<?=$q?>&page=<?=$p+1?>"><img src="next.gif"></a>
<? }
 

Re: simple pagination problem...

Posted: Tue Nov 17, 2009 8:37 am
by psaha
Thank you.. It was very simple.. I forgot it..
Thanks again... :)

Re: simple pagination problem...

Posted: Wed Dec 02, 2009 9:30 pm
by archies
Hi hi..
Referring to your thread, i am currently facing a problem which i think you might be able to help.
I can see that your pagination script is using a post method to pass a search query whereby the results will then split into pages.

Currently my script can only view the whole database results and when i pass my search query, only the 1st page is ok, subsequently on the 2nd or 3rd pages, it doesnt work.

Here's my script: hope you can assist me.
Lot of thanks :)

Code: Select all

<?php
include "header.php";
?>
 
<?php
 
$val_d = $_POST['devicesearch'];
if ($_POST['SEARCH'] = 'Device Search')
{
    $sql = "SELECT *
            FROM device
            WHERE device_num LIKE '%$val_d%' or '%$val_d'";
             
    $results1 = mysql_query($sql) or die (mysql_error());   
        
            $pagenum = $_GET["pagenum"];
            echo "<center>";
            //This checks to see if there is a page number. If not, it will set it to page 1 
            if (!(isset($pagenum))) 
            { 
            $pagenum = 1; 
            } 
 
    $numrows = mysql_num_rows($results1);       
    $page_rows = 10; //This is the number of results displayed per page 
    $last = ceil($numrows/$page_rows); //This tells us the page number of our last page 
    if ($pagenum < 1) //this makes sure the page number isn't below one, or more than our maximum pages 
    { 
    $pagenum = 1; 
    } 
    elseif ($pagenum > $last) 
    { 
    $pagenum = $last; 
    } 
 
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; //This sets the range to display in our query 
                
    $sql = "SELECT *
            FROM device
            WHERE device_num LIKE '%$val_d%' or '%$val_d' $max"; 
    $results = mysql_query($sql) or die (mysql_error());
     
             // This is where the result will print out. 
    include "FTsearchtable.php";
    
    echo "<br><br><center>Page $pagenum of $last<br><br>"; // This shows the user what page they are on, and the total number of pages
 
    // First we check if we are on page one. 
    //If we are then we don't need a link to the previous page or the first page so we do nothing. 
    //If we aren't then we generate links to the first page, and to the previous page.
    if($pagenum>1)
    {
 
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'>First</a> ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'>Previous</a> ";
    } 
    //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
    if ($pagenum == $last) 
    {
    } 
    else 
    {
    $next = $pagenum+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next</a> ";
    echo " ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last</a> ";
    } 
    
    }
?>