SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination($_GET) and $_POST loading on one page

    I'm have a search field on a page. I also have pagination with the search results. I'm trying to get this to work by loading the same page($_SERVER['PHP_SELF']) instead of having the pagination results on a new page .

    My problem is to get the pagination to work correctly.

    Since I have a $search = $_POST['search'] for receiving the search there is a problem with the pagination for the second pagination page because I somehow need $search = $_GET['search'] when I click on the next links for pagination.

    I know I could avoid this by creating a new page that receives the linked pagination values

    I have a lot of code on this page but I'm displaying some here. Maybe you can have some input about this. Wether it can be done by using one page(search.php) or if I should just create a second page for the pagination results(ie search2.php)

    search.php page

    POSTING FORM
    <form action="search.php" method="post">
    <input name="search" type="text" />
    <input type="submit" value="Find" />

    GETTING SEARCH RESULST INCLUDING PAGINATION

    PHP Code:

    //Getting posted search value
    $search clean($_POST['search']); 



    //***PAGINATION***


    //Getting the pagenumber value from the links
    $pagenum $_GET['pagenum'];



    //PAGINATION INITIATE_____________

    if (!(isset($pagenum))) 

    $pagenum 1



    //Here we count the number of results 

    //Edit $data to be your query 
    $data mysql_query("SELECT * FROM members WHERE studio_name LIKE '%".$search."%'") or die(mysql_error()); 
    $rows mysql_num_rows($data); 

    //This is the number of results displayed per page 
    $page_rows 25

    //This tells us the page number of our last page 
    $last ceil($rows/$page_rows); 

    //this makes sure the page number isn't below one, or more than our maximum pages 
    if ($pagenum 1

    $pagenum 1

    elseif (
    $pagenum $last

    $pagenum $last


    $counting = ($page_rows $pagenum) - ($page_rows) + 1;

    //This sets the range to display in our query 
    $max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows;



    //SELECT MEMBERS AND GET RESULTS
    $QUERY mysql_query("SELECT * FROM members WHERE studio_name LIKE '%".$search."%' ORDER BY studio_name $max");
    $NUMROWS = @mysql_num_rows($QUERY);

    if (!
    $NUMROWS) {

    echo 
    "No results found";

    } else {

    $I 0;
    while (
    $I $NUMROWS) {

    $member_id mysql_result($QUERY,$I,"member_id");
    $studio_name mysql_result($QUERY,$I,"studio_name");


    echo 
    $studio_name;

    $I++;

    }


    PAGINATION LINKS

    PHP Code:

    // 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
    {
    //nothing here
    } else {

    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=1&studio_name=$search'><img src='images/buttonfirst.png' border='0' title='First'</a> ";
    echo 
    " ";
    $previous $pagenum-1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&studio_name=$search'><img src='images/buttonprevious.png' border='0' title='Previous'></a>&nbsp;&nbsp; ";


    //just a spacer
    echo " ";

    //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&studio_name=$search'><img src='images/buttonnext.png' border='0' title='Next'></a> ";
    echo 
    " ";
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&studio_name=$search'><img src='images/buttonlast.png' border='0' title='Last'></a>&nbsp;&nbsp; ";


  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    //Getting posted search value
    $search clean($_REQUEST['search']); 
    $_REQUEST is a combination of both GET and POST.

    Or:

    PHP Code:
    //Getting posted search value
    $search clean($_GET['search']); 
    if(isset(
    $_POST['search'])) {
        
    $search clean($_POST['search']); 

    This will grab the searchterm from _GET and will override it if there's a value in _POST.

    Please note that both versions do not take the possibility into account that both _GET['search'] and _POST['search'] are empty: you should really check for that too.

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried both $_REQUEST and the other option you gave me, but they did not work.

    Maybe it's something else in the code that has a problem?

    When I submit a search I get the correct results on the first page(ie 77 results), and when I hover over the pagination links they say the correct address(ie search.php?pagenum=2&studio_name=mystudio).

    But when I click to go to the second pagination page I get all results from the field(ie 200) and when I hover over the links now the value is not there(ie search.php?pagenum=2&studio_name=) .

    Do you think there is something else in the code that is not correct?

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    You might need to change the name of the field 'studio_name' in the pagination links to 'search'.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome, That did the trick.

    Thanks for your help!


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
  •