Paging Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daveeboi
    New Member
    • Oct 2007
    • 18

    Paging Problems

    When running the script below I am able to get all related table results and display them the way I want. When it came to my paging script I entered and limited the rows per page to 2 (for a 3 record result) I get the first 2 records on page fine, when click through to next I get notices about undefined variables. I really dont understand.

    The code is,
    [code=php]
    <html>
    <head>

    </head>
    <body>
    <?php

    //include connection.php

    //This checks to see if there is a page number. If not, it will set it to page 1
    if (!(isset($pagen um)))
    {
    $pagenum = 1;
    }

    $min_price = $_POST['min_price'];
    $max_price = $_POST['max_price'];
    $property_type = $_POST['type'];
    $location = $_POST['location'];
    $min_bedrooms = $_POST['min_beds'];
    $keywords = $_POST['keywords'];

    if($keywords != " ")
    {
    $result = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' AND keywords LIKE '%$keywords%' ");
    }
    else
    {
    $result = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' ");
    }
    if(!$result) die(mysql_error ());
    $rows = mysql_num_rows( $result);
    if($rows== 0) die("<p>No matches met your criteria.");

    //This is the number of results displayed per page
    $page_rows = 2;

    //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;
    }

    //This sets the range to display in our query
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
    //This is your query again, the same one... the only difference is we add $max into it
    //$data_p = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' AND keywords LIKE '%$keywords%' $max") or die(mysql_error ());
    if($keywords != " ")
    {
    $data_p = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' AND keywords LIKE '%$keywords%' $max");
    }
    else
    {
    $data_p = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' $max");
    }
    if(!$data_p) die(mysql_error ());

    while($info=mys ql_fetch_array( $data_p)){

    $formatted = number_format($ info['price'],2);

    echo "<div id=\"right\"><i mg src=\"".$info['image']."\"><a href=\"detail.p hp\">".$info['title']."</a><h4>Offers over £".$formatted." </h4><p>".$info['type'].", ".$info['bedrooms']." bedrooms</p><br /></div>\n";

    }

    echo "<p>";

    // This shows the user what page they are on, and the total number of pages
    echo " --Page $pagenum of $last-- <p>";

    // 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)
    {
    }
    else
    {
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo " ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$prev ious'> <-Previous</a> ";
    }

    //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 '>Next -></a> ";
    echo " ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last '>Last ->></a> ";
    }

    ?>
    </body>
    </html>
    [/code]
    It may be because I'm tired but I can't see what would cause this. Please help if you can.
    Last edited by ak1dnar; Oct 3 '07, 02:38 PM. Reason: Code tags what else
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    When you pass the URL variable pagenum
    Code:
    <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    You need to extract this on the new page with $_GET['pagenum'].
    The following means pagenum will always be 1
    [PHP]if (!(isset($pagen um)))
    {
    $pagenum = 1;
    } [/PHP] You need
    [PHP]if (!(isset($_GET['pagenum'])))
    {
    $pagenum = 1;
    } [/PHP]

    Comment

    • daveeboi
      New Member
      • Oct 2007
      • 18

      #3
      Originally posted by code green
      When you pass the URL variable pagenum
      Code:
      <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
      You need to extract this on the new page with $_GET['pagenum'].
      The following means pagenum will always be 1
      [PHP]if (!(isset($pagen um)))
      {
      $pagenum = 1;
      } [/PHP] You need
      [PHP]if (!(isset($_GET['pagenum'])))
      {
      $pagenum = 1;
      } [/PHP]
      Thanks for the reply but it made no difference just the exact same output!

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Thanks for the reply but it made no difference just the exact same output!
        Well you have to make some personal effort. I was only showing where you have gone wrong, not rewriting the code for you.
        You still need to extract the pagenum variable from the URL and pass this into $pagenum.[PHP]$pagenum = $_GET['pagenum'];[/PHP]

        Comment

        • daveeboi
          New Member
          • Oct 2007
          • 18

          #5
          Originally posted by code green
          Well you have to make some personal effort. I was only showing where you have gone wrong, not rewriting the code for you.
          You still need to extract the pagenum variable from the URL and pass this into $pagenum.[PHP]$pagenum = $_GET['pagenum'];[/PHP]
          sorry if I offended you with the last comment but all I am saying is that, to me, the code you advised to put in has the exact same effect as the code I already had. It's just written a different way.

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Hi,
            This forum software provides CODE tags to format your source codes. when the text editor appears, See the REPLY GUIDELINES on the right side of the page, to find out how to use them.

            [code=php]

            Comment

            • daveeboi
              New Member
              • Oct 2007
              • 18

              #7
              Still having problems with this. Anyone else evr had problems with an "unidentifi ed index" whilst trying to insert pagination?

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                Still having problems with this.
                That is because you have made no attempt. I have shown you what you are doing wrong.
                You are not extracting the URL variables when paging through to the next page.
                the code you advised to put in has the exact same effect as the code I already had. It's just written a different way.
                Of course it is written a different way. It is written the correct way.
                Anyone else evr had problems with an "unidentifi ed index" whilst trying to insert pagination
                Please do not talk over me.
                It seems like you need taking by the hand and walking through this.
                What does this piece of code look like now? [PHP]if (!(isset($pagen um)))
                {
                $pagenum = 1;
                } [/PHP]

                Comment

                • daveeboi
                  New Member
                  • Oct 2007
                  • 18

                  #9
                  I have put in ...
                  [PHP]<?php
                  $pagenum = $_GET['pagenum'];[/PHP]

                  at the top of my file andfollowed on with...
                  [PHP]if (!(isset($pagen um)))
                  {
                  $pagenum = 1;
                  }[/PHP]

                  And I also tested it further done the code with an echo statement to extract the page number. It appeared on first page as expected - yet proceeding page just had an unidentified index message and also printed my "No Matches" message.

                  I sincerley appologise if I have upset you in anyway I did not intend to insult you in any manner. I hole heartidly appreciate your advice. Thank you.

                  Comment

                  • code green
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1726

                    #10
                    OK no problem. It's been a long day for me also.
                    Well at least now it looks like we are extracting the URL variable pagenum.
                    After this line [PHP]$pagenum = $_GET['pagenum'];[/PHP]Write [PHP]echo $pagenum;[/PHP]
                    The following test is now redundant because $pagenum is set as soon as you declare it.
                    [PHP]if (!(isset($pagen um)))[/PHP]It is better if now [PHP]if (!empty($pagenu m))[/PHP]This will tell you if anything was actually passed.
                    Lets have a look at the hyperlinks. They appear more than once which is not ideal
                    [PHP]echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next '>Next -></a> ";
                    echo " ";
                    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last '>Last ->></a> ";[/PHP]The quotes may be causing a problem, single quotes are literal strings and you have not escaped any.
                    I have a rule. Single quotes for php. Double for HTML.
                    The following is faster but requires more care when writing. But it is correct.
                    [PHP]echo '<a href="'.$_SERVE R['PHP_SELF'].'?pagenum='.$n ext.'">Next -></a>';[/PHP]And echo out all pagenum, next and previous variables so we can see what is there. Good luck.

                    Comment

                    • daveeboi
                      New Member
                      • Oct 2007
                      • 18

                      #11
                      I still haven't conquered this problem, I spent most of the day yesterday and all of last night trying to sort this out but I am totally stumped. I think I'm just going to have to go with just having 1 results page and maybe just links on it to keep it small. Not ideal but at least it will work, unlike the myth that is pagination.

                      Comment

                      • daveeboi
                        New Member
                        • Oct 2007
                        • 18

                        #12
                        When I put in this part of test code to see what the variable contained...
                        [PHP]$pagenum = $_GET['pagenum'];
                        echo"Statement after declare of pagenum";
                        echo $pagenum;[/PHP]

                        it produced the text line but no value what so ever for $pagenum. Any suggestions anybody?

                        Comment

                        • code green
                          Recognized Expert Top Contributor
                          • Mar 2007
                          • 1726

                          #13
                          Now we can start to narrow down your problem.
                          You have implemented one suggestion of mine but you have obviously not done this[PHP] if (!empty($pagenu m))[/PHP] Like I said, this will show if pagenum contains a value. Whilst isset() will not.
                          And if you check your code, everything depends upon pagenum having a value.
                          But pagenum does not have a value when the script first runs.
                          That is why nothing happens.
                          You need something like [PHP] if (empty($pagenum ))
                          $pagenum = 1;[/PHP] Did you follow my suggestion to re-write the hyperlinks by the way?

                          Comment

                          • daveeboi
                            New Member
                            • Oct 2007
                            • 18

                            #14
                            I implemented all of your suggestions yesterday but still couldn't crack the conundrun, my code as is ....
                            [PHP]<html>
                            <head>
                            <html>
                            <head>
                            </head>
                            <body>
                            <?php
                            $pagenum = $_GET['pagenum'];
                            echo"Statement after declare of pagenum";
                            echo $pagenum;

                            // Connects to your Database
                            // have my connection details here

                            //This checks to see if there is a page number. If not, it will set it to page 1
                            if (!empty($pagenu m))
                            {
                            $pagenum = 1;
                            }
                            echo $pagenum;

                            $min_price = $_POST['min_price'];
                            $max_price = $_POST['max_price'];
                            $property_type = $_POST['type'];
                            $location = $_POST['location'];
                            $min_bedrooms = $_POST['min_beds'];
                            $keywords = $_POST['keywords'];

                            if($keywords != " ")
                            {
                            $result = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' AND keywords LIKE '%$keywords%' ");
                            }
                            else
                            {
                            $result = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' ");
                            }
                            if(!$result) die(mysql_error ());
                            $rows = mysql_num_rows( $result);
                            if($rows== 0) die("<p>No matches met your criteria.");

                            //This is the number of results displayed per page
                            $page_rows = 2;

                            //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;
                            }

                            //This sets the range to display in our query
                            $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
                            //This is your query again, the same one... the only difference is we add $max into it
                            //$data_p = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' AND keywords LIKE '%$keywords%' $max") or die(mysql_error ());
                            //echo $pagenum;
                            if($keywords != " ")
                            {
                            $data_p = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' AND keywords LIKE '%$keywords%' $max");
                            }
                            else
                            {
                            $data_p = mysql_query("SE LECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type ' $max");
                            }
                            if(!$data_p) die(mysql_error ());

                            while($info=mys ql_fetch_array( $data_p))
                            {
                            $formatted = number_format($ info['price'],2);

                            echo "<div id=\"right\"><i mg src=\"".$info['image']."\"><a href=\"detail.p hp?id=". $info['id']. "\">".$info['title']."</a><h4>Offers over £".$formatted." </h4><p>".$info['type'].", ".$info['bedrooms']." bedrooms</p><br /></div>\n";

                            }

                            echo "<p>";

                            // This shows the user what page they are on, and the total number of pages
                            echo " --Page $pagenum of $last-- <p>";

                            // 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)
                            {
                            }
                            else
                            {
                            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
                            echo " ";
                            $previous = $pagenum-1;

                            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$prev ious'> <-Previous</a> ";
                            }

                            //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="'.$_SERVE R['PHP_SELF'].'?pagenum='.$n ext.'">Next -></a>';
                            echo '<a href="'.$_SERVE R['PHP_SELF'].'?pagenum='.$l ast.'">Last-></a>';
                            }

                            ?>
                            </body>
                            </html>[/PHP]

                            I'm not sure why there is no value what so ever given for pagenum when I echo it at the top.

                            Comment

                            • code green
                              Recognized Expert Top Contributor
                              • Mar 2007
                              • 1726

                              #15
                              Your not quite there with this.[PHP]if (!empty($pagenu m))
                              {
                              $pagenum = 1;
                              }
                              echo $pagenum;[/PHP] empty returns true if the variable is empty and NOT will flip this. Try to be a little more diligent [PHP]if (empty($pagenum ))
                              {
                              $pagenum = 1;
                              }
                              echo $pagenum;[/PHP]

                              Comment

                              Working...