SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination - next page?

    Hi Guys,

    Having problems with a script i've built that works on a pagination basis:

    if you kindly have a look at:

    http://www.slougheaz.org/directory/n...rch/search.php

    and search for an: accountant

    you will see that the search returns results...BUT

    When you click on 'next page' the system forgets what I searched for and returns all results in the database. Can anoyone please advise as I get confused by pagination in php?

    PHP Code:
    <html> 
    <head> 
    <title>Search Results ...</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    <link rel="stylesheet" href="ssheet.css"> 
    </head> 

    <body> 
    <p>Search Results</p>
    <?php 

    // Start the connection to the database 
             
            
    include('****************');
            
    $can_i_connect db_connect(); // by db_connect function is in my include file 
            
    if(!$can_i_connect
            { 
                echo 
    "Could not connect to database"
            } 

    // End the connection to the database     

    // Start to get the data from the form and trim any whitespace 
             
            
    if($_SERVER["REQUEST_METHOD"]=='POST'
            { 
                
    $business_type trim($_POST['business_type']); 
                
    $town trim($_POST['town']); 
                
    $company_name trim($_POST['company_name']); 
            } 
               else 
            { 
                
    $business_type trim($_GET['business_type']); 
                
    $town trim($_GET['town']); 
                
    $company_name trim($_GET['company_name']); 
            } 

    // End getting the data from the form and trimming any whitespace 
             
             
    // Start to build the query and order the listings by the company name 

        
    $add ''

        
    $search_query "SELECT * from directory_listings where listing='business' AND verified='1' AND"

        if(
    $business_type == ''//Nothing entered 
        

            
    $add .= " business_type LIKE '%'"
        } 
        else 
        { 
            
    $add .= " business_type LIKE '%$business_type%'"
        } 
        if(!empty(
    $town)) 
        { 
            
    $add .= " AND town LIKE '%$town%'";  
        } 
        if(!empty(
    $company_name)) 
        { 
            
    $add .= " AND company_name LIKE '%$company_name%'"
        } 

    // End building the query and order the listings by the company name 


    // Start pagination script and state amount of records per page 

        
    $limit 5
        
    $query_count mysql_query "SELECT COUNT(*) AS total FROM directory_listings WHERE " $add ); 
        
    $result_count mysql_fetch_assoc $query_count ); 
        
    $totalrows $result_count['total']; 
        
    $PHP_SELF $_SERVER['PHP_SELF']; 

        if( ! isset ( 
    $_GET['page'] ) ) 
        { 
            
    $page 1
        } 
        else 
        { 
            
    $page $_GET['page']; 
        } 

        
    $limitvalue $page $limit - ($limit);    

    // End pagination script and state amount of records per page 


    // Start to find how many search results are being found for the query 

            
    $search_query .= $add " LIMIT " $limitvalue ", " $limit
            
    $search_results mysql_query($search_query$can_i_connect); 
            
    $result mysql_query($search_query) or die (mysql_error()); 

    // Figure out the total number of results in DB: 
    $total_results mysql_result(mysql_query("SELECT FOUND_ROWS()"), 0); 
             
            if(
    $total_results <= 0
            { 
                echo 
    "Sorry, there were no results for your search."
            } 

    // Else and Start to find how many pagination pages I have 
            
    else 
            {     
                echo 
    "Your search returned ".$totalrows." result(s). <br /><br />Here are those results, listed in ascendng order. <br /><br />"

    if(
    $page != 1){    
            
    $pageprev $page 1
              
               
            echo(
    "<a href=\"$PHP_SELF?page=$pageprev\"><img src=\"results/previous.jpg\" width=\"120\" height=\"22\" class=\"prev_next_border\"></a> ");    
        }else{ 
            echo(
    ""); 
        } 
       
        
    $numofpages $number_of_results$limit
          
        
    #echo "<br>", $totalrows; 
       #exit; 
           
        
    for($i 1$i <= $numofpages$i++){ 
            if(
    $i == $page){ 
                echo(
    $i." "); 
            }else{ 
                echo(
    "<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
            } 
        } 

        if((
    $totalrows - ($limit $page)) > 0){ 
            
    $pagenext $page 1
                
            echo(
    "<a href=\"$PHP_SELF?page=$pagenext\"><img src=\"results/next.jpg\" width=\"120\" height=\"22\" class=\"prev_next_border\"></a>");    
        }else{ 
            echo(
    "");    
        } 

            }   
    // End of how many results I have found 
           
        
    mysql_free_result($result); 


    // End of Else and to find how many pagination pages I have 
                                                      

    ?> 

    <?while ($obj=mysql_fetch_object($search_results)){?> 
    <table width='100%' cellpadding="1" cellspacing="1" bgcolor="#CCCCCC"> 
      <tr bgcolor="#EEEEEE"> 
        <td height="50%" width="50%"><?echo $obj->business_name?></td><td height="50%" width="50%"><?echo $obj->business_type?></td></tr> 
      <tr bgcolor="#EEEEEE"> 
        <td height="50%" width="50%"><?echo $obj->town?></td><td height="50%" width="50%"><a href="full_details.php?business_id=<?echo $obj->business_id?>"><img src="../images/more.jpg" width="80" height="19" alt="More..."></a></td></tr> 
    </table><br /><?}?> 
    </body> 
    </html>

  2. #2
    SitePoint Member daxz's Avatar
    Join Date
    Mar 2007
    Location
    UK
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wat u need to do is put what the user searched for in the URL, at the moment it is just the page number e.g :

    http://www.slougheaz.org/directory/n...lts.php?page=2

    so it pulls out page 2 out the database.

    instead u need to do something like this :

    http://www.slougheaz.org/directory/n...h/results.php?business_type=accountant&page=2

    try the above URL it should work.


    so basically where u construct ur URL in ur php, change it to look at what the users has entered,


    e.g.

    "<a href=\"$PHP_SELF?compnay="$whatever"&page=$pageprev\"

    let me know if i aint been clear enough.
    Last edited by daxz; Mar 19, 2007 at 11:25.
    Dark Wing Chun Project
    www.darkwingchun.com

  3. #3
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for your help.

    I think I understand, but would that not be the case for GET when I am using POST?

    Thanks

    Chris

  4. #4
    SitePoint Member daxz's Avatar
    Join Date
    Mar 2007
    Location
    UK
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    both get and post pass variables on to the next page.

    so it does not matter what method u use on ur search page, it is how u contruct ur URL on the pagination page.

    so really u all u should need to do is something like this

    <a href=\"$PHP_SELF?BusinessType=$business_type&page=$i\>

    this URL is only part complete, so u would need to add all ur other variables in there.
    Dark Wing Chun Project
    www.darkwingchun.com

  5. #5
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, OK then cool. I understand now then:

    PHP Code:
    echo("<a href=\"$PHP_SELF?business_type=$business_type&town=$town&company_name=$company_name&page=$pageprev\"> 
    Thanks very much!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •