Problem defining variable

Hey everyone. Having a problem I was hoping someone could help me with. I am fairly new to php (i’ve done some forms and interactive database work) and can’t seem to get this one right. I am adding a couple options (one at a time since i’m still stuck on the first one) to show up on a cart page (size color etc). I have the variable going between the two pages as I can echo it out on the cart page but for some reason I can’t get it to output what variable they choose; in this instance it’s size. I will put the cart php here and if someone more knowledgeable has a moment to peruse it and point out where my error(s) are I would be so grateful as I have been working on this project 10 months (I have a developmental learning disability) and still don’t have a working final product.

[php]session_start();
error_reporting(E_ALL);
ini_set(‘display_errors’, ‘1’);

include “storescripts/connect_to_mysql.php”;
?>

<?php if (isset($_POST['pid'])) { $pid = $_POST['pid']; $wasFound = false; $i = 0; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $_SESSION["cart_array"] = array(1 => array("item_id" => $pid, "quantity" => 1, "size" => $size)); } else { foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $pid) { array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1, "size" => $size))); $wasFound = true; } } } if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1, "size" => $size)); } } header("location: cart.php"); exit(); } ?> <?php if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php $submit = $_POST['submit']; $size = $_POST['size']; if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); 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) { array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity, "size" => $size))); } } } } ?> <?php if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { $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 $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 { $pp_checkout_btn .= ' '; $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; } $pricetotal = $pricesub * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; setlocale(LC_MONETARY, "en_US"); $pricetotal = money_format("%10.2n", $pricetotal); $x = $i + 1; $pp_checkout_btn .= ' '; $product_id_array .= "$item_id-".$each_item['quantity'].","; $cartOutput .= ""; $cartOutput .= ' ' . $product_name . '
' . $product_name. ' '; $cartOutput .= '' . $details . ''; $cartOutput .= '' . $size . ''; $cartOutput .= '' . "$shipping" . ''; $cartOutput .= '$' . $price . ''; $cartOutput .= ' '; $cartOutput .= '' . $pricetotal . ''; $cartOutput .= ''; $cartOutput .= ''; $i++; } setlocale(LC_MONETARY, "en_US"); $cartTotal = money_format("%10.2n", $cartTotal); $cartTotal = "
Cart Total : ".$cartTotal." USD
"; $pp_checkout_btn .= ' '; } [/php]
I can echo it out on the cart page but for some reason I can't get it to output what variable they choose;

So which is it you can echo out the var or the var is not assigned a value ? as you say you can echo it (what is it ?)out from page to page but then if you can echo it out then its has to have a value that was chosen or something else ?
do you need to call the size var as a session[‘size’]

What is the form that is being posted from look like maybe its in that and not in your php ?

First of all, thank you for your time. It is greatly appreciated. Second the way I put it WAS fairly unclear. Let me try it again.
I’m having trouble declaring the variable in a multidimensional array. The page is a merchandise site.
the form is simply a drop down menu for size. You select your size and click add to cart and the information goes to cart page. I’m still not explaining it right. Let me give you the site address and a temporary login/pass and you can see what i’m talking about.
www.theblack44s.com/database/index.php
l/p temp/temporary
if you click on a shirt, then select the size and add to cart, that is where I am having trouble carrying the size over to the cart page then onto checkout.

ok I see what you mean you need to put the option selected=“selected” to the one that is selected but this has to be dynamic.

$size = $_REQUEST['size']; $size = $_SESSION['size'];

I think you will have to add comments to your php so you know what does what.
So have you printed out the arrays to see how they are set ?

I’m not sure I understand where to put the selected option (sorry, it’s my learning disability). I haven’t tried printing the array. I’ve never done it but I think I know how. I added several things to it with no problem (IE the shipping cost, though that info comes from the server, not a variable). I will try printing the array and see how it goes. Any other suggestions?

Because I do not know if the size variable is assigned to size its hard to start but if you dump the arrays to see how they look it will tell you more.
Where it vanishes try these to start with they might tell you a lot of information on your arrays and session data.

[php]
print_r()
or
var_dump()
print_r ($_SESSION);[/php]

ok using print_r ($_SESSION); I got the following array info:

[php]Array ( [username] => lostnvegas [cart_array] => Array ( [1] => Array ( [item_id] => 11 [quantity] => 1 [size] => xxxl ) ) ) [/php]

With the other fuctions print_r, var_dump you need to add the array var to it to get it to print it out to the screen.
[php]var_dump($var_array_name);[/php]

Can you try and use:
[php]
$size = $_session[‘cart_array’][‘1’][‘size’];
echo $size;
[/php]

no matter where i put
[php]$size = $_session[‘cart_array’][1][‘size’];[/php]
I still get an undefined variable error

hmmm
Just trying to get it to print out to start with so we are on the right track.
Its in the array so it just need to find the right keys.

[php]
echo ‘0’.$_SESSION[‘cart_array’][‘0’][‘2’];
echo’
’;
echo ‘1’.$_SESSION[‘cart_array’][‘1’][‘2’];
[/php]

Just to be sure, I am suppose to put that code at the end of all of the php code right?
when I do I get

0
1

at the top of the page

0 1 & 2 are just simbols so i know which line is giving the result

Try it without the quotes try it at the top but under the session_start as they are being set from the other page to this one in session ?

[php]
echo ‘0 ‘.$_SESSION[cart_array][0][2];
echo’
’;
echo ‘1 ‘.$_SESSION[cart_array][1][2];
echo’
’;
echo '2 '.$_SESSION[cart_array][1][size];

[/php]

this is what i get when i place the code at the beginning of the script:

Notice: Use of undefined constant cart_array - assumed ‘cart_array’ in /home/content/36/8631036/html/cart/cart.php on line 11

Notice: Undefined offset: 0 in /home/content/36/8631036/html/cart/cart.php on line 11
0

Notice: Use of undefined constant cart_array - assumed ‘cart_array’ in /home/content/36/8631036/html/cart/cart.php on line 13

Notice: Undefined offset: 2 in /home/content/36/8631036/html/cart/cart.php on line 13
1

Notice: Use of undefined constant cart_array - assumed ‘cart_array’ in /home/content/36/8631036/html/cart/cart.php on line 15

Notice: Use of undefined constant size - assumed ‘size’ in /home/content/36/8631036/html/cart/cart.php on line 15
2 Add to Shopping Cart
[hr]
And this is what I get when I place it at the end of the script:

1
2 Add to Shopping Cart

Try this code in different places where you have the arrays and top and bottom maybe it was the array [1] that is putting it off i see you use this onel once in your code after if !isset

echo '<br />print size <br />';
print_r($_SESSION['cart_array']['size']);
echo '<br />var dump<br />';
var_dump($_SESSION['cart_array']);
echo '<br /><br />';

ok at the beginning there was nothing but the same types of errors. I placed it at the end of the first block of code and got this:

print size

var dump
array(1) { [0]=> array(3) { [“item_id”]=> string(1) “9” [“quantity”]=> int(2) [“size”]=> string(20) “Add to Shopping Cart” } }

also got the same results at the very end of the code.

Can you put it in the code tags some of it is not showing

Is this where you splice and push the array ?
Is the product id and quantity right ?
or have then all been moved ?

this block ?
[php]
if (isset($_POST[‘pid’])) {
$pid = $_POST[‘pid’];
$wasFound = false;
$i = 0;
if (!isset($_SESSION[“cart_array”]) || count($_SESSION[“cart_array”]) < 1) {
$_SESSION[“cart_array”] = array(1 => array(“item_id” => $pid, “quantity” => 1, “size” => $size));
} else {
foreach ($_SESSION[“cart_array”] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == “item_id” && $value == $pid) {
array_splice($_SESSION[“cart_array”], $i-1, 1, array(“item_id” => $pid, “quantity” => $each_item[‘quantity’] + 1, “size” => $size));
$wasFound = true;
}
}
}
if ($wasFound == false) {
array_push($_SESSION[“cart_array”], array(“item_id” => $pid, “quantity” => 1, “size” => $size));
}
}
header(“location: cart.php”);
exit();
}
[/php]

This might be due to the count in it

[php]
echo 'size = '.$_SESSION[‘cart_array’][$i][‘size’]);
[/php]

[php]
echo '
print size
';
print_r($_SESSION[‘cart_array’][$i][‘size’]);
echo ‘
var dump
’;
var_dump($_SESSION[‘cart_array’]);
echo ‘

’;
[/php]

Did you mean this:

[php]

<?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(); } error_reporting(E_ALL ^ E_NOTICE); //print_r ($_SESSION); ?>

[/php]

or this:

[php]
print size

var dump
array(1) { [1]=> array(3) { [“item_id”]=> string(2) “11” [“quantity”]=> int(1) [“size”]=> string(20) “Add to Shopping Cart” } } [/php]

this was missing from it

 [1] 
var dump
array(1) { [1] => array(3) { ["item_id"]=> string(2) "11" ["quantity"]=> int(1) ["size"]=> string(20) "Add to Shopping Cart" 
} } 
Sponsor our Newsletter | Privacy Policy | Terms of Service