Jump to content

pagination problem


oracle765

Recommended Posts

 

if (!(isset($pagenum)))
{
    $pagenum = 1;
}

 

$pagenum is never defined before this - so it will always be 1. I think you meant to use something like:

 

if(!(isset($_GET['pagenum']))
{
    $pagenum = 1;
}
else
{
    $pagenum = intval($_GET['pagenum']);
}

 

That's just the first thing that jumped out at me. The code is a bit unorganized

Link to comment
Share on other sites

Try this:

(Note: I define all the output in a variable called $output. This way you can put the 'logic' at the top of your page and just echo the output into the appropriate place on the page. Makes the pages easier to manage and maintain)

 

 

<?php

//This is the number of results displayed per page
$page_rows = 4;

// Connects to your Database
mysql_connect("localhost", "secret", "secret") or die(mysql_error());
mysql_select_db("mydatabase") or die(mysql_error());
 
//Here we count the number of results
$query = "SELECT COUNT(*) FROM merchant";
$result = mysql_query($query) or die(mysql_error());
$total_rows = mysql_result($result, 0);

//This tells us the page number of our last page
$total_pages = ceil($total_rows/$page_rows);  

//This checks to see if there is a page number. If not, it will set it to page 1
$pagenum = (isset($_GET['pagenum'])) ? intval($_GET['pagenum']) : 1;
//this makes sure the page number isn't below one, or more than our maximum pages
$pagenum = max(1, $pagenum);
$pagenum = min($total_rows, $pagenum);

//This sets the limit to get the records for the selected page
$limit = ($pagenum - 1) * $page_rows;
//This is your query again, the same one... the only difference is we add LIMIT to it
$query = "SELECT * FROM merchant ORDER BY ID ASC LIMIT {$limit}, {$page_rows}";
$result = mysql_query($query) or die(mysql_error());

//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);   
$results = $mysqli->query("SELECT * FROM merchant $max ORDER BY ID ASC");

//This is where you display your query results
while($row = mysql_fetch_assoc($result))
{
    $output  = "<div class=\"item\">\n";
    $output .= "<form method=\"post\" action=\"Cart_update.php\">\n";
    $output .= "<div class=\"product-thumb\"><img src=\"{$row['url']}\" width=\"20\" height=\"50\"></div>\n";
    $output .= "<div class=\"product-content\"><h3>{$row['name']}</h3>\n";
    $output .= "  <div class=\"product-desc\">{$row['product']}</div>\n";
    $output .= "  <div class=\"product-info\">Price {$row['currency']}{$row['price']} <button class=\"add_to_cart\">Add To Cart</button></div>\n";
    $output .= "</div>\n";
    $output .= "<input type=\"hidden\" name=\"product_code\" value=\"{$row['ID']}\" />\n";
    $output .= "<input type=\"hidden\" name=\"type\" value=\"add\" />\n";
    $output .= "<input type=\"hidden\" name=\"return_url\" value=\"{$row['current_url']}\" />\n";
    $output .= "</form>\n";
    $output .= "</div>\n";
}
 
// This shows the user what page they are on, and the total number of pages
$output .= " --Page $pagenum of $total_pages-- <p>";

//Create var for pagination URL
$pageURL = "{$_SERVER['PHP_SELF']}?pagenum=";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing.
//If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum > 1)
{
    $previous = $pagenum-1;
    $output .= " <a href='{$pageURL}1'> <<-First</a> ";
    $output .= " <a href='{$pageURL}{$previous}'> <-Previous</a> ";
}

//just a spacer
$output .= " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum < $total_pages)
{
    $next = $pagenum+1;
    echo " <a href='{$pageURL}{$next}>Next -></a> ";
    echo " <a href='{$pageURL}{$total_pages}'>Last ->></a> ";
}
Link to comment
Share on other sites

ive tried that but it does not show any output now from the DB it just shows the link saying >>Last and when I click it nothing

 

 

Well, two things. 1. I made a mistake in not converting those last two echo statements to have the content concatenated to the $output variable. and 2. You failed to echo the $output variable on your page. Please read my "note" again. Lastly, I do not provide the code with any guarantee it will work out-of-the-box. I don't have your database to test against, so I don't waste my time debugging the code. If you want that kind of help then post in the freelance forum.

Link to comment
Share on other sites

<?php
$currency = '$';
$db_username = 'secret';
$db_password  = 'secret';
$db_name = 'mydatabase';
$db_host = 'localhost';
$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);
?>

hi above is the config file

 

there is only 1 test table within the database called "merchant" which consists of

 

ID,Item,Price,Product and Description

 

hope this helps

thanks

Link to comment
Share on other sites

oh and no worries I am not sure what you mean by echoing the output statement and concatenating it, sorry but its all still a bit new to me

 

In the code I provided, instead of echoing the output to the page, I simply added the content to a variable names $output

 

    $output  = "<div class=\"item\">\n";
    $output .= "<form method=\"post\" action=\"Cart_update.php\">\n";

 

The reason for this is to make the code more modular and flexible. You should be able to separate the logic (i.e. PHP code) from the presentation (i.e. the HTML). That way you can create a separate file to generate the pagination results from the actual output (just put a single echo). That way you can just include the PHP file to create the pagination results. This reduces the amount of lines of code in any single file making it easier to debug your code and to reuse portions. For example, you have a lot of code for things such as the menu and footer. Those should be taken out of the main page script and put into separate files that you include() in the main script. That way you can include the same files in all your pages and only have to maintain the content in one place. So, thin about when you may want to change the footer content for your whole site. You would want to have to track down dozens of different pages to copy/paste the same content. Just have the content defined once and used many times. Then you would only have to edit one file to update the footer for your entire site.

 

Here is a rewrite of your whole page with the code I provided above. Plus, I also modified the code to create the cart contents. All of the PHP code it at the top of the script. You could take it out and put it into separate files if you wish. Again, I did not test the code since I have not taken the time to set up a database with test data. So, there may be a few typos to fix.

 

 

<?php
session_start();

include_once('includes/config.php');

######################################################
###### Start Logic to create pagination results ######
######################################################

$pageResults = '';

//This is the number of results displayed per page
$page_rows = 4;

// Connects to your Database
mysql_connect("localhost", "secret", "secret") or die(mysql_error());
mysql_select_db("mydatabase") or die(mysql_error());
 
//Here we count the number of results
$query = "SELECT COUNT(*) FROM merchant";
$result = mysql_query($query) or die(mysql_error());
$total_rows = mysql_result($result, 0);

//This tells us the page number of our last page
$total_pages = ceil($total_rows/$page_rows);  

//This checks to see if there is a page number. If not, it will set it to page 1
$pagenum = (isset($_GET['pagenum'])) ? intval($_GET['pagenum']) : 1;
//this makes sure the page number isn't below one, or more than our maximum pages
$pagenum = max(1, $pagenum);
$pagenum = min($total_rows, $pagenum);

//This sets the limit to get the records for the selected page
$limit = ($pagenum - 1) * $page_rows;
//This is your query again, the same one... the only difference is we add LIMIT to it
$query = "SELECT * FROM merchant ORDER BY ID ASC LIMIT {$limit}, {$page_rows}";
$result = mysql_query($query) or die(mysql_error());

//current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);   
$results = $mysqli->query("SELECT * FROM merchant $max ORDER BY ID ASC");

//This is where you display your query results
while($row = mysql_fetch_assoc($result))
{
    $pageResults .= "<div class=\"item\">\n";
    $pageResults .= "<form method=\"post\" action=\"Cart_update.php\">\n";
    $pageResults .= "<div class=\"product-thumb\"><img src=\"{$row['url']}\" width=\"20\" height=\"50\"></div>\n";
    $pageResults .= "<div class=\"product-content\"><h3>{$row['name']}</h3>\n";
    $pageResults .= "  <div class=\"product-desc\">{$row['product']}</div>\n";
    $pageResults .= "  <div class=\"product-info\">Price {$row['currency']}{$row['price']} <button class=\"add_to_cart\">Add To Cart</button></div>\n";
    $pageResults .= "</div>\n";
    $pageResults .= "<input type=\"hidden\" name=\"product_code\" value=\"{$row['ID']}\" />\n";
    $pageResults .= "<input type=\"hidden\" name=\"type\" value=\"add\" />\n";
    $pageResults .= "<input type=\"hidden\" name=\"return_url\" value=\"{$row['current_url']}\" />\n";
    $pageResults .= "</form>\n";
    $pageResults .= "</div>\n";
}
 
// This shows the user what page they are on, and the total number of pages
$pageResults .= " --Page $pagenum of $total_pages-- <p>";

//Create var for pagination URL
$pageURL = "{$_SERVER['PHP_SELF']}?pagenum=";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing.
//If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum > 1)
{
    $previous = $pagenum-1;
    $pageResults .= " <a href='{$pageURL}1'> <<-First</a> ";
    $pageResults .= " <a href='{$pageURL}{$previous}'> <-Previous</a> ";
}

//Add a spacer
$pageResults .= " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum < $total_pages)
{
    $next = $pagenum+1;
    $pageResults .= " <a href='{$pageURL}{$next}>Next -></a> ";
    $pageResults .= " <a href='{$pageURL}{$total_pages}'>Last ->></a> ";
}
######################################################
######  End Logic to create pagination results  ######
######################################################

######################################################
######   Start Logic to create cart contents    ######
######################################################
if(isset($_SESSION["products"]))
{
    $cartContents = '';
    $total = 0;
    
    foreach ($_SESSION["products"] as $cart_itm)
    {
        //echo '<li class="cart-itm">';
        $cartContents .= '<span class="remove-itm"><a href="Cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">−</a></span>';
        $cartContents .= '<span class="add-itm"><a href="Cart_update.php?addp='.$cart_itm["code"].'&return_url='.$current_url.'">&plus;</a></span>';
        $cartContents .= '<h3>'.$cart_itm["name"].'</h3>';
        $cartContents .= '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
        $cartContents .= '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
        $cartContents .= '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
        //  echo '</li>';
        $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
        $total = ($total + $subtotal);
    }
     
    $cartContents .= '<span class="check-out-txt">';
    $cartContents .= '<strong>Total : '.$currency.$total.'</strong> ';
    $cartContents .= '<a href="View_cart.php"><img src="images/cart.gif" alt="" width="18" height="19" class="cart">Shopping Cart</a>';
    $cartContents .= '</span>';
}
######################################################
######    End Logic to create cart contents     ######
######################################################
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Compare and Choose</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<link rel="stylesheet" type="text/css" href="style.css" />
<style type="text/css">
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
div#pagination_controls{font-size:21px;}
div#pagination_controls > a{ color:#06F; }
div#pagination_controls > a:visited{ color:#06F; }
</style>
<script type="text/javascript">
function transparent(im)
   {
   if (!im.transparented && (/\.png/.test(im.src)))
      {
      im.transparented = 1;
      var picture = im.src;
      var w = im.width;
      var h = im.height;
      im.src = "images/spacer.gif";
      im.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod='scale', src='" + picture + "');";
      im.width = w;
      im.height = h;
      }
   return "transparent";
   }
</script>
</head>

<body>

    <div id="content">
        <div id="header">
            <ul class="top_menu">
                <li><a href="index.html" class="active">Home</a></li>                                                                                                                                                                                                            
                <li><a href="#">How it works</a></li>
                <li><a href="#">Travel</a></li>
                <li><a href="#">Goods</a></li>
                <li><a href="#">Insurance</a></li>
                <li><a href="#">Services</a></li>
                <li><a href="#">Hire</a></li>
                <li><a href="#">About us</a></li>
                <li><a href="#">Bookmark</a></li>
            </ul>
            <ul class="top_menu">
                <li><a href="#">My Account</a></li>
                <li><a href="#">Track Order</a></li>
                <li><a href="#">Order</a></li>
            </ul>
            <div id="search">
                Search<br />                                                                                                                                                                                                                            
                <form action="#">
                    <select>
                        <option>Category</option>
                        <option>Travel</option>
                        <option>Goods</option>
                        <option>Insurance</option>
                    </select>
                    <select>
                    <option>other</option>
                        <option>Services</option>
                        <option>Hire</option>
                        <option>Bookmark</option>
                    </select>
                    <a href="#"><img src="images/ok.jpg" alt="setalpm" width="44" height="23" /></a>        
                </form>
            </div>
            <img src="images/tel.jpg" alt="" width="181" height="13" class="tel" />
        </div>
        <div id="wrapper">
            <div id="background">
                <div id="left">
                    <h4 class="title1">All categories</h4>
                    <ul id="list">
                        <li><a href="#" class="type1">Home</a></li>
                        <li><a href="#" class="type2">How it works</a></li>
                        <li><a href="#" class="type2">Travel</a></li>
                        <li><a href="#" class="type2">Goods</a></li>
                        <li><a href="#" class="type1">Insurance</a></li>
                        <li><a href="#" class="type2">Services</a></li>
                        <li><a href="#" class="type1">Hire</a></li>
                        <li><a href="#" class="type2">About us</a></li>
                        <li>
                            <a href="#" class="type3">Test</a>
                            <ul id="inside">
                                <li><a href="#">Test</a></li>
                                <li><a href="#">Test</a></li>
                                <li><a href="#">Test</a></li>
                                <li><a href="#">Test</a></li>
                            </ul>
                        </li>
                        <li><a href="#" class="type2">Test</a></li>
                        <li><a href="#" class="type2">Test</a></li>
                        <li><a href="#" class="type1">Test</a></li>
                        <li><a href="#" class="type2">Test</a></li>
                    </ul>
                    <h4 class="title2">Test</h4>
                    <div class="news">
                        <span>Xmas day</span>
                        <p>Test item to show any promotions that an affilaite might have they can be shown around this area to the customer on specific dates. </p>
                        <a href="#" class="more">read more</a>
                    </div>
                    <div class="news">
                        <span>17 november</span>
                        <p>A different older promotion to be shown here. </p>
                        <a href="#" class="more">read more</a>
                    </div>
                    <div class="news">
                        <span>05 november</span>
                        <p>Even older promotions that an affilaite might have they can be shown around this area to the customer on specific dates. </p>
                        <a href="#" class="more">read more</a>
                    </div>
                    <h4 class="title3">Currencies</h4>
                    <div class="currencies">
                        <select>
                            <option>AUD</option>
                            <option>GBP</option>
                        </select>
                    </div>
                </div>
                <div id="center">
                    <div id="discount">
                        <img src="images/discount.png" alt="" width="121" height="119" border="0" usemap="#Map" class="transparent" />
                        <map name="Map">
                          <area shape="circle" coords="61,57,55" href="#">
                        </map>
                    </div>
                    <div id="photo">
                        <img src="images/tpart.png" alt="" width="49" height="23" class="transparent" /><br />
                        <div class="text">
                            <p><span>test</span> - test details, this is where we need to show different marketed product options for the compare. </p>
                            <p>Another test<br /> lets make sure we double check everything here for the page layout,<br /> test test test test test<br /> out put test output<br /> plain english, </p>
                        </div>
                    </div>
                    <div id="items">
                    
                        <div class="item">
                        <?php echo $pageResults; ?>
                        </div>

                    </div>
                
                <div id="right">
                    <div class="bag">
                        <h4 class="title2"><img src="images/cart.gif" alt="" width="18" height="19" class="cart" />Shopping Cart</h4>
                        <?php echo $cartContents; ?>
                        <div class="entry">
                        <form action="#">
                            Login<br />
                            <input type="text" /><br />
                            Password<br />
                            <input type="text" /><br />
                            <a href="#" class="forgot">Forgot password</a>
                        </form>
                    </div>
                    <p class="below"><a href="#">Register</a> <a href="#"><img src="images/enter.jpg" alt="" width="52" height="23" /></a></p>
                    <div class="users">
                        <p>Active Users:</p>
                        <p><font>257</font></p>
                    </div>
                    <h4 class="title3">Search by brand</h4>
                    <ul class="brands">
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                        <li class="color"><a href="#">test</a></li>
                        <li><a href="#">test</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    </div>
    <div id="footer">
        <div>
            <ul>
                <li><a href="index.html">Home</a>|</li>
                <li><a href="#">How it works</a>|</li>
                <li><a href="#">Travel</a>|</li>
                <li><a href="#">Goods</a>|</li>
                <li><a href="#">Insurance</a>|</li>
                <li><a href="#">Services</a>|</li>
                <li><a href="#">Hire</a>|</li>
                <li><a href="#">About us</a>|</li>
                <li><a href="#">Bookmark</a>|</li>
                <li><a href="#">My Account</a>|</li>
                <li><a href="#">Track Order</a>|</li>
                <li> </li>
            </ul>
            <p id="bft">Copyright ©<a href="http://www.compareandchoose.com.au">C&C</a>. All rights reserved.</p>                                                                                                                                                                                                                                                                                                                                            
        </div>
    </div>
</body>
</html>
Link to comment
Share on other sites

 


there is only 1 test table within the database called "merchant" which consists of

 

ID,Item,Price,Product and Description

 

Really? That's not the case based upon your code. In your code you are referencing fields from that table as follows: url, name, product, price, and ID. The only one that is the same is 'ID'. Note: "Product" is not the same as "product".

Link to comment
Share on other sites

hey physco thanks for that it works great.

 

just one thing I have notice which I am confused about is which DB connection is it using or is it using both as I am including the config file as shown above but also specifying the connection within the webpage code?

 

as you state it is better to take out as much as possible from the actual page then I only have to make any changes in one place.

 

 

thanks again you have been more than helpful in resolving my problem

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.