Results 1 to 5 of 5

 

Thread: PHP Pagination Problem

  1. #1
    Brett's Avatar
    Registered User

    Status
    Offline
    Join Date
    Dec 2003
    Location
    Oxford
    Posts
    521
    Thanks
    2
    Thanked 11 Times in 8 Posts


    Morning,

    I am using a script that was posted on WMW a while ago but the problem I have is that the first result is always missing.


    <?
    $db = mysql_connect("localhost",$username,$password) or die("Couldn't connect to the database.");
    mysql_select_db($database) or die("Couldn't select the database");
    /* Set current, prev and next page */
    $page = (!isset($_GET['page']))? 1 : $_GET['page'];
    $prev = ($page - 1);
    $next = ($page + 1);

    /* Max results per page */
    $max_results = 15;

    /* Calculate the offset */
    $from = (($page * $max_results) - $max_results);

    /* Query the db for to
    tal results. You need to edit the sql to fit your needs */
    $result = mysql_query("SELECT * from products where brand='$brand' and category='$category'");

    $total_results = mysql_num_rows($result);

    $total_pages = ceil($total_results / $max_results);

    $pagination = '';

    /* Create a PREV link if there is one */
    if($page > 1)
    {
    $pagination .= "<a href=\"review$brand-$category-$prev.htm\"class='maintext'>Previous</a>";
    }

    /* Loop through the total pages */
    for($i = 1; $i <= $total_pages; $i++)
    {
    if(($page) == $i)
    {
    $pagination .= $i;
    }
    else
    {
    $pagination .= "<a href=\"review$brand-$category-$i.htm\" class='maintext'>$i</a>";
    }
    }

    /* Print NEXT link if there is one */
    if($page < $total_pages)
    {
    $pagination .= "<a href=\"review$brand-$category-$next.htm\" class='maintext'>Next</a>";
    }
    $result=mysql_query("SELECT * from products where brand='$brand' and category='$category' order by product ASC LIMIT $from, $max_results ");

    while ($i = mysql_fetch_array($result))
    {
    echo "<table width='100%' class='maintext'>";
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){


    echo "results here";

    }

    echo "</table>";
    }
    ?>

    I think that my first result is getting put into this line: while ($i = mysql_fetch_array($result)) but am not sure how to overcome it.

    Thanks in advance for any help
    Brett
    Brett

  2. #2
    futureweb's Avatar
    Registered User

    Status
    Offline
    Join Date
    Jul 2005
    Location
    North Devon
    Posts
    919
    Thanks
    13
    Thanked 22 Times in 22 Posts
    Arrays start at 0 not 1

    for($i = 1; $i <= $total_pages; $i++)

    for($i = 0; $i <= $total_pages; $i++)

  3. #3
    Registered User

    Status
    Offline
    Join Date
    Aug 2003
    Posts
    2,448
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That for statement is ok as you want the first page to be page 1 and its not an array. The problem is here:-
    PHP Code:
    $result=mysql_query("SELECT * from products where brand='$brand' and category='$category' order by product ASC LIMIT $from, $max_results ");

    while (
    $i mysql_fetch_array($result))
    {
    echo 
    "<table width='100%' class='maintext'>";
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){


    echo 
    "results here";

    }

    echo 
    "</table>";

    The first mysql_fetch_array grabs the first product but doesn't dispay it.

    Change
    PHP Code:
    while ($i mysql_fetch_array($result))

    to
    PHP Code:
    if ($result!==false) { 
    and that should sort it out.

  4. #4
    futureweb's Avatar
    Registered User

    Status
    Offline
    Join Date
    Jul 2005
    Location
    North Devon
    Posts
    919
    Thanks
    13
    Thanked 22 Times in 22 Posts
    Haha lol what was I thinking

    I don't get why you would need that line at all wouldn't you just output the start and end of your table outside the loop anyway.

    And as Rich said above check a result returned

    Code:
     if(mysql_num_rows($result) != 0){ 
    
    // Loop through results
    
    }else{
    echo("Naff all there!");
    }

  5. #5
    Brett's Avatar
    Registered User

    Status
    Offline
    Join Date
    Dec 2003
    Location
    Oxford
    Posts
    521
    Thanks
    2
    Thanked 11 Times in 8 Posts
    Quote Originally Posted by Rich
    That for statement is ok as you want the first page to be page 1 and its not an array. The problem is here:-
    PHP Code:
    $result=mysql_query("SELECT * from products where brand='$brand' and category='$category' order by product ASC LIMIT $from, $max_results ");

    while (
    $i mysql_fetch_array($result))
    {
    echo 
    "<table width='100%' class='maintext'>";
    while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){


    echo 
    "results here";

    }

    echo 
    "</table>";

    The first mysql_fetch_array grabs the first product but doesn't dispay it.

    Change
    PHP Code:
    while ($i mysql_fetch_array($result))

    to
    PHP Code:
    if ($result!==false) { 
    and that should sort it out.
    Thanks Rich,

    That worked a treat - I thought the first mysql_fetch_array was the culprit - just didn't know what to do instead!

    Thanks futureweb as well.

    Brett
    Brett



Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. xml and php route on feed, page switch problem
    By ryanbof in forum TradeDoubler
    Replies: 1
    Last Post: 23-08-04, 02:20 AM
  2. PHP - No input file specified. - Problem
    By Barry in forum Programming
    Replies: 1
    Last Post: 02-08-04, 10:57 AM
  3. PHP problem with CJ product feed
    By Andy in forum Programming
    Replies: 6
    Last Post: 18-08-03, 04:53 AM
  4. PHP Script error problem
    By Edward in forum Programming
    Replies: 1
    Last Post: 16-08-03, 04:45 PM
  5. PHP Pagination
    By MorleyM in forum Programming
    Replies: 5
    Last Post: 11-08-03, 08:47 PM

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
  •  
To Top

Content Relevant URLs by vBSEO 3.5.0 RC2