3

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

ValleyDigital
  • 1,460
  • 4
  • 21
  • 37
  • The `` tag is depreciated. – StackSlave Dec 18 '13 at 23:44
  • I know, it's complicated but I am using the Fresco browser, which it works with. That is not my problem, though. – ValleyDigital Dec 18 '13 at 23:46
  • I would leave your script as original but use an client script method to do the pagination easy and simple: [Datatables](http://datatables.net/release-datatables/examples/basic_init/zero_config.html) You just have to adapt it a bit to get it working. – Jorge Campos Dec 18 '13 at 23:54

2 Answers2

1

I think you have a small problem here. Instead of
$count = $_SESSION["cart_array"];

I think you meant to have:
$count = count($_SESSION["cart_array"]);

Joe T
  • 2,300
  • 1
  • 19
  • 31
  • Thanks Joe! It displays the next and previous buttons as it should, but it doesn't limit the number of items to only 2 at a time. – ValleyDigital Dec 18 '13 at 23:55
0
        <?php
        session_start();

        $_SESSION['cart_array_start'] = $_GET['start'] ;

        $_SESSION['cart_array'] = array("a", "b", "c", "d", "e", "f", "g", "h",         "i");


        $cartoutput = "";
        if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
            $cartoutput = "<font>Your Cart is currently empty.</font>";
        }
        else{
            $i=0;
            //set our start session to 1 if it doesn't exist.
            if (!isset($_SESSION['cart_array_start'])){
                $_SESSION['cart_array_start'] = 1 ;
            }
            $first_product = $_SESSION['cart_array_start'] ;
            //echo $first_product ;
            $last_product = $_SESSION['cart_array_start'] + 1 ;
            foreach($_SESSION["cart_array"] as $each_item){

                $i++;
                //echo $i ;
                if ($i == $first_product || $i == $last_product){
                    $cartoutput .="<h2>Cart Item $i</h2>";
                    $key = $i - 1 ;
                    $cartoutput .="$key: $each_item <br /><br />";

                }

            }
        }

        echo $cartoutput ;

        //code to start at the previous page.
        if (empty($_SESSION['cart_array_start']) || $_SESSION['cart_array_start'] <= 2)        {
            //they dont get a back button because they are on the first page.
        }else{
            $back = $_SESSION['cart_array_start'] - 2 ;
            echo "<a href=\"test.php?start=" . $back . "\">Back one page</a><br />";
        }

        //next page code.
        if (count($_SESSION['cart_array']) > 2){
            //start to display it - but lets check to see if they are on the last page         first.
            $next = $_SESSION['cart_array_start'] + 2 ;
            //give our product in cart count plus one because we want to display the         last page even if it only has 1 product.
            $products = count($_SESSION['cart_array']) + 1 ;
            if ($next > $products){
                //do nothing because nothing will be on that page.
            }else{
                //give them a next button.
                echo "<a href=\"test.php?start=" . $next . "\">next page</a>";
            }
        }
        ?>
Nazca
  • 112
  • 1
  • 7
  • It just creates a new session for you and uses that session to keep track of what product in the array they are on. it iterates through the whole array each time and only runs the $cartoutput .= part if the array key matches the iteration its on. Effectively displaying two each time. – Nazca Dec 18 '13 at 23:59
  • 1
    Thank you. I tried your code and it displays the next and previous buttons as it should, but it doesn't actually load the 3rd item. It just populates the button. Am I missing something? – ValleyDigital Dec 19 '13 at 00:10
  • My Anchor tags were different than yours. Did you change those in your code? remove page.php from the anchor tags I posted for the buttons. – Nazca Dec 19 '13 at 00:14
  • I updated the code. It works fine on my server. I had a typo of $_SESSION['cart_arrat_start'] that I fixed. – Nazca Dec 19 '13 at 00:36
  • Dont forget to change the anchor tags again. I had them as test.php as that's what it is on my server. – Nazca Dec 19 '13 at 00:45