SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2002
    Location
    Scotland, UK
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This PHP script I stole dont work ! :)

    I used some code I found on PHPBuilder to add page numbers to my database driven news feed. Thing is it doesnt seem to work right

    I had to rewrite bits of it to get it running this far!

    The main problem with it now is that it displays a NEXT PAGE link when it is on the last page (causing it to load a blank page if clicked).

    Im ok with PHP but not when debugging someone elses code, could someone have a quick look and see where its going wrong.

    Thanks

    PHP Code:
    <?

    $dbhost = "";
    $dbname = "";
    $dbuser = "";
    $dbpasswd = "";

    $tb = "news";

    $connect = mysql_connect($dbhost, $dbuser, $dbpasswd);
    mysql_select_db($dbname);


    $limit=5; // rows to return
    $numresults=mysql_query("select * from $tb order by id desc");
    $numrows=mysql_num_rows($numresults);

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

    // get results
    $result=mysql_query("SELECT * FROM $tb ORDER BY id DESC limit $page,$limit");

    // now you can display the results returned
    while ($data=mysql_fetch_array($result)) {

    print "<A NAME='";
    print $data[id];
    print "'>";
    print "<B><FONT COLOR=\"#ffff88\">";
    print $data[date];
    print " - ";
    print nl2br(stripslashes($data[headline]));
    print "...</FONT></B><P>";
    print "<IMG SRC='/news/images/";
    print $data[image];
    print "'><P>";
    print nl2br(stripslashes($data[newstext]));
    print "<P><HR COLOR=\"#AAAABB\">\n";

    }

    print "<BR>\n";

    // next we need to do the links to other results

    if ($page!=0) { // bypass PREV link if page is 0
        $prevpage=$page-5;
        print "<a href=\"$PHP_SELF?page=$prevpage\">PREV</a> &nbsp; \n";
    }

    // 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%$limit) {
        // has remainder so add one page
        // rikki $pages++;
    }


    for ($i=1;$i<=$pages;$i++) { // loop thru
        $newpage=$limit*($i-1);
        print "<a href=\"$PHP_SELF?page=$newpage\">$i</a> &nbsp; \n";
    }

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

    if (!(($page/$limit)==$pages) && $pages!=1) {
        // not last page so give NEXT link
        $newpage=$page+$limit;
        print "<a href=\"$PHP_SELF?page=$newpage\">NEXT</a><p>\n";
    }

  2. #2
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
    $dbhost = "";
    $dbname = "";
    $dbuser = "";
    $dbpasswd = "";

    $tb = "news";

    $connect = mysql_connect($dbhost, $dbuser, $dbpasswd);
    mysql_select_db($dbname);

    $limit=5;
    $numresults=mysql_query("select * from $tb order by id desc");
    $numrows=mysql_num_rows($numresults);

    if (empty($page)) {
        $page=0;
    }

    $result=mysql_query("SELECT * FROM $tb ORDER BY id DESC limit $page,$limit");

    while ($data=mysql_fetch_array($result)) {

    print "<A NAME='";
    print $data[id];
    print "'>";
    print "<B><FONT COLOR=\"#ffff88\">";
    print $data[date];
    print " - ";
    print nl2br(stripslashes($data[headline]));
    print "...</FONT></B><P>";
    print "<IMG SRC='/news/images/";
    print $data[image];
    print "'><P>";
    print nl2br(stripslashes($data[newstext]));
    print "<P><HR COLOR=\"#AAAABB\">\n";

    }

    print "<BR>\n";


    if ($page > 0) { //(a better check here) bypass PREV link if page is 0
        $prevpage=$page-5;
        print "<a href=\"$PHP_SELF?page=$prevpage\">PREV</a> &nbsp; \n";
    }

    $pages=intval($numrows/$limit);

    if ($numrows%$limit) {
     $pages++;
    }


    for ($i=1;$i<=$pages;$i++) { // loop thru
        $newpage=$limit*($i-1);
        print "<a href=\"$PHP_SELF?page=$newpage\">$i</a> &nbsp; \n";
    }

    if ($numrows-$page > $limit) { // much cleaner code here to check if $numrows minus $page is greater than the limit of 5,
    // the code before had the variable $pages which was in the wrong place
        $newpage=$page+$limit;
        print "<a href=\"$PHP_SELF?page=$newpage\">NEXT</a><p>\n";
    }
    ?>
    Last edited by redgoals; Jun 19, 2002 at 04:54.
    http://redgoals.com ... my site ... nuff said

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2002
    Location
    Scotland, UK
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that, Im still a little lost could you tell me what you did to make it work ?

    Thanks

  4. #4
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i annotated my post, take a look.
    http://redgoals.com ... my site ... nuff said

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2002
    Location
    Scotland, UK
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by redgoals
    i annotated my post, take a look.
    Many thanks, this forum and its users (like yourself) are the best source of help and information, I just hope some day when I learn a little more I can be useful here too.


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
  •