Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Pagination Problem!

    Hi All,

    I have a page pagination script, which will brake up the returned results (for the searched terms) but once I click through to the second page, instead of returning the 2nd page of results I searched for.... It starts to return all of the results..

    So say I searched for apple, it would return 5 instances of that and split then when i clicked page 2 it would return everything, e.g. orange, grape, pear etc etc...

    The code is:

    PHP Code:
    <?php
    $name 
    mysql_real_escape_string($_POST['name']);
        
    $lea mysql_real_escape_string($_POST['lea']);
        
    $type mysql_real_escape_string($_POST['type']);
        
    $postcode mysql_real_escape_string($_POST['postcode']);

        if (isset(
    $_GET['pageno'])) {
               
    $pageno $_GET['pageno'];
            } else {
               
    $pageno 1;
        } 
    // if
        
        
    $query "SELECT count(*) FROM schools WHERE Name LIKE '%$name%' AND Leaname LIKE '%$lea%' AND SchoolType LIKE '%$type%' AND PostcodeSubArea LIKE '%$postcode' ORDER BY Name ASC";
            
    $result mysql_query($query) or die (mysql_error());
                
    $query_data mysql_fetch_row($result);
                    
    $numrows $query_data[0];
                    
        
    $rows_per_page 5;
            
    $lastpage  ceil($numrows/$rows_per_page);
        
        
    $pageno = (int)$pageno;
            if (
    $pageno $lastpage) {
                   
    $pageno $lastpage;
            } 
    // if
            
    if ($pageno 1) {
                   
    $pageno 1;
            } 
    // if
            
        
    $limit 'LIMIT ' .($pageno 1) * $rows_per_page .',' .$rows_per_page;

        
    $query "SELECT * FROM schools WHERE Name LIKE '%$name%' AND Leaname LIKE '%$lea%' AND SchoolType LIKE '%$type%' AND PostcodeSubArea LIKE '%$postcode' $limit";
            
    $result mysql_query($query) or die(mysql_error());
                while (
    $row mysql_fetch_array ($result)) {
        
    ?>
        <a href="schooldetails.php?RID=<?php echo $row['RID'];?>"><?php echo $row['Name'];?></a>    
        <?php        
        
    echo '<br/>';
        echo 
    '<br/>';
        echo 
    'LEA:'.' '.'<span class="normalfont">'.$row['Leaname'].'</span>';
        echo 
    '<br/>';
        echo 
    'Address:'.' '.'<span class="normalfont">'.$row['Address1'].'</span>';
        echo 
    '<br/>';
        echo 
    '<span class="normalfont">'.$row['Address2'].'</span>';
        echo 
    '<br/>';
        echo 
    '<span class="normalfont">'.$row['Address3'].'</span>';
        echo 
    '<br/>';
        echo 
    'Postcode:'.' '.'<span class="normalfont">'.$row['Postcode'].'</span>';
        echo 
    '<br/>';
        echo 
    'Telephone:'.' '.'<span class="normalfont">0'.$row['Phone'].'</span>';
        echo 
    '<br/>';
        echo 
    'Fax:'.' '.'<span class="normalfont">0'.$row['Fax'].'</span>';
        echo 
    '<br/>';?>
        Website: <span class="normalfont"><a href="<?php echo $row['url']?>"><?php echo $row['url'];?></a></span>
    <?php
        
    echo '<br/>';
        echo 
    '<br/>';
    }
        if (
    $pageno == 1) {
               echo 
    " FIRST PREV ";
        } else {
               echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
               
    $prevpage $pageno-1;
               echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
        } 
    // if

        
    echo " ( Page $pageno of $lastpage ) ";

        if (
    $pageno == $lastpage) {
               echo 
    " NEXT LAST ";
        } else {
               
    $nextpage $pageno+1;
               echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
               echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
        } 
    // if
    Can anyone see what I'm missing here?

    I followed a tutorial on the net, but it doesn't seem to be working!

    Thanks,

    Greens85
    Last edited by greens85; 10-26-2009 at 05:52 PM.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You must have missed something in the tutorial because the POST variables won't be passed to the "next" page by clicking a link. Without those, all of your WHERE..LIKE clauses are empty, which means you're going to get all results.
    Are you a Help Vampire?

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tomws View Post
    You must have missed something in the tutorial because the POST variables won't be passed to the "next" page by clicking a link. Without those, all of your WHERE..LIKE clauses are empty, which means you're going to get all results.
    Hey Tomws,

    Thanks for the reply... I don't see how i could of missed anything as its broken up into different pieces of code, and I copied them one by one (I could have even copied and pasted but I thought it might help me learn if I copied the code my self)...

    Just for the reference what I used was:

    http://www.tonymarston.net/php-mysql/pagination.html#a4

    I see your point about no variables being passed, but if you click that link you'll see it is the same there

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You're right: you didn't miss anything. But you did add to it. (Just the POST vars.)

    You'll need to pass the initial POST variables to subsequent pages. I'm not sure what the usual way of doing this is, but you could look at passing keywords in the GET string or using sessions. Maybe other ways. I would probably go for sessions, but the GET option would allow for bookmarking of results pages, if that's desired.
    Are you a Help Vampire?

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tomws View Post
    You're right: you didn't miss anything. But you did add to it. (Just the POST vars.)

    You'll need to pass the initial POST variables to subsequent pages. I'm not sure what the usual way of doing this is, but you could look at passing keywords in the GET string or using sessions. Maybe other ways. I would probably go for sessions, but the GET option would allow for bookmarking of results pages, if that's desired.
    Sorry I didnt really get what you meant by that

    When you say I added to it, im guessing you mean these lines, where i am just posting what the user entered?

    PHP Code:
    $name mysql_real_escape_string($_POST['name']);
    $lea mysql_real_escape_string($_POST['lea']);
    $type mysql_real_escape_string($_POST['type']);
    $postcode mysql_real_escape_string($_POST['postcode']); 
    When you say passing keywords in the get string, do you mean when they click next, if so im not sure how to do this

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Yes, those are the added lines I'm talking about. They're important because those are your search terms. Subsequent pages need that information because you're querying based upon those terms. POST values are only available to the page to which the form action points.

    Passing the keywords (or "search terms", or "magic monkeys", or whatever you want to call them) in the GET string is easy. Do it just like you're passing the page number. Here's one line modified, for example:
    PHP Code:
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&name=$name&lea=$lea&type=$type&postcode=$postcode'>NEXT</a> "
    Doing that, you'd need to modify the top of the code to test whether the variables are coming in GET or POST. Perhaps something like this:
    PHP Code:
    // Am I coming from the form page?
    if (isset($_POST['submit'])) // the name of the submit button, whatever you named it
    {
      
    // use the POST vars
    }
    else 
    // Not coming from the form page
    {
      
    // use the GET vars

    Last edited by tomws; 10-26-2009 at 03:55 PM.
    Are you a Help Vampire?

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    With the if statement am i correct in thinking:

    PHP Code:
    if (isset($_POST['submit'])) {
            
    $name mysql_real_escape_string($_POST['name']);
        
    $lea mysql_real_escape_string($_POST['lea']);
        
    $type mysql_real_escape_string($_POST['type']);
        
    $postcode mysql_real_escape_string($_POST['postcode']);
    } else {
      
    // Not sure how I display the GET vars here???


  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    PHP Code:
    if (isset($_POST['submit'])) {
            
    $name mysql_real_escape_string($_POST['name']);
        
    $lea mysql_real_escape_string($_POST['lea']);
        
    $type mysql_real_escape_string($_POST['type']);
        
    $postcode mysql_real_escape_string($_POST['postcode']);
    } else {
        
    $name mysql_real_escape_string($_GET['name']);
        
    $lea mysql_real_escape_string($_GET['lea']);
        
    $type mysql_real_escape_string($_GET['type']);
        
    $postcode mysql_real_escape_string($_GET['postcode']);

    Are you a Help Vampire?

  • #9
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tomws View Post
    PHP Code:
    if (isset($_POST['submit'])) {
            
    $name mysql_real_escape_string($_POST['name']);
        
    $lea mysql_real_escape_string($_POST['lea']);
        
    $type mysql_real_escape_string($_POST['type']);
        
    $postcode mysql_real_escape_string($_POST['postcode']);
    } else {
        
    $name mysql_real_escape_string($_GET['name']);
        
    $lea mysql_real_escape_string($_GET['lea']);
        
    $type mysql_real_escape_string($_GET['type']);
        
    $postcode mysql_real_escape_string($_GET['postcode']);

    Hey Tomws,

    This is the code I have now:

    PHP Code:
    <?php
        
    if (isset($_POST['submit'])) {
            
    $name mysql_real_escape_string($_POST['name']);
            
    $lea mysql_real_escape_string($_POST['lea']);
            
    $type mysql_real_escape_string($_POST['type']);
            
    $postcode mysql_real_escape_string($_POST['postcode']);
        } else {
            
    $name mysql_real_escape_string($_GET['name']);
            
    $lea mysql_real_escape_string($_GET['lea']);
            
    $type mysql_real_escape_string($_GET['type']);
            
    $postcode mysql_real_escape_string($_GET['postcode']);
    }

    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&amp;Name=$name&amp;Leaname=$lea&amp;SchoolType=$type&amp;PostcodeSubArea=$postcode'>NEXT</a> ";
    ?>
    But the same problems are occuring

    Have I missed something?

  • #10
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Why is that the only code you have? Re-read the previous posts. I never said to destroy everything and use only those lines.
    Are you a Help Vampire?

  • #11
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tomws View Post
    Why is that the only code you have? Re-read the previous posts. I never said to destroy everything and use only those lines.
    sorry didnt make myself clear...

    I only posted those lines as they are the only ones that have been altered (upon your knowledge)... the full code is:

    PHP Code:
    <?php
        
        
    if (isset($_POST['submit'])) {
            
    $name mysql_real_escape_string($_POST['name']);
            
    $lea mysql_real_escape_string($_POST['lea']);
            
    $type mysql_real_escape_string($_POST['type']);
            
    $postcode mysql_real_escape_string($_POST['postcode']);
        
        } else {
        
            
    $name mysql_real_escape_string($_GET['name']);
            
    $lea mysql_real_escape_string($_GET['lea']);
            
    $type mysql_real_escape_string($_GET['type']);
            
    $postcode mysql_real_escape_string($_GET['postcode']);
    }
        
        if (isset(
    $_GET['pageno'])) {
               
    $pageno $_GET['pageno'];
            } else {
               
    $pageno 1;
        } 
    // if
        
        
    $query "SELECT count(*) FROM schools WHERE Name LIKE '%$name%' AND Leaname LIKE '%$lea%' AND SchoolType LIKE '%$type%' AND PostcodeSubArea LIKE '%$postcode' ORDER BY Name ASC";
            
    $result mysql_query($query) or die (mysql_error());
                
    $query_data mysql_fetch_row($result);
                    
    $numrows $query_data[0];
                    
        
    $rows_per_page 5;
            
    $lastpage  ceil($numrows/$rows_per_page);
        
        
    $pageno = (int)$pageno;
            if (
    $pageno $lastpage) {
                   
    $pageno $lastpage;
            } 
    // if
            
    if ($pageno 1) {
                   
    $pageno 1;
            } 
    // if
            
        
    $limit 'LIMIT ' .($pageno 1) * $rows_per_page .',' .$rows_per_page;

        
    $query "SELECT * FROM schools WHERE Name LIKE '%$name%' AND Leaname LIKE '%$lea%' AND SchoolType LIKE '%$type%' AND PostcodeSubArea LIKE '%$postcode' $limit";
            
    $result mysql_query($query) or die(mysql_error());
                while (
    $row mysql_fetch_array ($result)) {
        
    ?>
        <a href="schooldetails.php?Name=<?php echo $row['Name'];?>"><?php echo $row['Name'];?></a>    
        <?php        
        
    echo '<br/>';
        echo 
    '<br/>';
        echo 
    'LEA:'.' '.'<span class="normalfont">'.$row['Leaname'].'</span>';
        echo 
    '<br/>';
        echo 
    'Address:'.' '.'<span class="normalfont">'.$row['Address1'].'</span>';
        echo 
    '<br/>';
        echo 
    '<span class="normalfont">'.$row['Address2'].'</span>';
        echo 
    '<br/>';
        echo 
    '<span class="normalfont">'.$row['Address3'].'</span>';
        echo 
    '<br/>';
        echo 
    'Postcode:'.' '.'<span class="normalfont">'.$row['Postcode'].'</span>';
        echo 
    '<br/>';
        echo 
    'Telephone:'.' '.'<span class="normalfont">0'.$row['Phone'].'</span>';
        echo 
    '<br/>';
        echo 
    'Fax:'.' '.'<span class="normalfont">0'.$row['Fax'].'</span>';
        echo 
    '<br/>';?>
        Website: <span class="normalfont"><a href="<?php echo $row['url']?>"><?php echo $row['url'];?></a></span>
    <?php
        
    echo '<br/>';
        echo 
    '<br/>';
    }
        if (
    $pageno == 1) {
               echo 
    " FIRST PREV ";
        } else {
               echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
               
    $prevpage $pageno-1;
               echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
        } 
    // if

        
    echo " ( Page $pageno of $lastpage ) ";

        if (
    $pageno == $lastpage) {
               echo 
    " NEXT LAST ";
        } else {
               
    $nextpage $pageno+1;
               echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&amp;Name=$name&amp;Leaname=$lea&amp;SchoolType=$type&amp;PostcodeSubArea=$postcode'>NEXT</a> ";
               echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
        } 
    // if
    ?>
    But like I said, im having the same problem as I was before

  • #12
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    See post #6 again. You're not using the link like I provided. Compare these lines:
    PHP Code:
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&name=$name&lea=$lea&type=$type&postcode=$postcode'>NEXT</a> ";  
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&amp;Name=$name&amp;Leaname=$lea&amp;SchoolType=$type&amp;PostcodeSubArea=$postcode'>NEXT</a> "
    Note also that I said that's one line modified and that you need to pass the values in each link if you're going to use the GET string.
    Are you a Help Vampire?

  • #13
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tomws View Post
    See post #6 again. You're not using the link like I provided. Compare these lines:
    PHP Code:
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&name=$name&lea=$lea&type=$type&postcode=$postcode'>NEXT</a> ";  
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&amp;Name=$name&amp;Leaname=$lea&amp;SchoolType=$type&amp;PostcodeSubArea=$postcode'>NEXT</a> "
    Note also that I said that's one line modified and that you need to pass the values in each link if you're going to use the GET string.
    Ahhh, sorry about that I thought that the text before the equals should have represented the name of the field in the database, but obviously not!

    I realised you had only done one line, I was just doing one also while I tested it!

    Anyway it's working now, so many thanks for your help which is greatly appreciated.

  • #14
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by greens85 View Post
    Ahhh, sorry about that I thought that the text before the equals should have represented the name of the field in the database, but obviously not!
    Ah! I didn't know where you got those from and thought you were crazy.

    You can make them whatever you want, but they must match the names used in the assigments at the top. I only used those to match the POST names - thought it might be less confusing.
    Are you a Help Vampire?


  •  

    Posting Permissions

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