Jump to content

Archived

This topic is now archived and is closed to further replies.

Bee

Pagination problem

Recommended Posts

Hi,

I have taken a tutorial about pagination. The tutorial works fine with simple test data. But when I used it in my website, it did not work. It navigates to the right page e.g. page2.php, but it does not display any more data apart from the first two records.

I went through my code several times, but I could not see the problem! It will be really helpful if anyone can identify this problem.

Thanks,
Bee

[code]<?php
    //start session
    session_start();
    
    //show table and header
    include("table_Upper_Part.php");
    include("header_SWF.php");
?>

    <form name="formCat" action="pages.php" method="post">
        <div align = "left">
          <select name="cat">
            <option>Select a category</option>
            <option value='1' selected>Books</option>
            <option value='2'>Computers</option>
            <option value='3'>Bikes</option>
            <option value='4'>Music</option>
            <option value='5'>Software</option>
            <input type='submit' value='Search'/>
          </select>
        </div>
    </form>


<?php
    include("connect.php");
    
    // If current page number, use it
    // if not, set one!
    
    if(!isset($_GET['page'])){
        $page = 1;
    } else {
        $page = $_GET['page'];
    }
    
    // Define the number of results per page
    $max_results = 2;
    
    // Figure out the limit for the query based
    // on the current page number.
    $from = (($page * $max_results) - $max_results);  
    
    //open form
    echo "<form name='checkbox1' method='post' action='basket.php'>";
    
    //get category
    $category = $_POST['cat'];
    
    
    //select database
    $sql = "Select ProductID, Name, Description, PhotoName, Price, AmountAvailable From product WHERE CategoryID=".$category." LIMIT $from, $max_results";

    $result = mysql_query($sql);

    if ($result == false) {
        echo "<B>Please select a category.</B>";
    } else {
        $count = 1;
        $item = 1;
    while($row = mysql_fetch_array($result)){
                $item = $count;
                    
                echo "<table border='0' width = '778px' cellpadding='2'>";
                echo "<tr><td colspan=4  bgcolor='#FF9900'><B>Item: " . $count . "</B></td></tr>";
                
                echo "<tr><td width = '160px'>";
                
                echo "ProductID: " . 1000 . $row["ProductID"] . "<br>";
                echo "<img src=../images/".$row["PhotoName"].">" . "</font><br>";
                echo "<B>Price on shop:</B> <S><B>£" . $row["Price"] . "</B></S><br>";
                echo "<B>Our price: £" . $row["AmountAvailable"] . "<B>";
                
                echo "</td><td>";
                echo " Name: <font color='#0000FF'> " . $row["Name"] . "</font><br><br>";
                echo " Description: " . $row["Description"] . "<br>";                         
                
                echo "<td width='10px'></td>";
                
                echo "</td><td width = '50px' cellpadding='2'>";  
                echo "<p>Add to Basket <p><input type='checkbox' name='".$checkOut.$item."'>";  
                echo "</td></tr>";
                echo "</table> <br>";
                
                
                //carry the value to the next page
                $_SESSION['$loop'] = $item; //will be used on basket.php to match the number of loops (records)
                $_SESSION['$productID'][$item] = $row["ProductID"];
                $_SESSION['name'][$item] = $row["Name"];
                $_SESSION['priceOnShop'][$item] = $row["Price"];
                $_SESSION['ourPrice'][$item] = $row["AmountAvailable"];
                
                $count++;  
            }
            
            echo "<input type='submit' value='Checkout' name='submit'>";
            echo "</form> ";
        }
    
    // Figure out the total number of results in DB:
    $argument = mysql_query("SELECT COUNT(*) as Num FROM product WHERE CategoryID=".$category."");  
    $total_results = mysql_result($argument, 0);
    
    // Figure out the total number of pages. Always round up using ceil()
    $total_pages = ceil($total_results / $max_results);
    
    // Build Page Number Hyperlinks
    echo "<center>Select a Page<br />";
    
    // Build Previous Link
    if($page > 1){
        $prev = ($page - 1);
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
    }
    
    for($i = 1; $i <= $total_pages; $i++){
        if(($page) == $i){
            echo "$i ";
            } else {
                echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
        }
    }
    
    // Build Next Link
    if($page < $total_pages){
        $next = ($page + 1);
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
    }
    echo "</center>";
    
    //close connection
    mysql_close($link);
    
    //show table lower part
    include("table_Lower_Part.php")
?>[/code]

Share this post


Link to post
Share on other sites
I found out that it was $category variable that was causing that problem. Whenever I click next or prev, the variable loses its value.

Is there any way I can use to maintain the value of the $category?

Thanks.

Share this post


Link to post
Share on other sites
[!--quoteo(post=352867:date=Mar 8 2006, 09:48 AM:name=Bee)--][div class=\'quotetop\']QUOTE(Bee @ Mar 8 2006, 09:48 AM) [snapback]352867[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Is there any way I can use to maintain the value of the $category?
[/quote]

2 options I can think of... Pass it as a GET variable in the next link. Something like this :

[code]
<a href='results.php?category=stuff&amp;page=12'>
[/code]

Or you can store it in the user's session (assuming you're using sessions) like this :

[code]
$_SESSION['category'] = $category;
[/code]

Either way will work, although I prefer the former.. :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.