Results 1 to 21 of 21
  1. #1
    Member Sharan's Avatar
    Join Date
    May 19th, 2006
    Location
    SW Missouri
    Posts
    54
    Combining pagination with $_POST
    I have a script that shows 25 results (limit I have set) when a name is entered into the form. The query works because I have tested it.
    Code:
    $query = $name;
    $smart_query = $query;
    $sql = "SELECT * FROM $table WHERE name LIKE '%$smart_query%' LIMIT 25 ";
    $result = mysql_query($sql);
    $error = mysql_error(); print $error;
    $row[Description]=substr($row[Description], 0, 250);
    while ($row = mysql_fetch_array($result))
    I have another script that works for pagination whether it is for all the categories or just specific ones and it works.
    Code:
    // how many rows to show per page
    $rowsPerPage = 10;
    
    // by default we show first page
    $pageNum = 1;
    
    // if $_GET['page'] defined, use it as page number
    if(isset($_GET['page']))
    {
    	$pageNum = $_GET['page'];
    }
    
    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;
    
    $query = "SELECT * FROM $table where merchantcategory like'%XXXX%'";
    		   
    $pagingQuery = "LIMIT $offset, $rowsPerPage";
    $result = mysql_query($query . $pagingQuery) or die('Error, query failed');
    
    $row[Description]=substr($row[Description], 0, 250);
    while ($row = mysql_fetch_array($result))
    	{
    		extract($row);
    		echo "<table align='center' width='80%' border='0' cellspacing='0' cellpadding='3'> \n";
    		echo "<tr>\n";
    		echo "<td valign='top'>\n
    
    <a href='$Link'> <img src='$Thumbnail' border=0> </a> \n <br> <b> Price: </b> $$Price\n </td> \n";
    		
    		echo "<td valign='top'>\n
    <a href=\"$row[Link]\" title=\"$row[Name]\" target=\"_blank\"><b>$row[Name]</A><br></b>$row[Description]... <a href=\"$row[Link]\" target=\"_blank\">For more information</A></TD>
    
    
    ";
    		
    		echo "</tr>\n";
    		echo "</table>\n";
    		echo "<hr size='1'>\n";
    	}
    
    // how many rows we have in database
    $result  = mysql_query($query) or die('Error, query failed');
    $numrows = mysql_num_rows($result);
    
    // how many pages we have when using paging?
    $maxPage = ceil($numrows/$rowsPerPage);
    
    $self = $_SERVER['PHP_SELF'];
    
    // creating 'previous' and 'next' link
    // plus 'first page' and 'last page' link
    
    // print 'previous' link only if we're not
    // on page one
    if ($pageNum > 1)
    {
    	$page = $pageNum - 1;
    	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
    
    	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
    }
    else
    {
    	$prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
    	$first = ' [First Page] '; // nor 'first page' link
    }
    
    // print 'next' link only if we're not
    // on the last page
    if ($pageNum < $maxPage)
    {
    	$page = $pageNum + 1;
    	$next = " <a href=\"$self?page=$page\">[Next]</a> ";
    
    	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    }
    else
    {
    	$next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    	$last = ' [Last Page] '; // nor 'last page' link
    }
    
    // print the page navigation link
    echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
    I am trying to combine the two so if someone enters a query/name and the results are greater than what I have set to return it will show more pages of that particular type item. However, in my limited knowledge I cannot get the query to work. This is how I have tried to combine the two scripts:
    Code:
    // how many rows to show per page
    $rowsPerPage = 10;
    
    // by default we show first page
    $pageNum = 1;
    
    // if $_GET['page'] defined, use it as page number
    if(isset($_GET['page']))
    {
    	$pageNum = $_GET['page'];
    }
    
    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;
    
    
    $query = $name;
    $smart_query = $query;
    $sql = "SELECT * FROM $table WHERE name LIKE '%$smart_query%' LIMIT $rowsPerPage ";
    
    
    $pagingQuery = "LIMIT $offset, $rowsPerPage";
    $result = mysql_query($sql . $pagingQuery) or die('Error, query failed');
    
    $row[Description]=substr($row[Description], 0, 250);
    while ($row = mysql_fetch_array($result))
    	{
    		extract($row);
    		echo "<table align='center' width='80%' border='0' cellspacing='0' cellpadding='3'> \n";
    		echo "<tr>\n";
    		echo "<td valign='top'>\n
    
    <a href='$Link'> <img src='$Thumbnail' border=0> </a> \n <br> <b> Price: </b> $$Price\n </td> \n";
    		
    		echo "<td valign='top'>\n
    <a href=\"$row[Link]\" title=\"$row[Name]\" target=\"_blank\"><b>$row[Name]</A><br></b>$row[Description]... <a href=\"$row[Link]\" target=\"_blank\">For more information</A></TD>
    
    ";
    		
    		echo "</tr>\n";
    		echo "</table>\n";
    		echo "<hr size='1'>\n";
    }
    
    // how many rows we have in database
    $result  = mysql_query($sql) or die('Error, query failed');
    $numrows = mysql_num_rows($result);
    
    // how many pages we have when using paging?
    $maxPage = ceil($numrows/$rowsPerPage);
    
    $self = $_SERVER['PHP_SELF'];
    
    // creating 'previous' and 'next' link
    // plus 'first page' and 'last page' link
    
    // print 'previous' link only if we're not
    // on page one
    if ($pageNum > 1)
    {
    	$page = $pageNum - 1;
    	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
    
    	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
    }
    else
    {
    	$prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
    	$first = ' [First Page] '; // nor 'first page' link
    }
    
    // print 'next' link only if we're not
    // on the last page
    if ($pageNum < $maxPage)
    {
    	$page = $pageNum + 1;
    	$next = " <a href=\"$self?page=$page\">[Next]</a> ";
    
    	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    }
    else
    {
    	$next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    	$last = ' [Last Page] '; // nor 'last page' link
    }
    
    // print the page navigation link
    echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
    Any suggestions?

    Sharan

  2. #2
    Member Sharan's Avatar
    Join Date
    May 19th, 2006
    Location
    SW Missouri
    Posts
    54
    When I change:
    Code:
    $sql = "SELECT * FROM $table WHERE name LIKE '%$smart_query%' LIMIT $rowsPerPage ";
    to:
    Code:
    $sql = "SELECT * FROM $table WHERE name LIKE '%$smart_query%' ";
    I get results to show, but if I click on next (for next page) I am taken back to the form page. Bottom line - I'm sure it is something in the pagination script links that isn't set up correctly.

    Sharan

  3. #3
    ABW Ambassador bettylou's Avatar
    Join Date
    December 27th, 2005
    Location
    Indiana
    Posts
    595
    Hi Sharan,

    I'm sorry, I'm not quite sure that I follow.

    Are you trying to show more results on the search page than on the regular category page? How many are you wanting to show?

  4. #4
    Member Sharan's Avatar
    Join Date
    May 19th, 2006
    Location
    SW Missouri
    Posts
    54
    BettyLou,

    I probably haven't been very clear, sorry. This is what I was wanting to do:
    1) a person comes to my affiliate site, say it's on widgets
    2) they enter blue in the search box and it returns 10 items, even though there are in reality 33
    3) so they then click on the "next page" and see 10 more blue widgets, next page for 10 more, etc until they have seen all the blue widgets offered by the merchant

    At the present I can get the first 10 but clicking on the next page link goes back to the page with the search box.

    If anyone has an idea I'd welcome it, otherwise I won't spend much time because I have lots of other areas to work on.

    Thanks,
    Sharan

  5. #5
    ABW Ambassador bettylou's Avatar
    Join Date
    December 27th, 2005
    Location
    Indiana
    Posts
    595
    Sharan,

    What is the url of the "next" link? Is is something like searchpage.php?page=2&keyword=searchquery or is it just searchpage.php?

    It sounds like the page variable might not be getting passed properly.

    What I do for my search results is create a temporary table. That way the customer can sort the results however they like and I can do other cool things with them. Temp tables are actually quite simple. Just create a temp table, find the search results, fill the temp table with them and display them as you please.

    In your case, create the temp table, fill it will search results with a SELECT query, and then within your pagination area -- the query would pull results from that table.

  6. #6
    Member
    Join Date
    July 13th, 2008
    Posts
    57
    Normally, the search results URL look like this:

    whateversite. com / search.php?q=whatever&page=n

    where n is the page number and q is the search term.

    the query for the page would be:

    select * from whatever_table where column_name like '%search_term%' limit n,10

    assuming you are showing 10 results per page, and n is the page no.

    in every page you can have a next and prev links like this:

    next: whateversite. com / search.php?q=whatever&page=n+1
    prev:whateversite. com / search.php?q=whatever&page= n-1

    (of course, there wont be prev link on the first page, and no next link on the last page, unless you want them "cyclic")

    Sharan: just hover your mouse pointer on the next link and check the URL. probably the page number variable is wrong, or not getting passed at all. If you want me to write the code, I can, let me know.

    bettylou: using temp tables is never a good idea.

  7. #7
    Member Sharan's Avatar
    Join Date
    May 19th, 2006
    Location
    SW Missouri
    Posts
    54
    bettylou and anabayan,

    Thanks for your replys. When I mouse over the "next" it shows:
    mysite.com/test/test/index.php?page=2
    and for the last page it shows:
    mysite.com/test/test/index.php?page=81
    I tried testing it on my local machine then uploaded it to a directory I set up for testing & experimenting with the same results.

    I just know enough php to be dangerous. What looks easy to someone who knows how takes me a while to get and just when I think I'm beginning to get it, well I find out I don't really.

    Any help would be appreciated - I just like to understand & learn how to do these things.

    Sharan

  8. #8
    ABW Veteran Mr. Sal's Avatar
    Join Date
    January 18th, 2005
    Posts
    6,795
    Quote Originally Posted by Sharan
    This is what I was wanting to do:
    1) a person comes to my affiliate site, say it's on widgets
    2) they enter blue in the search box and it returns 10 items, even though there are in reality 33
    3) so they then click on the "next page" and see 10 more blue widgets, next page for 10 more, etc until they have seen all the blue widgets offered by the merchant

    At the present I can get the first 10 but clicking on the next page link goes back to the page with the search box.

    If anyone has an idea I'd welcome it, otherwise I won't spend much time because I have lots of other areas to work on.

    Thanks,
    Sharan
    Sharan, I think you shoud have something like this on the search script:

    $Prev = ($start-$limit);
    $Next = ($start+$limit);

  9. #9
    Member Sharan's Avatar
    Join Date
    May 19th, 2006
    Location
    SW Missouri
    Posts
    54
    Mr Sal,

    I have this in the search script:
    Code:
    if ($pageNum > 1)
    {
    	$page = $pageNum - 1;
    	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
    
    	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
    }
    else
    {
    	$prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
    	$first = ' [First Page] '; // nor 'first page' link
    }
    
    // print 'next' link only if we're not
    // on the last page
    if ($pageNum < $maxPage)
    {
    	$page = $pageNum + 1;
    	$next = " <a href=\"$self?page=$page\">[Next]</a> ";
    
    	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    }
    else
    {
    	$next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    	$last = ' [Last Page] '; // nor 'last page' link
    }
    
    // print the page navigation link
    echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
    but, obviously I'm doing something wrong. I'm just so green it's a wonder I ever got the pagination to work right without the search part. I can get the search (without the pagination part) to work so I could just set the return result to 25 or so and go with that if I need to. I just like to understand and seeing it in action works best for me. I can read from php.net but I don't always understand what it means until I see an example, then I come closer to knowing how to get it to work.

    Sharan

  10. #10
    ABW Ambassador bettylou's Avatar
    Join Date
    December 27th, 2005
    Location
    Indiana
    Posts
    595
    Hi Sharan,

    Try adding your search variable in the links, so that this:

    PHP Code:
    $next " <a href=\"$self?page=$page\">[Next]</a> "
    would become this:

    PHP Code:
    $next " <a href=\"$self?page=$page&name=$name\">[Next]</a> "

    where the "&name=$name" part is passing the searched upon variable (you may have to rename these to match what you are using).

    This way you are carrying that variable from page to page.

    Let me know how it works for you.


    Oh, I am editing to add that you will need to do this for all the prev and next links.
    Last edited by bettylou; August 22nd, 2008 at 11:18 AM. Reason: added sentence

  11. #11
    Member Sharan's Avatar
    Join Date
    May 19th, 2006
    Location
    SW Missouri
    Posts
    54
    bettylou,

    I made the change to the "next" page, but unfortunately it had the same results and displayed the search page again.

    Thanks for the suggestion,
    Sharan

  12. #12
    ABW Ambassador bettylou's Avatar
    Join Date
    December 27th, 2005
    Location
    Indiana
    Posts
    595
    Sharan,

    What is the address showing now for the next link? Is the search term actually showing up?

  13. #13
    Member Sharan's Avatar
    Join Date
    May 19th, 2006
    Location
    SW Missouri
    Posts
    54
    bettylou,

    This is what shows up now in the url (changed actual name for mysite.com):
    but what it does is goes back to the search box page. I don't have anything else on the page except the search box, but the connection and everything is set correctly because it actually shows 10 products with the pagination links at the bottom of the page which displays the results of the search.

    Sharan

  14. #14
    ABW Ambassador bettylou's Avatar
    Join Date
    December 27th, 2005
    Location
    Indiana
    Posts
    595
    Is the pagination contained within a function?

    I have used a similar script and it is contained within a function and you had to request the variables again from within the function. I don't think this is the case for you but give it a shot and I will keep looking.

  15. #15
    Member Sharan's Avatar
    Join Date
    May 19th, 2006
    Location
    SW Missouri
    Posts
    54
    bettylou,

    I am posting my whole script (without comments to conserve a little space). You can see where I have made the change you suggested in only one place. Maybe this will give you a better picture.

    Code:
    <?php
    
    if ($_POST) {
    	$name = strip_tags($_POST['name']);
    
      $username="XXX";
      $password="XXX";
      $table="XXX";
      $database="XXX";
      $connection = mysql_connect(localhost,$username,$password);
      @mysql_select_db($database) or die( "Unable to select database");
    
      $rowsPerPage = 10;
      $pageNum = 1;
    
    if(isset($_GET['page']))
      {
        $pageNum = $_GET['page'];
      }
    
    $offset = ($pageNum - 1) * $rowsPerPage;
    
    $query = $name;
    $smart_query = $query;
    $sql = "SELECT * FROM $table WHERE name LIKE '%$smart_query%' ";
    
    $pagingQuery = "LIMIT $offset, $rowsPerPage";
    $result = mysql_query($sql . $pagingQuery) or die('Error, query failed');
    
    $row[Description]=substr($row[Description], 0, 250);
    while ($row = mysql_fetch_array($result))
    	{
    		extract($row);
    		echo "<table align='center' width='80%' border='0' cellspacing='0' cellpadding='3'> \n";
    		echo "<tr>\n";
    		echo "<td valign='top'>\n
    
    <a href='$Link'> <img src='$Thumbnail' border=0> </a> \n <br> <b> Price: </b> $$Price\n </td> \n";
    		
    		echo "<td valign='top'>\n
    <a href=\"$row[Link]\" title=\"$row[Name]\" target=\"_blank\"><b>$row[Name]</A><br></b>$row[Description]... <a href=\"$row[Link]\" target=\"_blank\">For more information</A></TD>
    
    ";
    		
    		echo "</tr>\n";
    		echo "</table>\n";
    		echo "<hr size='1'>\n";
    }
    
    $result  = mysql_query($sql) or die('Error, query failed');
    $numrows = mysql_num_rows($result);
    
    $maxPage = ceil($numrows/$rowsPerPage);
    
    $self = $_SERVER['PHP_SELF'];
    
    if ($pageNum > 1)
    {
    	$page = $pageNum - 1;
    	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
    
    	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
    }
    else
    {
    	$prev  = ' [Prev] '; 
    	$first = ' [First Page] ';
    }
    
    if ($pageNum < $maxPage)
    {
    	$page = $pageNum + 1;
    	$next = " <a href=\"$self?page=$page&name=$name\">[Next]</a> ";
    
    	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    }
    else
    {
    	$next = ' [Next] '; 
    	$last = ' [Last Page] ';
    }
    
    echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
    
    mysql_close($connection);
    
    }
    
    ?>
    
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="name"> <br />
    <input type="submit" name="submit">
    </form>
    I was just coping an example I saw for creating the search form, so it could have some wrong information also.

    Sharan
    Last edited by Sharan; August 22nd, 2008 at 12:24 PM. Reason: put from - changed to form

  16. #16
    Member
    Join Date
    July 13th, 2008
    Posts
    57
    Sharan, sorry for the late reply. was held up with work.

    change this line if ($_POST) { to { (basically remove the if condition) and try.

  17. #17
    Member
    Join Date
    July 13th, 2008
    Posts
    57
    Also, change this line

    $name = strip_tags($_POST['name']);

    to
    $name = strip_tags(trim ($_POST['name']));
    if ($name == "")
    $name = strip_tags(trim ($_GET['name']));

    Its a quick fix, but most likely it should work for now.

  18. #18
    ABW Ambassador bettylou's Avatar
    Join Date
    December 27th, 2005
    Location
    Indiana
    Posts
    595
    Hi Sharan,

    Sorry, I had to run to the grocery store and make lunch for my daughter and myself. This works when I plug in my data for my own database but it does need some cleaning up (escape the data and such). Also, I generally make sure that the keyword is at least 2 or 3 letters in length. If you want I can add that.


    PHP Code:
    <?php

    $name 
    $_REQUEST['name'];
    $page $_REQUEST['page'];

    if (
    $name <> '') {
        
      
    $username="XXX";
      
    $password="XXX";
      
    $table="XXX";
      
    $database="XXX";
      
    $connection mysql_connect(localhost,$username,$password);
      @
    mysql_select_db($database) or die( "Unable to select database");

      
    $rowsPerPage 10;
      
    $pageNum 1;

    if (
    $page <> ''){
      
        
    $pageNum $_REQUEST['page'];
      }

    $offset = ($pageNum 1) * $rowsPerPage;

    $query $name;
    $smart_query $query;
    echo 
    $smart_query;
    echo 
    $page;
    $sql "SELECT * FROM $table WHERE name LIKE '%$smart_query%' ";


    $pagingQuery "LIMIT $offset$rowsPerPage";
    $result mysql_query($sql $pagingQuery) or die('Error, query failed');

    $row[Description]=substr($row[Description], 0250);
    while (
    $row mysql_fetch_array($result))
        {
            
    extract($row);
            echo 
    "<table align='center' width='80%' border='0' cellspacing='0' cellpadding='3'> \n";
            echo 
    "<tr>\n";
            echo 
    "<td valign='top'>\n

    <a href='
    $Link'> <img src='$Thumbnail' border=0> </a> \n <br> <b> Price: </b> $$Price\n </td> \n";
            
            echo 
    "<td valign='top'>\n
    <a href=\"
    $row[Link]\" title=\"$row[Name]\" target=\"_blank\"><b>$row[Name]</A><br></b>$row[Description]... <a href=\"$row[Link]\" target=\"_blank\">For more information</A></TD>

    "
    ;
            
            echo 
    "</tr>\n";
            echo 
    "</table>\n";
            echo 
    "<hr size='1'>\n";
    }

    $result  mysql_query($sql) or die('Error, query failed');
    $numrows mysql_num_rows($result);

    $maxPage ceil($numrows/$rowsPerPage);

    $self $_SERVER['PHP_SELF'];

    if (
    $pageNum 1)
    {
        
    $page $pageNum 1;
        
    $prev " <a href=\"$self?page=$page&name=$name\">[Prev]</a> ";

        
    $first " <a href=\"$self?page=1&name=$name\">[First Page]</a> ";
    }
    else
    {
        
    $prev  ' [Prev] '
        
    $first ' [First Page] ';
    }

    if (
    $pageNum $maxPage)
    {
        
    $page $pageNum 1;
        
    $next " <a href=\"$self?page=$page&name=$name\">[Next]</a> ";

        
    $last " <a href=\"$self?page=$maxPage&name=$name\">[Last Page]</a> ";
    }
    else
    {
        
    $next ' [Next] '
        
    $last ' [Last Page] ';
    }

    echo 
    $first $prev " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " $next $last;

    mysql_close($connection);

    }else{

    ?>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" name="name"> <br />
    <input type="submit" name="submit">
    </form>

    <?php
    }
    ?>

    Well, I have to go pick up my son from school. I'll check in when I get home. Let me know how it goes.

  19. #19
    Member
    Join Date
    July 13th, 2008
    Posts
    57
    basically, nothing is wrong with the pagination code, but the query string wasn't getting passed correctly. Either of the above two solutions should work.

    As bettylou said, some clean up is required for this script. but its good for now :-)

  20. #20
    ABW Ambassador bettylou's Avatar
    Join Date
    December 27th, 2005
    Location
    Indiana
    Posts
    595
    Sharan,

    I just saw that I left two echo statements in the above code. You can just delete those. I do that so I can see exactly what is being passed through to the variable. Sorry about that.

  21. #21
    Member Sharan's Avatar
    Join Date
    May 19th, 2006
    Location
    SW Missouri
    Posts
    54
    bettylou.

    It works

    I was about to post the changes I had made and the results when I saw your post -- I made a couple more tweaks and that took care of the problems I had. I also moved the } from just before
    mysql_close($connection);
    to just after
    if ($_POST) {
    $name = strip_tags(trim ($_POST['name']));
    if ($name == "")
    $name = strip_tags(trim ($_GET['name']));
    anabayan - thanks for that

    Thanks for all your help,
    Sharan

  22. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Pagination
    By drumminlogan in forum PopShops
    Replies: 6
    Last Post: November 21st, 2008, 06:01 PM
  2. Combining programs???
    By globalsaint in forum Rakuten LinkShare - LS
    Replies: 2
    Last Post: March 22nd, 2007, 05:56 PM
  3. Combining programs???
    By globalsaint in forum Commission Junction - CJ
    Replies: 0
    Last Post: March 22nd, 2007, 04:50 PM
  4. Combining Affiliate Programs with RSS
    By mika in forum Midnight Cafe'
    Replies: 1
    Last Post: May 31st, 2004, 04:56 PM
  5. Combining two feeds...
    By fredollars in forum WebMerge (Fourthworld.com)
    Replies: 3
    Last Post: May 3rd, 2004, 05:25 AM

Posting Permissions

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