PHP Programming > Beginners - Learning PHP
Problem defining variable
lostnvegas:
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 Code: ---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 = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
$pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="sales@theblack44s.com">';
$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 .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
<input type="hidden" name="size_' . $x . '" value"' . $size . '">
<input type="hidden" name="shipping_' . $x . '" value="' . $shipping . '">
<input type="hidden" name="amount_' . $x . '" value="' . $price . '">
<input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
$product_id_array .= "$item_id-".$each_item['quantity'].",";
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>' . $size . '</td>';
$cartOutput .= '<td>' . "$shipping" . '</td>';
$cartOutput .= '<td>$' . $price . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post">
<input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form></td>';
$cartOutput .= '<td>' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '</tr>';
$i++;
}
setlocale(LC_MONETARY, "en_US");
$cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>";
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="http://www.theblack44s.com/cart/storescripts/my_ipn.php">
<input type="hidden" name="return" value="https://www.theblack44s.com/checkout_complete.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="http://www.theblack44s.com/paypal_cancel.php">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>';
}
--- End code ---
Noodles:
--- Quote ---I can echo it out on the cart page but for some reason I can't get it to output what variable they choose;
--- End quote ---
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 ?
lostnvegas:
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.
Noodles:
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.
--- Code: ---$size = $_REQUEST['size'];
$size = $_SESSION['size'];
--- End code ---
Noodles:
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 ?
Navigation
[0] Message Index
[#] Next page
Go to full version