Page 1 of 1

Add products from different pages

Posted: Mon Jan 30, 2012 8:47 pm
by v8don
Hi guys, I am having a slight problem with a php script shopping cart. Well, what I want to accomplish is to have products from different pages go to the same shopping cart with no script errors which is my problem. For example, the products page will show only the icon and quantity but no unit price, description, nor total. However, on the shirts page it will show all the details of that purchase. The following script has two pages. Product page and a Product page 2 concide with Item_Id and Item_Id_2 which are respectively two pages.

I get these errors when I run the code and I know its pulling from product page NOT product page 2. Everything goes to the shopping cart from product page 2 with its coinciding Item_Id_2 but not product page and its coinciding Item_Id.


Any help will be highly appreciated!!!!


Notice: Undefined variable: price in C:\xampp\htdocs\onlinestore\cart2.php on line 136

Notice: Undefined variable: price in C:\xampp\htdocs\onlinestore\cart2.php on line 141

Notice: Undefined variable: item_id in C:\xampp\htdocs\onlinestore\cart2.php on line 146

Notice: Undefined variable: product_name in C:\xampp\htdocs\onlinestore\cart2.php on line 146

Notice: Undefined variable: product_name in C:\xampp\htdocs\onlinestore\cart2.php on line 147

Notice: Undefined variable: details in C:\xampp\htdocs\onlinestore\cart2.php on line 149

Notice: Undefined variable: price in C:\xampp\htdocs\onlinestore\cart2.php on line 150

Code: Select all



<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section  (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])){
    $pid=$_POST['pid'];
	
	$wasFound = false;
	$i = 0;
	// If the cart session variable is not set or cart array is empty
	if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
	    // RUN IF THE CART IS EMPTY OR NOT SET
		$_SESSION["cart_array"]=array(0 => array("item_id" => $pid, "item_id_2" => $pid, "quantity" => 1));
	} else {
		// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
		foreach ($_SESSION["cart_array"] as $each_item) { 
		      $i++;
		      while (list($key, $value) = each($each_item)) {
				  if ($key == "item_id" && "item_id_2" && $value == $pid) {
					  // That item is in cart already so let's adjust its quantity using array_splice()
					  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "item_id_2" => $pid, "quantity" => $each_item['quantity'] + 1)));
					  $wasFound = true;
				  } // close if condition
		      } // close while loop
	       } // close foreach loop
		   if ($wasFound == false) {
			   array_push($_SESSION["cart_array"], array("item_id" => $pid, "item_id_2" => $pid, "quantity" => 1));
		   }
	}
	header("location: cart2.php"); 
    exit();
}
?>

<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 2 (if user chooses to empty their shopping cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_GET['cmd'])&& $_GET['cmd'] == "emptycart") {
    unset($_SESSION["cart_array"]);
}
?>


<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 3 (if user chooses to adjust their item quantity)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
    //execute code
	$item_to_adjust = $_POST['item_to_adjust'];
	$quantity = $_POST['quantity'];
	
	If($quantity >=100){$quantity = 99;}
	If($quantity < 1) {$quantity = 1;}
	
	
	
	$i = 0;
	foreach ($_SESSION["cart_array"] as $each_item) { 
		      $i++;
		      while (list($key, $value) = each($each_item)) {
				  if ($key == "item_id" && "item_id_2" && $value == $item_to_adjust) {
					  // That item is in cart already so let's adjust its quantity using array_splice()
					  array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id", "item_id_2" => $item_to_adjust, "quantity" => $quantity)));
					  
				  } // close if condition
		      } // close while loop
	       } // close foreach loop
	
	
}
?>


 



<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 4 (if user wants to remove an item from cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
    // Access the array and run code to remove that array index
 	$key_to_remove = $_POST['index_to_remove'];
	if (count($_SESSION["cart_array"]) <= 1) {
		unset($_SESSION["cart_array"]);
	} else {
		unset($_SESSION["cart_array"]["$key_to_remove"]);
		sort($_SESSION["cart_array"]);
	
	}
}
?>





<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 5 (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
if(!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"])<1){
    $cartOutput="<h2 align = 'center'> Your shopping cart is empty </h2>";
} else {
	$i = 0;
	foreach($_SESSION["cart_array"] as $each_item) { 
			  
			  $item_id_2 = $each_item ['item_id_2'];
			 
			  $sql = mysql_query("SELECT * FROM shirts WHERE id_2 = '$item_id_2' LIMIT 1");
			  while ($row = mysql_fetch_array($sql)) {
				$product_name = $row["product_name"];
				$price = $row ["price"];
				$details = $row ["details"];
			  
			  }
			  
			   
			  
			  
			  $pricetotal = $price * $each_item['quantity'];
			  $cartTotal = $pricetotal + $cartTotal;
		
			  
			  
			  $pricetotal = number_format($price * $each_item['quantity'],2);
			  $cartTotal = number_format($cartTotal,2);
			 
			  //Dynamic table row assembly
			  $cartOutput.= '<tr>';
			  $cartOutput.= '<td><a href = "product2.php?id=' . $item_id_2 . '">' . $product_name . '</a><br/>
			  <img src ="inventory_images/' . $item_id_2 . '.jpg"alt="' . $product_name . '"width="40"height="52"border="1"/> </td>';
			  
			  $cartOutput.= '<td>'.$details. '</td>';
			  $cartOutput.= '<td>$'.$price. '</td>';
			  $cartOutput.= '<td><form action = "cart2.php" method = "post">
			
			  <input name = "quantity" type = "text" value = "' . $each_item['quantity'] . '" size = "1" maxlength = "2" />
			  <input name = "adjustBtn' .$item_id_2. '" type = "submit" value = "Update" />
			  <input name = "item_to_adjust" type = "hidden" value = "' .$item_id_2. '"/>
			  </form></td>';
			  
			  
			  $cartOutput.= '<td>$' . $pricetotal . '</td>';
			  $cartOutput.= '<td><form action = "cart2.php" method = "post"><input name = "deleteBtn' . $item_id_2 .'" type = "submit" value = "Remove"/><input name = "index_to_remove"
			  type = "hidden" value = "' . $i . '" /></form></td>';
			  $cartOutput.= '</tr>';
			  $i++;
			  
		}
		
		$cartTotal = "<div align = 'right'>Cart Total: $".$cartTotal." USD </div>";
}
?>





Re: Add products from different pages

Posted: Mon Jan 30, 2012 8:59 pm
by Celauran
You're getting the notices because you're trying to use variables that may not have been defined. The solution is remarkably simple; declare your variables and assign them default values before trying to use them.

Re: Add products from different pages

Posted: Mon Jan 30, 2012 9:42 pm
by twinedev
More importantly in your code, from what i can figure is the trouble area is this

Code: Select all

$sql = mysql_query("SELECT * FROM shirts WHERE id_2 = '$item_id_2' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
    $product_name = $row["product_name"];
    $price = $row ["price"];
    $details = $row ["details"];
}

$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;

$pricetotal = number_format($price * $each_item['quantity'],2);
$cartTotal = number_format($cartTotal,2);

//Dynamic table row assembly
$cartOutput.= '<tr>';
$cartOutput.= '<td><a href = "product2.php?id=' . $item_id_2 . '">' . $product_name . '</a><br/>
    <img src ="inventory_images/' . $item_id_2 . '.jpg"alt="' . $product_name . '"width="40"height="52"border="1"/> </td>';

$cartOutput.= '<td>'.$details. '</td>';
$cartOutput.= '<td>$'.$price. '</td>';
$cartOutput.= '<td><form action = "cart2.php" method = "post">
    <input name = "quantity" type = "text" value = "' . $each_item['quantity'] . '" size = "1" maxlength = "2" />
    <input name = "adjustBtn' .$item_id_2. '" type = "submit" value = "Update" />
    <input name = "item_to_adjust" type = "hidden" value = "' .$item_id_2. '"/>
    </form></td>';

$cartOutput.= '<td>$' . $pricetotal . '</td>';
$cartOutput.= '<td><form action = "cart2.php" method = "post"><input name = "deleteBtn' . $item_id_2 .'" type = "submit" value = "Remove"/><input name = "index_to_remove"
    type = "hidden" value = "' . $i . '" /></form></td>';
$cartOutput.= '</tr>';
$i++;
This whole block (except for the first line) should be wrapped with an iff to make sure the query returned something. Also, since you are doing a LIMIT 1 in the query, there is no need to do a loop on it. So the query is most likely failing to return any rows, so the inital variables used for $cartOuput (and for variables calculated for it).

So try the following:

Code: Select all

$sql = mysql_query("SELECT * FROM shirts WHERE id_2 = '$item_id_2' LIMIT 1");
if ($sql && mysql_num_rows($sql)>0) {
    $row = mysql_fetch_array($sql);
    $product_name = $row["product_name"];
    $price = $row ["price"];
    $details = $row ["details"];

    $pricetotal = $price * $each_item['quantity'];
    $cartTotal = $pricetotal + $cartTotal;

    $pricetotal = number_format($price * $each_item['quantity'],2);
    $cartTotal = number_format($cartTotal,2);

    //Dynamic table row assembly
    $cartOutput.= '<tr>';
    $cartOutput.= '<td><a href = "product2.php?id=' . $item_id_2 . '">' . $product_name . '</a><br/>
        <img src ="inventory_images/' . $item_id_2 . '.jpg"alt="' . $product_name . '"width="40"height="52"border="1"/> </td>';

    $cartOutput.= '<td>'.$details. '</td>';
    $cartOutput.= '<td>$'.$price. '</td>';
    $cartOutput.= '<td><form action = "cart2.php" method = "post">
        <input name = "quantity" type = "text" value = "' . $each_item['quantity'] . '" size = "1" maxlength = "2" />
        <input name = "adjustBtn' .$item_id_2. '" type = "submit" value = "Update" />
        <input name = "item_to_adjust" type = "hidden" value = "' .$item_id_2. '"/>
        </form></td>';

    $cartOutput.= '<td>$' . $pricetotal . '</td>';
    $cartOutput.= '<td><form action = "cart2.php" method = "post"><input name = "deleteBtn' . $item_id_2 .'" type = "submit" value = "Remove"/><input 
        name = "index_to_remove" type = "hidden" value = "' . $i . '" /></form></td>';
    $cartOutput.= '</tr>';
    $i++;
}

Re: Add products from different pages

Posted: Tue Jan 31, 2012 8:27 am
by v8don
Hey thanks, I have not implemented this yet. But I forgot to mention that I have two tables from the database that has the two pages. The first table is products and the second is shirts and as you can see in the loop. I'm trying to pull everything from the shirt only mysql query and I initially thought that if I did the following code it would work.

So would I still need to insert code where it would pull from the products / item_id table because I only have shirts / item_id_2?

Thanks for all your help

Code: Select all




<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 5 (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
if(!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"])<1){
    $cartOutput="<h2 align = 'center'> Your shopping cart is empty </h2>";
} else {
        $i = 0;
        foreach($_SESSION["cart_array"] as $each_item) { 

                          [color=#4040FF]$item_id= $each_item ['item_id'];[/color]                          
                          $item_id_2 = $each_item ['item_id_2'];
                          [color=#4080FF]$sql = mysql_query("SELECT * FROM products WHERE id_2 = '$item_id_2' LIMIT 1");[/color]                         
                          $sql = mysql_query("SELECT * FROM shirts WHERE id_2 = '$item_id_2' LIMIT 1");
                          while ($row = mysql_fetch_array($sql)) {
                                $product_name = $row["product_name"];
                                $price = $row ["price"];
                                $details = $row ["details"];
                          
                          }
                          
?>




Re: Add products from different pages

Posted: Tue Jan 31, 2012 6:12 pm
by v8don
I've tryed the code but I am getting the same results. However, I wanted to make you aware that I have 2 tables in the database: products and shirts with coinciding item_id and item_id_2 that pulls the products from the tables. One table will not show price, description and total which is the products table and the shirts table shows everything.

Ok this is what I had. Under the foreach loop I thought if I just added the products mysql query along with the item_id it would work fine but it didn't.

Thanks for all your help!!!

Code: Select all


<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 5 (render the cart for the user to view on the page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
$cartTotal = "";
if(!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"])<1){
    $cartOutput="<h2 align = 'center'> Your shopping cart is empty </h2>";
} else {
        $i = 0;
        foreach($_SESSION["cart_array"] as $each_item) { 


 $item_id= $each_item ['item_id'];                      
                                                      $item_id_2 = $each_item ['item_id_2'];

$sql = mysql_query("SELECT * FROM products WHERE id_2 = '$item_id_2' LIMIT 1");                
        
                                                     $sql = mysql_query("SELECT * FROM shirts WHERE id_2 = '$item_id_2' LIMIT 1");


                          while ($row = mysql_fetch_array($sql)) {
                                $product_name = $row["product_name"];
                                $price = $row ["price"];
                                $details = $row ["details"];
                          
                          }
                          
?>