Jump to content

Recommended Posts

Hi all,

So first off i would like to say this is my first shopping cart in the making. Ive been playing around with a few ways to build one and choose a rather simple way to make it all work.. How ever Im getting the dreaded undefined var error in a few places of my code..

If anyone wouldnt mind reading through my code.. And just btw this specific render is for a mini cart display and not the main cart before the check out. 

<?php 
//RENDERING CART % Display Settings
$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 {
	// Start PayPal Checkout Button
	$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="you@youremail.com">';
	// 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"];
		}
		// Dynamic Checkout Btn Assembly
		$x = $i + 1;
		$pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> //ERROR HERE FOR PRODUCT NAME
        <input type="hidden" name="amount_' . $x . '" value="' . $price . '"> //ERROR HERE WITH PRICE VAR
        <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';
		// Create the product array variable
		$product_id_array .= "$item_id-".$each_item['quantity'].","; 
		// Dynamic table row assembly
		
		$cartOutput .= '<div style="float:left;width:89%">
			<form action="view_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>
			X
			<span>' . $product_name . '</span> //ERROR HERE FOR PRODCT NAME VAR
		</div>
		<div style="float:right;width:10%;text-align:center;">
			<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>
		</div>
		';
		$i++; 
    } 
    // Finish the Paypal Checkout Btn
	$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
	<input type="hidden" name="notify_url" value="https://www.yoursite.com/storescripts/my_ipn.php">
	<input type="hidden" name="return" value="https://www.yoursite.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="https://www.yoursite.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>';
}
?>

 Ive commented the lines that hold errors, i have checked the var list, just really doesnt make much sense to me.

Any help would be appreciated..

 

Thanks in advance. :D 

Link to comment
https://forums.phpfreaks.com/topic/286528-undefined-var-errors/
Share on other sites

Try this. Just briefly skimmed through it and changed a couple of things (explained below)

<?php 
//RENDERING CART % Display Settings
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if(!empty($_SESSION["cart_array"])){
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
// Start PayPal Checkout Button
$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="you@youremail.com">';
// 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");
$prod = mysql_fetch_assoc($sql);
// Dynamic Checkout Btn Assembly
$x = $i + 1;
$pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $prod['product_name'] . '"> //ERROR HERE FOR PRODUCT NAME
        <input type="hidden" name="amount_' . $x . '" value="' . $prod['price'] . '"> //ERROR HERE WITH PRICE VAR
        <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].","; 
// Dynamic table row assembly


$cartOutput .= '<div style="float:left;width:89%">
<form action="view_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>
X
<span>' . $prod['product_name'] . '</span> //ERROR HERE FOR PRODCT NAME VAR
</div>
<div style="float:right;width:10%;text-align:center;">
<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>
</div>
';
$i++; 
    } 
    // Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="https://www.yoursite.com/storescripts/my_ipn.php">
<input type="hidden" name="return" value="https://www.yoursite.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="https://www.yoursite.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>';
}
?>
 
 
changed this:
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
to this: (for the sake of simplicity. It does the same as your code)
if(!empty($_SESSION["cart_array"])){
 
I changed your database query results to $prod and removed the while loop (why loop through results if you know there's only 1 sinceyou have LIMIT 1 on your statement)?
 
changed your variables, since the results of the query are now stored in the variable I called $prod (I'm assuming your table 'products' has a field called 'product_name' and another called 'price')
 
Why use SELECT *  if you only need 2 fields?
 
Test it now, and let me know.
 
Hope this helps
Edited by WebStyles

reason for inline styling is becasue when i uploaded it to a live server it didnt pick up on the class for some strange reason.. the original cart was done in oop and that didnt work either.. very strange, that nothing really worked... hmmm.. 

 

Okay changed the code to the above.. Im getting these two errors now: 

 

Notice: Undefined index: cart_array in line 102

Warning: Invalid argument supplied for foreach() line 102

 

This is the line with the error : 

foreach ($_SESSION["cart_array"] as $each_item) { 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.