Problem defining variable

Apparently I’m having a blonde moment. I am totally lost now. I’m so sorry, this must be frustrating for you. Where was I suppose to place the
[php][1][/php]

Can you print the array before the foreach loop see if it is being changed in the loop.

No that was what was missing when you did not put the code in code tags just forget that post.

oh ok, sorry about that.
nothing happens when i try to print the array before the [php]foreach[/php] loop.

This gives you nothing ?
If put above the foreach.
[php]var_dump($_SESSION[“cart_array”] );[/php]

That’s correct, it just gives me the normal screen.

But you have something in the cart though?
So there should be something assigned.

We just have to figure out where Add to Shopping Cart is being put in the place of the real value for size.

It seems the submit button value is being put in the place of the size value.

it prints this after the foreach loop
[php]array(1) { [1]=> array(3) { [“item_id”]=> string(2) “11” [“quantity”]=> int(1) [“size”]=> string(20) “Add to Shopping Cart” } } [/php]

We have the right value in this array for size but it gets changed somewhere along the way.

can you add after

[php]while (list($key, $value) = each($each_item)) { [/php]

[php]echo “$key => $value
”;[/php]

Not really watching tv and I enjoy the end result if we get it sorted, I know how frustrating things get when you cant get something to work.

same thing, nothing unless i put it at the end of the block and even then it gives me this error:

[php]
Notice: Undefined variable: key in /home/content/36/8631036/html/cart/cart.php on line 48

Notice: Undefined variable: value in /home/content/36/8631036/html/cart/cart.php on line 48
=>
[/php]

have you added an item to the cart when you do the checks ?
I did ask a couple back but you did not say if it was empty or not.

Yes i’m sorry I do have one item in the cart. I wish there was an easier way to show you everything.

Sorry for jumping in… But…

This code:
$submit = $_POST[‘submit’];
$size = $_POST[‘size’];

is usually done in this format:
if (isset($_POST[‘submit’])) {
$size = $_POST[‘size’];
}else{
echo “No size selected!”;
}

So, if the submit button is pressed, then you process whatever.
The $_POST[‘size’]; line is correct and should pull the text in the values such as “small”, “large”, etc…

Now, onto the array. This line displayed looks like the array is correctly being created:

Array ( [username] => lostnvegas [cart_array] => Array ( [1] => Array ( [item_id] => 11 [quantity] => 1 [size] => xxxl ) ) )
This "array" is actually a nested array or a multidimensional array. So, when displaying it, you would need to make sure you take the inside array for the display. So, the array actually contains: Array#1 - [username] => lostnvegas, followed by: Array#2 - [cart_array] => [1]=> an array, followed by: Array#3 - [item_id] => 11 [quantity] => 1 [size] => xxxl So, you actually have 3 array's... You would need to display this info backwards pulling each array out to get the inside data. Can you repost the display code where you try to display this so we have the current version? Perhaps it is just a minor issue with pulling the array data out...

Sure here is the cart page:
[php]<?php
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set(‘display_errors’, ‘1’);
// Connect to the MySQL database
include “storescripts/connect_to_mysql.php”;
?>

<?php if(isset($_POST['submit'])) { $size = $_POST['submit']; } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 1 (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(1 => array("item_id" => $pid, "quantity" => 1, "size" => $size)); } 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" && $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, "quantity" => $each_item['quantity'] + 1, "size" => $size))); $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1, "size" => $size)); } } header("location: cart.php"); exit(); } echo "$key => $value
"; // var_dump($_SESSION["cart_array"] ); error_reporting(E_ALL ^ E_NOTICE); //print_r ($_SESSION); ?> <?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 item quantity) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $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_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) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //if(isset($_POST['size'])) { //$size=$_POST['size']; //} $cartOutput = ""; $cartTotal = ""; $pp_checkout_btn = ''; $product_id_array = ''; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "

Your shopping cart is empty

"; } else { // Start PayPal Checkout Button $pp_checkout_btn .= ' '; // Start the For Each loop $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $product_name = $row["product_name"]; $price = $row["price"]; $details = $row["details"]; $shipping = $row["shipping"]; $pricesub = $price + $shipping; $size = $_POST['size']; } $size = $_GET['size']; $pricetotal = $pricesub * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; setlocale(LC_MONETARY, "en_US"); $pricetotal = money_format("%10.2n", $pricetotal); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= ' '; // Create the product array variable $product_id_array .= "$item_id-".$each_item['quantity'].","; // Dynamic table row assembly $cartOutput .= ""; $cartOutput .= ' ' . $product_name . '
' . $product_name. ' '; $cartOutput .= '' . $details . ''; $cartOutput .= "'' . $size . ''"; $cartOutput .= '' . $shipping . ''; $cartOutput .= '$' . $price . ''; //$cartOutput .= '$' . $price . ''; $cartOutput .= ' '; //$cartOutput .= '' . $each_item['quantity'] . ''; $cartOutput .= '' . $pricetotal . ''; $cartOutput .= ''; $cartOutput .= ''; $i++; } setlocale(LC_MONETARY, "en_US"); $cartTotal = money_format("%10.2n", $cartTotal); $cartTotal = "
Cart Total : ".$cartTotal." USD
"; // Finish the Paypal Checkout Btn $pp_checkout_btn .= ' '; } ?> Your Cart
<?php include_once("template_header.php");?>
<br />
<table width="100%" height="60" border="1" cellpadding="6" cellspacing="0">
  <tr>
    <td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td>
    <td width="30%" bgcolor="#C5DFFA"><strong>Product Description</strong></td>
	<td width="19%" bgcolor="#C5DFFA"><strong>Size</strong></td>
	<td width="5%" bgcolor="#C5DFFA"><strong>Shipping</strong></td>
	<td width="5%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td>
    <td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
    <td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td>
    <td width="5%" bgcolor="#C5DFFA"><strong>Remove</strong></td>
  </tr>

 <?php echo $cartOutput; ?>
 <!-- <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
	<td>&nbsp;</td>
	<td>&nbsp;</td>
  </tr> -->
</table>
<?php echo $cartTotal; ?>
<br />

Continue Shopping



<?php echo $pp_checkout_btn; ?>
<?php include_once("styles/template_footer.php");?>
[/php] [hr] the actual page is online at www.theblack44s.com/database/index.php

Try replacing lines 18-19 or so which was:
if (isset($_POST[‘pid’])) {
$pid = $_POST[‘pid’];

To something like this:
if (isset($_POST[‘pid’])) {
$pid = $_POST[‘pid’];
$size = $_POST[‘size’];

You never pulled the size from the posted page…

Also, the error on line 48 is due to this line: echo “$key => $value
”;
which you can not print. You could make it echo $key . “=” . $value . “
”;

Not sure why that line is there…

I will try this code first thing tomorrow morning. I appreciate your posting it. I have an overnight medical stay but I will be back first thing and will let you know how it goes. Thanks again.

size = $_POST[‘size’] is this not in the $_session[‘cart_array’] array

Maybe that is where I was going wrong, I thought it was posting from store php page to cart php page.

Maybe I should have added $size = $_POST[size] $_SESSION[‘size’] = $size; in the store.php and size = $_SESSION[‘size’]; in the cart.php.

But if it can $_POST to the other page without this then that would solve it.

Well, that is what I was basically saying. A form POST’s to a PHP page, but, you have to read the posted values with $_POST[‘variable’]… So, you did not do this. YOU have to get that value and then store it inside of the $_SESSION variables. The session variable will be carried from page to page, but POST’ed values must be read first and then stored in the session’s cart array…

Hope that makes sense… Look at my last note about lines 18-19. That is where you need to pull (or read) the posted value for ‘size’… Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service