Jump to content


Photo

Pagination problem


  • Please log in to reply
2 replies to this topic

#1 Bee

Bee
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 08 March 2006 - 01:22 PM

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

<?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")
?>


#2 Bee

Bee
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 08 March 2006 - 02:48 PM

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.

#3 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 08 March 2006 - 05:41 PM

[!--quoteo(post=352867:date=Mar 8 2006, 09:48 AM:name=Bee)--][div class=\'quotetop\']QUOTE(Bee @ Mar 8 2006, 09:48 AM) View Post[/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 :

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

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

$_SESSION['category'] = $category;

Either way will work, although I prefer the former.. :)
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users