Current location: Hot Scripts Forums » Programming Languages » PHP » Showing Search Form Results in Pagination


Showing Search Form Results in Pagination

Reply
  #1 (permalink)  
Old 03-22-08, 05:54 PM
macintosh macintosh is offline
Newbie Coder
 
Join Date: Mar 2007
Posts: 84
Thanks: 1
Thanked 0 Times in 0 Posts
Showing Search Form Results in Pagination

Hey..
I want to display the mysql results when someone uses the search form. Now im trying by this way


PHP Code:

if($_POST['search']){

$search = $_POST['searchBox'];

... and so on.  Here I want to display the results in Pagination.
}


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<span style="vertical-align: middle; font-weight: bold;">Find:&nbsp;</span>
<input name="searchBox" class="searchBox" type="text" style="width: 433px" >
 <input type="submit" name="search" value="Search">
</form>
The problem i am facing is that, when i search something. It showing the paging nav.
but when i click on 2nd page or so on, the paging nav. disappears.
I think this is because i doing everything inside if $_POST condition.
If someone can suggest something about how to display search results in pagination.
Thank you very much for always help
Reply With Quote
  #2 (permalink)  
Old 03-22-08, 09:12 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You will need to post your code. This one form you are showing doesn't help much.
__________________
Jerry Broughton
Reply With Quote
  #3 (permalink)  
Old 03-23-08, 01:22 PM
macintosh macintosh is offline
Newbie Coder
 
Join Date: Mar 2007
Posts: 84
Thanks: 1
Thanked 0 Times in 0 Posts
Here is complete code.. please have a look at it.
PHP Code:

<?php


if($_POST['search']){
$search $_POST['searchBox'];

... and 
so on.  Here I want to display the results in Pagination.
$search trim($search);
//Explode into seperate keywords and make all unique
$terms array_unique(explode(' ',$search));

//Loop through all terms, and add text for query
foreach($terms as $k=>$v)
{
    
//Check for empty values
    
if($v=="") continue;
    
$terms[$k]="`tags` LIKE '%$v%'";
}

//Implode each term back together with the OR in between them
$newsearch implode(' OR '$terms);


$tbl_name="";        //your table name
    // How many adjacent pages should be shown on each side?
    
$adjacents 3;
    
    
/* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    
$query "SELECT COUNT(*) as id FROM upload";
    
$total_pages mysql_fetch_array(mysql_query($query));
    
$total_pages $total_pages[id];
    
    
/* Setup vars for query. */
    //$targetpage = "test.php";     //your file name  (the name of this file)
    
$limit 1;                                 //how many items to show per page
    
$page $_GET['page'];
    if(
$page
        
$start = ($page 1) * $limit;             //first item to display on this page
    
else
        
$start 0;                                //if no page var is given, set start to 0
    
    /* Get data. */
    
$sql "SELECT * FROM upload ORDER BY id DESC LIMIT $start$limit";
    
$result mysql_query($sql);
    
    
/* Setup page vars for display. */
    
if ($page == 0$page 1;                    //if no page var is given, default to 1.
    
$prev $page 1;                            //previous page is page - 1
    
$next $page 1;                            //next page is page + 1
    
$lastpage ceil($total_pages/$limit);        //lastpage is = total pages / items per page, rounded up.
    
$lpm1 $lastpage 1;                        //last page minus 1
    
    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    
$pagination "";
    if(
$lastpage 1)
    {    
        
$pagination .= "<div class=\"pagination\">";
        
//previous button
        
if ($page 1
            
$pagination.= "<a href=\"$targetpage?page=$prev\">&laquo; previous</a>";
        else
            
$pagination.= "<span class=\"disabled\">&laquo; previous</span>";    
        
        
//pages    
        
if ($lastpage + ($adjacents 2))    //not enough pages to bother breaking it up
        
{    
            for (
$counter 1$counter <= $lastpage$counter++)
            {
                if (
$counter == $page)
                    
$pagination.= "<span class=\"current\">$counter</span>";
                else
                    
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
            }
        }
        elseif(
$lastpage + ($adjacents 2))    //enough pages to hide some
        
{
            
//close to beginning; only hide later pages
            
if($page + ($adjacents 2))        
            {
                for (
$counter 1$counter + ($adjacents 2); $counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                
$pagination.= "...";
                
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            
//in middle; hide some front and some back
            
elseif($lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
            {
                
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                
$pagination.= "...";
                for (
$counter $page $adjacents$counter <= $page $adjacents$counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
                
$pagination.= "...";
                
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
            }
            
//close to end; only hide early pages
            
else
            {
                
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                
$pagination.= "...";
                for (
$counter $lastpage - (+ ($adjacents 2)); $counter <= $lastpage$counter++)
                {
                    if (
$counter == $page)
                        
$pagination.= "<span class=\"current\">$counter</span>";
                    else
                        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                    
                }
            }
        }
        
        
//next button
        
if ($page $counter 1
            
$pagination.= "<a href=\"$targetpage?page=$next\">next &raquo</a>";
        else
            
$pagination.= "<span class=\"disabled\">next &raquo</span>";
        
$pagination.= "</div>\n";        
    }
?>

    <?php
        
while($row mysql_fetch_array($result))
        {
        
?>
        <div class="thumb">
                        <div class="image">
                <a href="image.php?id=<?=$row[id];?>" target='_blank'><img src="thumbs/<?=$row[name];?>"></a>            </div>
                      <div class="stars" id="stars5152">
                <img src="r4.gif" alt="design" width="16">                    
            </div>
                        <div class="links">
                <a href="http://link" >Link</a>            </div>
        </div>    
        
    <?
        
// Your while loop here
    
        
}
        
        echo 
$pagination;
}


<
form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<
span style="vertical-align: middle; font-weight: bold;">Find:&nbsp;</span>
<
input name="searchBox" class="searchBox" type="text" style="width: 433px" >
 <
input type="submit" name="search" value="Search">
</
form>
The problem seems to be carrying the $_POST variable to the next pages..thats why it doesnt show the pagination nav. on pages except first. i wish someone could have a look at above code and see wheres something wrong.

Last edited by Nico; 03-23-08 at 01:24 PM. Reason: Merged
Reply With Quote
  #4 (permalink)  
Old 03-23-08, 04:09 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
One thing I have noticed is that the following are wrong
PHP Code:

<?=$row[id];?>
#and
<?=$row[name];?>

#They should be
<?=$row['id'];?>
<?=$row
['name'];?>
Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #5 (permalink)  
Old 03-24-08, 06:11 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
The problem is, the search form isn't being submitted when you use the navigation links. You will need to isolate the search form results from the pagination links. You could do this by storing the search form results in session variables.
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Showing search results in another frame Hans Bauer PHP 13 09-15-06 05:32 AM
Search form causing errors? jwh JavaScript 3 02-09-06 09:41 AM
search form with date search newbieasp24 ASP 1 10-17-04 02:52 PM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 07:39 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.