Jump to content

Query In If/else Clause


son

Recommended Posts

On a cart page I get $id as product_id and $var as variation (for main product 0 will be passed and $var then equals 0). The variation is stored as an attribute in session as (in this example $id is the session key):if ($var != 0){$_SESSION['cart'][$id]['var'] = $var;}To distinguish what data to display (either main product data or variation data) on cart page I check if $var is 0 and then run one or the other query as appropriate. This does not work and when variation is added the pv.product, pv.code, pv.detail, pv.price for latest added variation will be shown for each item displayed on cart page. I also realise now that when I use the update function $var will be 0 and then for each row the main product data will be shown. To use 'if ($_SESSION['cart'][$id]['var'])' instead of 'if ($var == 0)' brings the same wrong result. The relevant code is:

if ($var == 0){$cart_query = "SELECT p.product_id, p.product, p.code, p.detail, p.price, p.discount_id, p.img1 FROM products as p WHERE p.product_id IN (";}else{$cart_query = "SELECT p.product_id, pv.product, pv.code, pv.detail, pv.price, p.discount_id, p.img1 FROM products as p, productsVar as pv WHERE pv.productVar_id = $var AND p.product_id IN (";}		foreach($_SESSION['cart'] as $id => $v)		{		$cart_query .= (int) $id . ',';		}	$cart_query = substr ($cart_query, 0, -1) . ") ORDER BY p.product";	$cart_result = mysqli_query ($dbc, $cart_query);	var_dump ($cart_query);		if (mysqli_num_rows($cart_result) > 0)		{					while ($row = mysqli_fetch_array($cart_result, MYSQLI_ASSOC))			{			$product_name = $row['product'];			$product_code = $row['code'];			$product_detail = $row['detail'];			$price = $row['price'];etc

Is it not that I have to run both queries somehow as I need to display variation data for all keys where a variation is added and I also need to run the query for main products fo the main product added? Am gettign really lost with this one now...Son

Link to comment
Share on other sites

Where are you setting $var?
Above I have:
	if (isset($_GET['var']))	{	$var = (int) $_GET['var'];	}	elseif (isset($_POST['var']))	{	$var = (int) $_POST['var'];	}	else	{	$var = 0;	}if (isset($_GET['id']))	{	$id = (int) $_GET['id'];		if ($id > 0)		{		$id_query = "SELECT product, price FROM products WHERE product_id = $id";		$id_result = mysqli_query ($dbc, $id_query);			if (mysqli_num_rows($id_result) == 1)			{				if ($fn != 0)				{				$_SESSION['cart'][$id]['fn'] = $fn;				}				if ($var != 0)				{				$_SESSION['cart'][$id]['var'] = $var;				}and so on

Son

Link to comment
Share on other sites

What is it getting set to, where are $_GET['var'] or $_POST['var'] coming from?
Get is coming from product page and post will be on cart page itself (have not worked on update bit yet and problem occurs right away without using the update button)...Son
Link to comment
Share on other sites

I'm not sure what you're trying to display, but check what your if statements are doing with the query. If the variation ID is 0 then you're getting the records from the products table for every product in the cart. If the variation ID is not 0 then you're getting the records for that variation only from the productsVar table, for all products in the cart. So I'm not sure what it means when you pass the variation ID, what are you trying to display?

Link to comment
Share on other sites

I'm not sure what you're trying to display, but check what your if statements are doing with the query. If the variation ID is 0 then you're getting the records from the products table for every product in the cart. If the variation ID is not 0 then you're getting the records for that variation only from the productsVar table, for all products in the cart. So I'm not sure what it means when you pass the variation ID, what are you trying to display?
The cart displays a form in a table with rows of product data (everything in session array): first cell in each row is the photo, second contains product name, code etc, third contains quantity form input, fourth holds the sub-total (price of product multiplied with quantity). And this is where my issue happens. I thought instead of 'if ($var ==0)' to use something like '$_SESSION['cart'][$id]['var']' but this produces the same result. I realise to use $var is the reason why the query displays then only the data from the latest added variation. I need some mean to run query on all session data and then distinguish if it should come from variations table or main product table. I hope I make sense...Son
Link to comment
Share on other sites

You don't need to use anything from get or post then, just loop through $_SESSION['cart'] and for each item you should be able to check the variations array to see if it has any variations, and if so what the ID is. It will be easiest to look up each individual product or variation instead of trying to do one query to look all of them up. Use print_r($_SESSION), what does that look like?

Link to comment
Share on other sites

You don't need to use anything from get or post then, just loop through $_SESSION['cart'] and for each item you should be able to check the variations array to see if it has any variations, and if so what the ID is. It will be easiest to look up each individual product or variation instead of trying to do one query to look all of them up. Use print_r($_SESSION), what does that look like?
For print_r brings:Cart data = Array ( [349] => Array ( [fn] => 8 [age] => 1 [qty] => 1 ) [56] => Array ( [var] => 30 [qty] => 1 ) ) when I add:- one table that has a colour option (fn) and wood option (age); colour and wood do not affect price, we only need to record to know what colour and wood to send to customer- one option of a vase (main product comes in different shapes); colour and wood not applicable to this particular productPrinting the query shows clearly that I am going wrong as it only gets data for item with the option:string(198) "SELECT p.product_id, pv.product, pv.code, pv.detail, pv.price, p.discount_id, p.img1 FROM products as p, productsVar as pv WHERE pv.productVar_id = 30 AND p.product_id IN (349,56) ORDER BY p.product"To loop through $_SESSION['cart'] would be something like:
foreach($_SESSION['cart'] as $id => $v)		{		[b]do something with $_SESSION['cart'][$id]['var'][/b]		}

But how do I correctly check the variations array and how can I look up each individual product or variation?Son

Link to comment
Share on other sites

I'm a little confused now, I thought your cart was set up so that it's not indexed by ID and instead has the ID and variations as properties of each array element. The data you showed doesn't have any variations array, the cart array is just an entry for each product or variation I think. You're going to need to change how products get added to the cart, I'm not sure what to make of your current structure.

Link to comment
Share on other sites

I'm a little confused now, I thought your cart was set up so that it's not indexed by ID and instead has the ID and variations as properties of each array element. The data you showed doesn't have any variations array, the cart array is just an entry for each product or variation I think. You're going to need to change how products get added to the cart, I'm not sure what to make of your current structure.
Sorry, justsomeguy, I should have said: I went back to version where product id is session key. It turned out that I was completely overwhelmed with all changes that I thought it might be an idea just to get the simple version working first. When I even struggeld in how to make the updating work and amend:
<input type=\"text\" style=\"width: 2em\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['qty']}\" /> <a href='#'><img src='assets/delete.gif' width='15' height='15' alt='Click to remove item' onclick=\"document.forms['cart'].elements['qty[{$row['product_id']}]'].value='0'; document.forms['cart'].submit()\"></a>

I decided that I have to get the basics right first and will go back to implement the other changes after this. I know this will take longer, but I seem just to get nowhere. Anyhow I list the complete current code, so you know what I am up to:

<h1>Cart</h1><?php			$discPerc = 0;			$redTotal = 0;		if (isset($_SESSION['back']) && $_SESSION['back'] != '')		{		$backlink = $_SESSION['back'];		}		else		{		$backlink = 'index.php';		}$basketPath = "products/basket/";if (isset($_REQUEST['do']) && ($_REQUEST['do'] == 'add')){	if (isset($_GET['qty2']))	{	$qty2 = (int) $_GET['qty2'];	}	else	{	$qty2 = 1;	}	if (isset($_GET['fn']))	{	$fn = (int) $_GET['fn'];	if ($fn == 999)		{		echo "<p>Please <a href=\"" . $backlink . "\" title=\"Go back\">go back</a> and select a colour option.</p>";		echo "</div>\n";		require_once "fyu/footer.php";		exit;		}	}	elseif (isset($_POST['fn']))	{	$fn = (int) $_POST['fn'];	}	else	{	$fn = 0;	}	if (isset($_GET['var']))	{	$var = (int) $_GET['var'];	}	elseif (isset($_POST['var']))	{	$var = (int) $_POST['var'];	}	else	{	$var = 0;	}	if (isset($_GET['age']))	{	$age = (int) $_GET['age'];	}	elseif (isset($_POST['age']))	{	$age = (int) $_POST['age'];	}	else	{	$age = 0;	}	if (isset($_GET['id']))	{	$id = (int) $_GET['id'];		if ($id > 0)		{		$id_query = "SELECT product, price FROM products WHERE product_id = $id";		$id_result = mysqli_query ($dbc, $id_query);			if (mysqli_num_rows($id_result) == 1)			{				if ($fn != 0)				{				$_SESSION['cart'][$id]['fn'] = $fn;				}				if ($var != 0)				{				$_SESSION['cart'][$id]['var'] = $var;				}				if ($age != 0)				{				$_SESSION['cart'][$id]['age'] = $age;				}							list ($product, $price) = mysqli_fetch_array($id_result, MYSQLI_NUM);				if (isset($_SESSION['cart'][$id]))				{				$_SESSION['cart'][$id]['qty'] += $qty2;				echo "<p>Another copy of the following item has been added to your cart: <strong>$product</strong>.</p>\n";				}				else				{				$_SESSION['cart'][$id]['qty'] = $qty2;				echo "<p>The following item has been added to your cart: <strong>$product</strong>.</p>\n";				}			}		}	}}elseif (isset($_REQUEST['do']) && ($_REQUEST['do'] == 'update')){	foreach ($_POST['qty'] as $k => $v)	{	$pid = (int) $k;	$qty = (int) $v;		if ($qty == 0)		{		unset ($_SESSION['cart'][$pid]);		}		elseif ($qty > 0)		{		$_SESSION['cart'][$pid]['qty'] = $qty;		}	}	foreach ($_POST['var'] as $t => $p)	{	$pid = (int) $t;	$var = (int) $p;	$_SESSION['cart'][$pid]['var'] = $var;	}	foreach ($_POST['fn'] as $u => $i)	{	$pid = (int) $u;	$fn = (int) $i;	$_SESSION['cart'][$pid]['fn'] = $fn;	}	foreach ($_POST['age'] as $r => $o)	{	$pid = (int) $r;	$age = (int) $o;	$_SESSION['cart'][$pid]['age'] = $age;	}	echo "<p>The cart has been updated.</p>\n";}if ($_SESSION['cart'] && !empty($_SESSION['cart'])){echo "Cart data = ";print_r($_SESSION['cart']); if ($var == 0){$cart_query = "SELECT p.product_id, p.product, p.code, p.detail, p.price, p.discount_id, p.img1 FROM products as p WHERE p.product_id IN (";}else{$cart_query = "SELECT p.product_id, pv.product, pv.code, pv.detail, pv.price, p.discount_id, p.img1 FROM products as p, productsVar as pv WHERE pv.productVar_id = $var AND p.product_id IN (";}		foreach($_SESSION['cart'] as $id => $v)		{		$cart_query .= (int) $id . ',';		}	$cart_query = substr ($cart_query, 0, -1) . ") ORDER BY p.product";	$cart_result = mysqli_query ($dbc, $cart_query);	var_dump ($cart_query);		if (mysqli_num_rows($cart_result) > 0)		{		echo "<form action=\"cart.php\" method=\"post\" name=\"cart\" id=\"cart\">\n";		echo "<table style=\"margin-top:15px;\">\n";		echo "<tr><th>Item</th><th>Description</th><th>Qty</th><th class=\"right\">Price</th></tr>\n";		$total = 0;			while ($row = mysqli_fetch_array($cart_result, MYSQLI_ASSOC))			{			$product_name = $row['product'];			$product_code = $row['code'];				if ($row['detail'] != '')				{				$product_detail = $row['detail'];				}				else				{				$product_detail = FALSE;				}			$price = $row['price'];			$price_unaltered = $row['price'];				if ($row['discount_id'] != '')				{				$discount = $row['discount_id'];				$discount = (int) $discount;				}				else				{				$discount = 0;				}			$img = $row['img1'];			$size = getimagesize("products/basket/{$img}");			$subtotal = $_SESSION['cart'][$row['product_id']]['qty']* $price;			$total += $subtotal;			// depending on total apply reductions on total			if ($discount == 2)			{			$redTotalInd = $_SESSION['cart'][$row['product_id']]['qty']* $price;			$redTotal += $redTotalInd;			}			if ($redTotal >= 500 && $redTotal <= 999)			{			$discPerc = 0.9;			}			elseif ($redTotal >= 1000 && $redTotal <= 1999)			{			$discPerc = 0.8975;			}			elseif ($redTotal >= 2000 && $redTotal <= 2999)			{			$discPerc = 0.8950;			}			elseif ($redTotal >= 3000 && $redTotal <= 3999)			{			$discPerc = 0.8925;			}			elseif ($redTotal >= 4000 && $redTotal <= 4999)			{			$discPerc = 0.89;			}			elseif ($redTotal >= 5000 && $redTotal <= 5999)			{			$discPerc = 0.8875;			}			elseif ($redTotal >= 6000 && $redTotal <= 6999)			{			$discPerc = 0.8850;			}			elseif ($redTotal >= 7000 && $redTotal <= 7999)			{			$discPerc = 0.8825;			}			elseif ($redTotal >= 8000 && $redTotal <= 8999)			{			$discPerc = 0.88;			}			elseif ($redTotal >= 9000 && $redTotal <= 9999)			{			$discPerc = 0.8775;			}			elseif ($redTotal >= 10000 && $redTotal <= 10999)			{			$discPerc = 0.8750;			}			elseif ($redTotal >= 11000 && $redTotal <= 11999)			{			$discPerc = 0.8725;			}			elseif ($redTotal >= 12000 && $redTotal <= 12999)			{			$discPerc = 0.87;			}			elseif ($redTotal >= 13000 && $redTotal <= 13999)			{			$discPerc = 0.8675;			}			elseif ($redTotal >= 14000 && $redTotal <= 14999)			{			$discPerc = 0.8650;			}			elseif ($redTotal >= 15000 && $redTotal <= 15999)			{			$discPerc = 0.8625;			}			elseif ($redTotal >= 16000 && $redTotal <= 16999)			{			$discPerc = 0.86;			}			elseif ($redTotal >= 17000 && $redTotal <= 17999)			{			$discPerc = 0.8575;			}			elseif ($redTotal >= 18000 && $redTotal <= 18999)			{			$discPerc = 0.8550;			}			elseif ($redTotal >= 19000 && $redTotal <= 19999)			{			$discPerc = 0.8525;			}			elseif ($redTotal >= 20000 && $redTotal <= 20999)			{			$discPerc = 0.85;			}			elseif ($redTotal >= 21000 && $redTotal <= 21999)			{			$discPerc = 0.8475;			}			elseif ($redTotal >= 22000 && $redTotal <= 22999)			{			$discPerc = 0.8450;			}			elseif ($redTotal >= 23000 && $redTotal <= 23999)			{			$discPerc = 0.8425;			}			elseif ($redTotal >= 24000 && $redTotal <= 24999)			{			$discPerc = 0.84;			}			elseif ($redTotal >= 25000 && $redTotal <= 25999)			{			$discPerc = 0.8375;			}			elseif ($redTotal >= 26000 && $redTotal <= 26999)			{			$discPerc = 0.8350;			}			elseif ($redTotal >= 27000)			{			$discPerc = 0.8325;			}	$subtotal2 = $_SESSION['cart'][$row['product_id']]['qty']* $price;	$total2 += $subtotal2;	$grand_total = $total2;	if ($discPerc != 0)	{	$percent = (1 - $discPerc) * 100;	}	if ($redTotal >= 500)	{	$discAmount = $redTotal * (1 - $discPerc);	}	else	{	$discAmount = 0;	}	$grand_total = $total - $discAmount;	$price = number_format($price, 2);			echo "<tr>";		echo "<td><img src=\"" . $basketPath . $img . "\" " . $size[3] . " alt=\"" . $product_name . "\" /></td>";		echo "<td>" . $product_name . "<br />Product Code: " . $product_code;		if ($product_detail)		{		echo "<br />Size in cm: " . $product_detail . "<br  />";		}		$fn_name = $_SESSION['cart'][$row['product_id']]['fn'];		if ($fn_name == 1)			{			echo 'Colour: light brown';			}			elseif ($fn_name == 2)			{			echo 'Colour: dark brown';			}			elseif ($fn_name == 3)				{			echo 'Colour: white';			}			elseif ($fn_name == 4)			{			echo 'Colour: black';			}		$age_name = $_SESSION['cart'][$row['product_id']]['age'];		if ($age_name != 0)		{		echo "<br />";			if ($age_name == 3)			{			echo 'Pine';			}			elseif ($age_name == 2)			{			echo 'Oak';			}		}		echo "</td>";		echo "<td><input type=\"text\" style=\"width: 2em\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['qty']}\" /> <a href='#'><img src='assets/delete.gif' width='15' height='15' alt='Click to remove item' onclick=\"document.forms['cart'].elements['qty[{$row['product_id']}]'].value='0'; document.forms['cart'].submit()\"></a>";		echo "<p style=\"margin-left:0;\">";		echo "<span class=\"small\">Per item: £" . $price_unaltered . "</span>";		if ($discount == 0)		{		echo "<br /><span class=\"small\">No discount for this item.</span>";		}		echo "</p>";		echo "</td>";		echo "<td class=\"right\">£" . number_format($subtotal, 2) . "</td>";		echo "</tr>\n";		}		echo "<tr><td colspan=\"3\" class=\"right noBorder\">Sub Total:</td><td class=\"right\">£" . number_format($total, 2) . "</td></tr>\n";		echo "<tr><td colspan=\"3\" class=\"right noBorder\">DISCOUNT ON APPLICABLE ITEMS";		if ($discPerc != 0)		{		echo " (" . number_format($percent, 2) . "%)";		}		echo ":</td><td class=\"right\">– £" . number_format($discAmount, 2) . "</td></tr>\n";		echo "<tr><td colspan=\"3\" class=\"right noBorder\"><strong>Total:</strong></td><td class=\"right\"><strong>£" . number_format($grand_total, 2) . "</strong></td></tr>\n";		echo "<tr><td colspan=\"4\" class=\"noBorder\"> </td></tr>\n";		echo "<tr><td colspan=\"4\" class=\"right noBorder\" style=\"padding-right:0;\">";		echo "<input type=\"hidden\" name=\"do\" value=\"update\" />\n";		echo "<input type=\"submit\" value=\"Update Total\" id=\"submitted\" name=\"submitted\" />";		echo "</td></tr>\n";		echo "<tr><td colspan=\"4\" class=\"noBorder\"> </td></tr>\n";				echo "</table>\n";		echo "</form>\n";	}}else{echo "<p>Currently there are no items added to this page.</p>\n";}?>

Son

Link to comment
Share on other sites

I just seem not to get my head around how to loop through the session array to get the data displayed on cart. Would it be easier for me to move the vatiations into main product table and just to insert new row with parent (which is product_id of product this variation should show under)? Then I could just simply pass the product id from drop down and would not have to worry about $var. What do you think?Son

Link to comment
Share on other sites

Frankly, it might be a good idea to start over. It sounds like you started working on the cart when you only had a few needs and now you're trying to add a bunch of other stuff in that may not fit into the model you started with. You may want to get your list of requirements for a shopping cart and start over with the design of the shopping cart so that it supports everything you need. I've had to do that periodically, I'll be working on something and it changes so much that it becomes a better idea to redesign it and start over then to try to retrofit all of the features into a model that may not work very well.

Link to comment
Share on other sites

Frankly, it might be a good idea to start over. It sounds like you started working on the cart when you only had a few needs and now you're trying to add a bunch of other stuff in that may not fit into the model you started with. You may want to get your list of requirements for a shopping cart and start over with the design of the shopping cart so that it supports everything you need. I've had to do that periodically, I'll be working on something and it changes so much that it becomes a better idea to redesign it and start over then to try to retrofit all of the features into a model that may not work very well.
I will try tomorrow to re-think. I am only worried as I am a bit lost with the multi-dimensional array and I seem not to be able to find any good tutorial to explain how I compare attributes from session array to values in database or how I do the quantity buttons. With:
<input type=\"text\" style=\"width: 2em\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['qty']}\" /> <a href='#'><img src='assets/delete.gif' width='15' height='15' alt='Click to remove item' onclick=\"document.forms['cart'].elements['qty[{$row['product_id']}]'].value='0'; document.forms['cart'].submit()\"></a>

I could deal with it, but not it is going deeper and deeper into the session array and I coudl really do with a good tutorial..Anyway, will spend tomorrow morning on task at hand and hopefully will be able to come up with a good start at least...Thanks for your help,Son

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...