I have a working script that uses a mutlidimensional array and brings in items from my database into the users cart, however the list keeps growing. I'd like to only display 2 items in the cart, and load the next two pages if the items in the cart are >2.
Here is the working piece of code that brings in the cart items.
$cartoutput = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$cartoutput = "<font>Your Cart is currently empty.</font>";
}
else{
$i=0;
foreach($_SESSION["cart_array"] as $each_item){
$i++;
$cartoutput .="<h2>Cart Item $i</h2>";
while(list($key, $value) = each($each_item)){
$cartoutput .="$key: $value <br /><br />";
}
}
}
HERE IS WHAT I HAVE TRIED: WHICH DOES NOT WORK CORRECTLY:
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) > 2){
$start = (isset($_GET['start']) ? (int)$_GET['start'] : 0); //setting the get function for pagnation
echo "<table width=\"1024\" align=\"center\" >";
echo "<tr height=\"50\"></tr>";
echo "<tr>";
$count = $_SESSION["cart_array"];
$prev = $start - 2;
if ($prev >= 0) {
echo '<td><a href="?start=' . $prev . '">Previous Items</a></td>';
}
$next = $start + 2;
if ($next < $count) {
echo '<td><a href="?start=' . $next . '">Next Items</a></td>';
}
echo "</tr>";
echo "</table>";
}
Can anyone tell me how I can limit a users items to 2 per page, and if more than 2, add a next and previous button?
Thank you