Pergunta

I am trying to paginate my PHP page by ten results per page. Currently the page shows all results at once. I did look up some tutorials on pagination but I cannot seem to figure out how to implement them into the current PHP page. The PHP page is called view_cart.php and it essentially retrieves the cached products that were added by the user via a shopping cart page and displays them on this page. I am just looking for a few hints to point me in the right direction, not for someone to do my work for me.

This is the view_cart.php page code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
session_start();
include_once("config.php");
    if(isset($_SESSION["products"]))
    {
        $total = 0;
        echo '<form method="post" action="PAYMENT-GATEWAY">';
        echo '<ul>';
        $cart_items = 0;
        foreach ($_SESSION["products"] as $cart_itm)
        {
           $product_code = $cart_itm["code"];
           $queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code'";
           $results = mssql_query($queryy, $mysqli);
           $obj = mssql_fetch_object($results);

            echo '<li class="cart-itm">';
            echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">&times;</a></span>';
            echo '<div class="p-price">'.$currency.$obj->price.'</div>';
            echo '<div class="product-info">';
            echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
            echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
            echo '<div>'.$obj->product_desc.'</div>';
            echo '</div>';
            echo '</li>';
            $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
            $total = ($total + $subtotal);

            echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
            echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
            echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
            echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
            $cart_items ++;

        }
        echo '</ul>';
        echo '<span class="check-out-txt">';
        echo '<strong>Total : '.$currency.$total.'</strong>  ';
        echo '</span>';
        echo '</form>';
        echo '<a href="checkout.php">Checkout</a>';

    }else{
        echo 'Your Cart is empty';
    }


?>
<?php
date_default_timezone_set('America/Edmonton');
?>
<?php echo date("D M d, Y G:i a"); ?>
</body>
</html>

This is the config.php page code:

<?php
$mysqli = mssql_connect('gd','Gad','Rdgaf!');  
$objConnectee = mssql_select_db('Gdaddf',$mysqli ); 
?>

Thank you for any help. All help is appreciated.

Foi útil?

Solução

  1. You will have url parameters: start and count.
  2. Use them in the sql query, like LIMIT $_GET['start'], $_GET['count']
  3. The last thing is to render links to the "Next" and "Previous" page. (and to "First", "Last", 1,2,3,4....). Those links will include parameters start and count.

For example, the "Next" link will contain url with parameters:

"view_cart.php?start=" . ($_GET['start'] + $_GET['count']) . "&count=" . $_GET['count']

Beware of the total record count to not overflow.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top