Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
  1. #1
    Senior Member beachkitty85's Avatar
    Join Date
    Feb 2007
    Location
    Apex, NC
    Posts
    131
    Member #
    14894
    I have a PHP search script that works fine, except when I try to integrate pagination. Whenever I click the page links, all form data is lost and there are no search results. Can anyone help?

    I've also attached the two PHP files. Sidebar.php contains the form and Search.php performs the action.

    Here's my form:

    Code:
    <form name="search" method="post" action="search.php">
    <input type="hidden" name="searching" value="yes" class="hidden" />
    <input type="hidden" name="field" value="<?php echo $field;?>" class="hidden" />
    <input type="hidden" name="find" value="<?php echo $find;?>" class="hidden" />
    <input type="text" name="find" value="Search..." checked="checked" maxlength="35" onblur="if(this.value=='') this.value='Search...';" onfocus="if(this.value=='Search...') this.value='';" />
      <select name="field" value="field">
        <option name="client_firstname" value="client_firstname">First Name</option>
        <option name="client_lastname" value="client_lastname">Last Name</option>
        <option name="id" value="id">Invoice No.</option>
        <option name="total" value="total">Total</option>
        <option name="website" value="website">Website</option>
        <option name="status" value="status">Status</option>
      </select>
    <input type="submit" name="search" value="Submit" />
    </form>
    And here's search.php, the page the form submits to:

    Code:
    <?php
     
    //if in search mode
    
    if (($searching == "yes") and ($find !== "")) {
    
    // prepare inputs
    $find = $_POST['find'];
    $field = $_POST['field'];
    
    // We perform a bit of filtering
    $find = strtoupper($find);
    $find = strip_tags($find);
    $find = trim ($find);
    
    // simple query to get total no of entries
    $result = mysql_query("SELECT COUNT(*) FROM epay_invoices JOIN epay_clients ON epay_clients.clientid = epay_invoices.clientid WHERE $field LIKE '%$find%'"); 
    $total = mysql_result($result, 0, 0); 
    
    // work out the pager values 
    $pager  = getPagerData($total, $limit, $page); 
    $offset = $pager->offset; 
    $limit  = $pager->limit; 
    $page   = $pager->page;     
    
    // use pager values to fetch data 
    $query = "SELECT * FROM epay_invoices JOIN epay_clients ON epay_clients.clientid = epay_invoices.clientid WHERE $field LIKE '%$find%' ORDER BY $param LIMIT $offset, $limit"; 
        if (!$param) {
        $query = "SELECT * FROM epay_invoices JOIN epay_clients ON epay_clients.clientid = epay_invoices.clientid WHERE $field LIKE '%$find%' ORDER BY id LIMIT $offset, $limit";
        }
    
    } 
    
    $result = mysql_query($query);
    if ($result > 0) {
            
    echo "<table class=\"invoice-list\">";
        echo "<tr>";
            echo "<th><a href=\"".$PHP_SELF."?param=id&amp;page=$page\" title=\"".TEXT_INVOICE_LIST_INVOICE_NO."\"><img src=\"".CONFIG_ICON_DIR."invoice.png\" alt=\"".TEXT_INVOICE_LIST_INVOICE_NO."\" title=\"".TEXT_INVOICE_LIST_INVOICE_NO."\" /></a></th>";
            echo "<th><a href=\"".$PHP_SELF."?param=client_lastname&amp;page=$page\" title=\"".TEXT_INVOICE_LIST_CLIENT_NAME."\"><img src=\"".CONFIG_ICON_DIR."client.png\" alt=\"".TEXT_INVOICE_LIST_CLIENT_NAME."\" title=\"".TEXT_INVOICE_LIST_CLIENT_NAME."\" /></a></th>";
            echo "<th><a href=\"".$PHP_SELF."?param=date&amp;page=$page\" title=\"".TEXT_INVOICE_LIST_INVOICE_DATE."\"><img src=\"".CONFIG_ICON_DIR."date.png\" alt=\"".TEXT_INVOICE_LIST_INVOICE_DATE."\" title=\"".TEXT_INVOICE_LIST_INVOICE_DATE."\" /></a></th>";
            echo "<th><a href=\"".$PHP_SELF."?param=total&amp;page=$page\" title=\"".TEXT_INVOICE_LIST_INVOICE_TOTAL."\"><img src=\"".CONFIG_ICON_DIR."total.png\" alt=\"".TEXT_INVOICE_LIST_INVOICE_TOTAL."\" title=\"".TEXT_INVOICE_LIST_INVOICE_TOTAL."\" /></a></th>";
            echo "<th><a href=\"".$PHP_SELF."?param=status&amp;page=$page\" title=\"".TEXT_INVOICE_LIST_INVOICE_STATUS."\"><img src=\"".CONFIG_ICON_DIR."status.png\" alt=\"".TEXT_INVOICE_LIST_INVOICE_STATUS."\" title=\"".TEXT_INVOICE_LIST_INVOICE_STATUS."\" /></a></th>";
            echo "<th>&nbsp;</th>";
            echo "<th>&nbsp;</th>";
            echo "<th>&nbsp;</th>";
            echo "<th>&nbsp;</th>";
        echo "</tr>";
    
    $num_classes = 2;
    $i = 0;
    while ($row = mysql_fetch_array($result))
    {
        $id = $row["id"];
        $clientid = $row["clientid"];
        $client_firstname = $row["client_firstname"];
        $client_lastname = $row["client_lastname"];
        $date = fixDate($row["date"]);
        $total = number_format($row["total"], 2, '.', '');
        $status = $row["status"];
    
        echo "<tr class=\"row_".$i++%$num_classes."\">";
        echo "<td>$id</td>";
        echo "<td>$client_firstname   $client_lastname</td>";
        echo "<td>$date</td>";
        echo "<td>$$total</td>";
        echo "<td>$status</td>";
        echo "<td><a href=\"invoice.php?id=$id\" title=\"".TEXT_INVOICE_LIST_VIEW."\"><img src=\"".CONFIG_ICON_DIR."view.png\" alt=\"".TEXT_INVOICE_LIST_VIEW."\" title=\"".TEXT_INVOICE_LIST_VIEW."\" /></a></td>";
        echo "<td><a href=\"client_notify.php?id=$id\" title=\"".TEXT_INVOICE_LIST_EMAIL."\"><img src=\"".CONFIG_ICON_DIR."send_email.png\" alt=\"".TEXT_INVOICE_LIST_EMAIL."\" title=\"".TEXT_INVOICE_LIST_EMAIL."\" /></a></td>";
        echo "<td><a href=\"invoice_edit.php?id=$id\" title=\"".TEXT_INVOICE_LIST_EDIT."\"><img src=\"".CONFIG_ICON_DIR."edit.png\" alt=\"".TEXT_INVOICE_LIST_EDIT."\" title=\"".TEXT_INVOICE_LIST_EDIT."\" /></a></td>";
        echo "<td><a href=\"invoice_delete.php?id=$id\" onclick=\"return confirm('".TEXT_INVOICE_LIST_DELETE_CONFIRM."".$id."?')\" title=\"Delete Invoice\"><img src=\"".CONFIG_ICON_DIR."delete.png\" alt=\"Delete Invoice\" title=\"Delete Invoice\" /></a></td>";
        echo "</tr>";
        }
    echo "</table>";
    
    ?>
    
    <div class="pagination">
    
    <?php
        if ($page == 1) // this is the first page - there is no previous page 
            ; 
        else            // not the first page, link to the previous page 
        
            echo "<span class=\"pagination-prev-symbol\"><a href=\"".$PHP_SELF."?param=$param&amp;page=" . ($page - 1) . "\">".CONFIG_PAGINATION_PREV."</a></span>"; 
    
        for ($i = 1; $i <= $pager->numPages; $i++) { 
            echo ""; 
            if ($i == $pager->page) 
                echo "<span class=\"pagination-active\">".CONFIG_PAGINATION_PAGE."$i</span>"; 
            else 
                echo "<span class=\"pagination-inactive\"><a href=\"".$PHP_SELF."?param=$param&amp;page=$i\">".CONFIG_PAGINATION_PAGE."$i</a></span>"; 
        } 
    
        if ($page == $pager->numPages) // this is the last page - there is no next page 
            ; 
        else            // not the last page, link to the next page 
            echo "<span class=\"pagination-next-symbol\"><a href=\"".$PHP_SELF."?param=$param&amp;page=" . ($page + 1) . "\">".CONFIG_PAGINATION_NEXT."</a></span>"; 
    ?>
    
    </div>
    
    <?php 
    } else {
    
    echo TEXT_SEARCH_NO_MATCHES;
    
    }
    ?>
    Christina Van Dyke

    CRV Designs
    Apex, NC
    www.crv-designs.com

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    You're just going to have to pass the form values originally submitted to that page back to itself with the pagination links. The other way you'll see it done is to store the search results in one way or another (session, database, whatever) and then paginate over those given a search id.

  4. #3
    Senior Member beachkitty85's Avatar
    Join Date
    Feb 2007
    Location
    Apex, NC
    Posts
    131
    Member #
    14894
    I registered the search variables as suggested, but the pagination links still return "There are no invoices that match your search", even though I know there are more results. The only page that displays results is the first page. My hidden form fields contain the search variables after registering them now, where they were empty before. Any ideas?

    Here is the code I added to search.php:

    Code:
    //register search variables
    session_start();
    
    session_register("field");
    session_register("find");
    
    $find = $HTTP_POST_VARS['find'];
    $field = $HTTP_POST_VARS['field'];
    blah blah blah

    Code:
    session_unregister ("find"); 
    session_unregister ("field");
    Christina Van Dyke

    CRV Designs
    Apex, NC
    www.crv-designs.com

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Hmm... Maybe the unregistering happens at the wrong time, e.g., before things have a chance to pass along. Try to get rid of the unregistering?

  6. #5
    Senior Member beachkitty85's Avatar
    Join Date
    Feb 2007
    Location
    Apex, NC
    Posts
    131
    Member #
    14894
    I tested more thoroughly this time and got different results.

    Using session_unregister causes the variables not to show up on the next page.

    Commenting out session_unregister causes old form data to remain in the hidden form fields. For example, if my last search was for balloons and this search is for cats, I would still see balloons in my hidden fields.

    I think I have session_unregister in the wrong spot. I've attached the full search.php with the mods I've made so maybe someone can tell me where it should go.
    Christina Van Dyke

    CRV Designs
    Apex, NC
    www.crv-designs.com

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Which version of PHP are you using? I'm asking because it seems like the $_SESSION superglobal would be easier than all this registering and unregistering stuff. Not the least because I don't know for 100% sure how those work :-P

  8. #7
    Member
    Join Date
    Apr 2007
    Posts
    96
    Member #
    15165
    Quote Originally Posted by beachkitty85
    I think I have session_unregister in the wrong spot. I've attached the full search.php with the mods I've made so maybe someone can tell me where it should go.
    Just from glancing at the file. I wonder if you just need to move your session_register to the top of the page (above where you have your hidden fields).

    PHP Code:
    <?php
     
    //if in search mode
    if (($searching == "yes") and ($find !== "")) {

    //start search session
    session_start();

    //register search variables
    session_register("field");
    session_register("find");

    //pull form data
    $find $HTTP_POST_VARS['find'];
    $field $HTTP_POST_VARS['field'];

  9. #8
    Senior Member beachkitty85's Avatar
    Join Date
    Feb 2007
    Location
    Apex, NC
    Posts
    131
    Member #
    14894
    I moved this to the top of the page:
    Code:
    <?php
    
    //start search session
    session_start();
    
    //register search variables
    session_register("field");
    session_register("find");
    
    //pull form data
    $find = $HTTP_POST_VARS['find'];
    $field = $HTTP_POST_VARS['field'];
    
    ?>
    But it still won't work. I also tried using $_SESSION with no luck. If I see " There are no invoices that match your search." one more time I'm going to throw my computer out the window!
    Christina Van Dyke

    CRV Designs
    Apex, NC
    www.crv-designs.com

  10. #9
    Member
    Join Date
    Apr 2007
    Posts
    96
    Member #
    15165
    Quote Originally Posted by beachkitty85
    I moved this to the top of the page:
    Code:
    //pull form data
    $find = $HTTP_POST_VARS['find'];
    $field = $HTTP_POST_VARS['field'];
    
    ?>
    But it still won't work. I also tried using $_SESSION with no luck. If I see " There are no invoices that match your search." one more time I'm going to throw my computer out the window!
    I wish I could work with a working copy of the file, but from looking at it again...
    Can you try adding

    $page = $HTTP_GET_VARS['page'];

    as I am thinking you have register_globals turned off on the php you are running.

  11. #10
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    And while you're at it, use $_GET and $_POST, too, as the other globals are deprecated... I should be able to have a more thorough look at it in a little while, at which point I'll try and see what I can figure out.

    Don't throw your computer out the window, they tend to cost money


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

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

Search tags for this page

pagination from search query php

Click on a term to search for related topics.
All times are GMT -6. The time now is 05:54 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com