Not displaying the products in the cart

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 2238 Views - Last Post: 23 January 2014 - 09:49 AM Rate Topic: -----

#1 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Not displaying the products in the cart

Posted 23 January 2014 - 05:54 AM

This is my AddToCart

<?php
   session_start();

   // get the product id
   $DVDID = isset($_GET['DVDID']) ? $_GET['DVDID'] : "";
   $name = isset($_GET['NameOfTheDVD']) ? $_GET['NameOfTheDVD'] : "";

   /* 
    * check if the 'cart' session array was created
    * if it is NOT, create the 'cart' session array
 . */
   if(!isset($_SESSION['cart'])){
     $_SESSION['cart'] = array();
    }

   // check if the item is in the array, if it is, do not add
   if(in_array($DVDID, $_SESSION['cart'])){
      // redirect to product list and tell the user it was added to cart
      header('Location: shop.php?action=exists&DVDID' . $DVDID . '&name=' . $name);
    }

     // else, add the item to the array
    else{
    array_push($_SESSION['cart'], $DVDID);
    
    // redirect to product list and tell the user it was added to cart
    header('Location: shop.php?action=add&DVDID' . $DVDID . '&name=' . $name);
    }

?>


This is my basket where the products are meant to be displayed but I have no idea why it is not displaying

<?php
	    $name = isset($_GET['NameOfTheDVD']) ? ($_GET['NameOfTheDVD']): "";
	    $DVDID = isset($_GET['DVDID']) ? ($_GET['DVDID']) : "";
	    $Quantity =isset($_GET['Quantity']) ? ($_GET['Quantity']) : "";
	 
	   $action = isset($_GET['action']) ? $_GET['action'] : "";
    
    if($action=='removed'){
        echo "<div>" . $_GET['NameOfTheDVD'] . " was removed from cart.</div>";
    }
    
    if(isset($_SESSION['cart'])){
        $ids = "";
        foreach($_SESSION['cart'] as $DVDID){
           echo $ids = $ids . $DVDID . ",";
			
        }
        
        // remove the last comma
        $ids = rtrim($ids, ',');
        
        require "connect.php";

        $query = "SELECT DVDID, NameOfTheDVD, Quantity FROM DVD ";
        $stmt = $dbhandle->prepare( $query );
        $stmt->execute();

        $num = $stmt->rowCount();

        if($num>0){
            echo "<table border='0'>";//start table
            
                // our table heading
                echo "<tr>";
                    echo "<th class='textAlignLeft'>Product Name</th>";
                    echo "<th>Quantity</th>";
                    echo "<th>Action</th>";
                echo "</tr>";
                
                //also compute for total price
                $totalQuantity = 0;
                
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                    
                    
                    $totalQuantity += $Quantity;
                    
                    //creating new table row per record
                    echo "<tr>";
                        echo "<td>{$name}</td>";
                        echo "<td class='textAlignRight'>{$Quantity}</td>";
                        echo "<td class='textAlignCenter'>";
                            echo "<a href='RemoveFromCart.php?id={$DVDID}&name={$name}' class='customButton'>";
                                echo "<img src='images/remove-from-cart.png' title='Remove from cart' />";
                            echo "</a>";
                        echo "</td>";
                    echo "</tr>";
                }
                
                echo "<tr>";
                    echo "<th class='textAlignCenter'>Total Price</th>";
                    echo "<th class='textAlignRight'>{$totalQuantity}</th>";
                    echo "<th></th>";
                echo "</tr>";
                
            echo "</table>";
            echo "<br /><div><a href='#' class='customButton'>Checkout</a></div>";
        }else{
            echo "<div>No products found in your cart. :(/></div>";
        }

    }else{
        echo "<div>No products in cart yet.</div>";
    }
    ?>


Any help would appreciated thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Not displaying the products in the cart

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4219
  • View blog
  • Posts: 13,353
  • Joined: 08-June 10

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:19 AM

and which of the two messages do you get?

several other problems:
- rowCount() is to be used on INSERT/UPDATE/DELETE
- there is no need for rowCount() *(see below)
- PDO works best with a foreach() loop
- you can query the total quantity in SQL
- $Quantity (line #4) is not guaranteed to be something sensible, you need to check this user input for validaty (e.g. by using filter functions)
- it does not hurt to do 2 queries, if this provides better use on the PHP side


*
$out_string = "";

foreach ($query as $row)
{
    $out_string .= vsprintf($template, $row);
}

if (strlen($out_string))
{
    $out_string = $prepend_string . $out_string . $append_string;
}

echo $out_string;

Was This Post Helpful? 0
  • +
  • -

#3 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:21 AM

Well, I am not getting any errors, it is just not displaying anything
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4219
  • View blog
  • Posts: 13,353
  • Joined: 08-June 10

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:23 AM

like a blank page?
Was This Post Helpful? 0
  • +
  • -

#5 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:25 AM

yh kind of with a message saying "No products found in the cart yet"
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4219
  • View blog
  • Posts: 13,353
  • Joined: 08-June 10

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:27 AM

yupp, rowCount() is not meant to be used on SELECTs. thatís even said in the Manual. and Iíve shown you an example how to do it without rowCount().
Was This Post Helpful? 0
  • +
  • -

#7 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:29 AM

I removed this code as I thought it was not doing anything because I am not Inserting or updating

$num = $stmt->rowCount();
29
 
30
        if($num>0){
31
            echo "<table border='0'>";//start table
32
             
33
                // our table heading
34
                echo "<tr>";
35
                    echo "<th class='textAlignLeft'>Product Name</th>";
36
                    echo "<th>Quantity</th>";
37
                    echo "<th>Action</th>";
38
                echo "</tr>";
39
                 
40
                //also compute for total price
41
                $totalQuantity = 0;
42
                 
43
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
44
                     
45
                     
46
                    $totalQuantity += $Quantity;
47
                     
48
                    //creating new table row per record
49
                    echo "<tr>";
50
                        echo "<td>{$name}</td>";
51
                        echo "<td class='textAlignRight'>{$Quantity}</td>";
52
                        echo "<td class='textAlignCenter'>";
53
                            echo "<a href='RemoveFromCart.php?id={$DVDID}&name={$name}' class='customButton'>";
54
                                echo "<img src='images/remove-from-cart.png' title='Remove from cart' />";
55
                            echo "</a>";
56
                        echo "</td>";
57
                    echo "</tr>";
58
                }
59
                 
60
                echo "<tr>";
61
                    echo "<th class='textAlignCenter'>Total Price</th>";
62
                    echo "<th class='textAlignRight'>{$totalQuantity}</th>";
63
                    echo "<th></th>";
64
                echo "</tr>";
65
                 
66
            echo "</table>";
67
            echo "<br /><div><a href='#' class='customButton'>Checkout</a></div>";
68
        }else{
69
            echo "<div>No products found in your cart. :(/>/></div>";
70
        }



and now I am getting a blank page.

yes and I saw that example
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4219
  • View blog
  • Posts: 13,353
  • Joined: 08-June 10

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:33 AM

if you removed the above code block, what do you expect to be printed then?
Was This Post Helpful? 0
  • +
  • -

#9 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:37 AM

I did not remove everythign from the block of code, I just kept in the table bit of code but I don't understand what do i need. DO i need some kind of if loop that check if the cart is empty?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4219
  • View blog
  • Posts: 13,353
  • Joined: 08-June 10

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:43 AM

Quote

DO i need some kind of if loop that check if the cart is empty?

that is what I was trying to show you with the example. if you just loop over the results (no matter how many there are) and put it into a variable, the variable will only stay empty if and only if the DB result was empty. alternately ask the DB for the cartís total quantity, if and only if that is 0, the cart is empty.
Was This Post Helpful? 0
  • +
  • -

#11 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not displaying the products in the cart

Posted 23 January 2014 - 06:47 AM

I dont quite understand can you explain it properly please?
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4219
  • View blog
  • Posts: 13,353
  • Joined: 08-June 10

Re: Not displaying the products in the cart

Posted 23 January 2014 - 07:04 AM

as simple as I can imagine:
$array_i_dont_know_the_length_of = array();

$compiled_output_string = "";

foreach($array_i_dont_know_the_length_of as $element)
{
  $compiled_output_string .= $element;
}

if (strlen($compiled_output_string) > 0)
{
  echo "Yay! there must have been some elements in the array.";
}
else
{
  echo "Booo! the array was empty.";
}


Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3803
  • View blog
  • Posts: 13,812
  • Joined: 08-August 08

Re: Not displaying the products in the cart

Posted 23 January 2014 - 07:10 AM

Why are you not using objects?
Was This Post Helpful? 0
  • +
  • -

#14 mfredy92   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 144
  • Joined: 09-May 13

Re: Not displaying the products in the cart

Posted 23 January 2014 - 07:15 AM

It was easier for me to use functions rather than objects because I managed to create the arrays for AddToCart, my only problem is that it is not diaplaying the products
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3803
  • View blog
  • Posts: 13,812
  • Joined: 08-August 08

Re: Not displaying the products in the cart

Posted 23 January 2014 - 07:21 AM

Huh? You know that objects can have functions, right? And one of those functions could be used to display itself? That way if you got it working for one element it would work for all. Turning $_SESSION['cart'] into a multi dimensional array is hard.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3