Php Paging not showing all records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • underground
    New Member
    • Sep 2006
    • 41

    Php Paging not showing all records

    I have a problem that I've spent countless hours on and I'm more than certain this is a obviuos issue to an expert but I am still learning. I have a paging script that I have modified to display a certian amount of records per page. But when I click on the next link , no new result are displayed it keep displaying the same result. Here is a sample of the script
    [PHP]
    <?php include('hdb.ht ml'); ?>
    <?php
    // Get the search variable from URL
    $var = @$_GET['category'] ;
    $trimmed = trim($var); //trim whitespace from the stored variable

    // rows to return
    $limit=2;

    // check for an empty string and display a message.
    if ($trimmed == "")
    {
    echo "<p class=T1>Please enter a search...</p>";
    exit;
    }

    // check for a search parameter
    if (!isset($var))
    {
    echo "<p class=T1>We dont seem to have a search parameter!</p>";
    exit;
    }

    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect(" xxxxx","xxxxxxx ","xxxxxx") ; //(host, username, password)

    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db ("xxxxxxxxx" ) or die("Unable to select database"); //select which database we're using

    // Build SQL Query
    $query = "select * from pix where category like \"%$trimmed% \"
    order by category, pid, albumname, "; // EDIT HERE and specify your table and field names for the SQL query

    $numresults=mys ql_query($query );
    $numrows=mysql_ num_rows($numre sults);

    // If we have no results, offer a google search as an alternative

    if ($numrows == 0)
    {
    echo "<h4>Result s</h4>";
    echo "<p class=T1>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

    // google
    echo "<p class=T1><a href=\"http://www.google.com/search?q="
    . $trimmed . "\" target=\"_blank \" title=\"Look up
    " . $trimmed . " on Google\">Click here</a> to try the
    search on google</p>";
    }

    // next determine if s has been passed to script, if not use 0
    if (empty($s)) {
    $s=0;
    }

    // get results
    $query .= " limit $s,$limit";
    $result = mysql_query($qu ery) or die("Couldn't execute query");

    // display what the person searched for
    echo '<center><tabl e width="550" border="0" cellspacing="1" cellpadding="2" bgcolor="#Fffff f"><tr>';
    echo '<td width=230 class=T1>';
    echo "<p class=T1>Your searched for: &quot;" .$var . "&quot;</p>";
    echo '</td>
    <td align=right width=335 padding="3" bgcolor=#eoeccc class=T1>
    //html goes here
    </td>
    </tr></table></center>';

    // begin to show results set
    echo "";
    $count = 1 + $s ;

    // now you can display the results returned
    while ($row= mysql_fetch_arr ay($result)) {
    $state = stripslashes($r ow["state"]);
    $pid= stripslashes($r ow['pid']);
    $albumname= stripslashes($r ow['albumname']);

    echo '<center><tabl e width="550" border="0" cellspacing="1" cellpadding="2" bgcolor="#F7F7F 7"><tr>
    <td onmouseover="th is.className=\' bgcl\';" onmouseout="thi s.className=\'g b2\';" width=330 align=top bgcolor=#Ffffff class=T1>
    //html from output goes here
    </td>
    </tr>';
    }
    echo "</table></center>";
    $count++ ;

    $currPage = (($s/$limit) + 1);

    //break before paging
    echo "<br />";

    // next we need to do the links to other results
    if ($s>=1) { // bypass PREV link if s is 0
    $prevs=($s-$limit);
    print "&nbsp;<a href=\"".$_SERV ER['PHP_SELF']."??s=$prevs&ca tegory=$var\">& lt;&lt;
    Prev 10</a>&nbsp&nbsp;" ;
    }

    // calculate number of pages needing links
    $pages=intval($ numrows/$limit);

    // $pages now contains int of pages needed unless there is a remainder from division

    if ($numrows%$limi t) {
    // has remainder so add one page
    $pages++;
    }

    // check to see if last page
    if (!((($s+$limit)/$limit)==$pages ) && $pages!=1) {

    // not last page so give NEXT link
    $news=$s+$limit ;

    echo "&nbsp;<a href=\"".$_SERV ER['PHP_SELF']."?s=$news&cate gory=$var\">Nex t 10 &gt;&gt;</a>";
    }

    $a = $s + ($limit) ;
    if ($a > $numrows) { $a = $numrows ; }
    $b = $s + 1 ;
    echo "<center><p class=T1>Showin g results $b to $a of $numrows</p></center>";

    ?>
    <?php include('footer .html'); ?>
    [/PHP]
    I've already modified the php self function to work properly but the info being pass back to page when next link is clicked is not envoking the script to display the next set of records
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    You are conducting this test, [PHP]if (empty($s)) {
    $s=0;[/PHP]but have not read the value of $s through your GET array so $s will always be zero. You seem to pass it OK [PHP]echo "&nbsp;<a href=\"".$_SERV ER['PHP_SELF']."
    ?s=$news&catego ry=$var\">Next 10 &gt;&gt;</a>";[/PHP] but only read category [PHP]$var = @$_GET['category'] ;[/PHP]By the way DO NOT USE '@' except when deploying the final script live. This is a terrible habit.

    Comment

    • underground
      New Member
      • Sep 2006
      • 41

      #3
      I think I grasp what your saying. I will try it again and post back my findings latter.
      ;-)

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by code green
        By the way DO NOT USE '@' except when deploying the final script live. This is a terrible habit.
        I might add to that by saying that you should really never need to use '@' ever. If you want to hide your errors from the end User, just set:

        Code:
        display_errors = Off
        log_errors = On
        And optionally:

        Code:
        error_log /path/to/error_log
        in your php.ini file (usually located at /usr/local/lib/php.ini or somewhere else on Windows [somebody who uses a 'doze box on a regular basis will probably be a lot more helpful than I can in that respect]).

        Not only will your User never see error messages, but you don't have to go back into your code and remove a bunch of '@' characters if your script messes up.

        [EDIT: And most web hosting providers allow you to edit a php.ini file at the root level of your site.]
        Last edited by pbmods; May 8 '07, 10:34 PM. Reason: And now to make this relevant to 90% of the people that are going to read this....

        Comment

        • underground
          New Member
          • Sep 2006
          • 41

          #5
          Hi ,I just got in and used your advice the script worked perfectly. I added the Variable s to the get function! No more paging problem

          Thanks for the help

          Comment

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

            #6
            Well done Underground. I agree with pbmods. Why do people feel compelled to use '@'. in their scripts, especially when developing? I use the following code within a config file on all my scripts. Then I can use DEBUG and print 'Any message I want' all over the script and simply set DEBUG to zero to turn everything off.[PHP]
            if(!defined('DE BUG'))
            define('DEBUG', 1);

            if(DEBUG)
            error_reporting (E_ALL | E_STRICT);
            else
            ini_set('displa y_errors',0);[/PHP]

            Comment

            • Mesut
              New Member
              • May 2007
              • 3

              #7
              Hey hey hey hey hey hey

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Heya, Mesut.

                Originally posted by Mesut
                Hey hey hey hey hey hey
                Yes?

                Comment

                Working...