Page 1 of 1

some code i cant understand please help

Posted: Mon Sep 21, 2009 10:16 pm
by musicmancanora4
Hi guys im not sure what this php code is doing below i know if you click on the add cart button it adds more quantity of the product i understand that. But i do not understand the logic in the coding with the if statements. Iv put some comments below in the code to show you where im not understanding. If someone could explain to me i would appreciate it thanks.

Code: Select all

 
<?php
 
 
if (isset($_POST['submit'])) //isset is used to set the variable to turn it on basically remember what they
                             //added to the cart
{
 
  if($_SESSION['item']==$_POST['h1']) //does this actually do anything?
{
  $_SESSION['qty'] = $_SESSION['qty'] + 1; //the more you click add cart the more the qty increments and gets stored in the session
                                           //array
}
else
{
  $_SESSION['item'] = $_POST['h1']; //what do these two lines below do?
  $_SESSION['price']= $_POST['h2'];
}
 
 $_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item']; //i dont understand this part of the code either
 $_SESSION['itemqty'][$_SESSION['item']] = $_SESSION['qty'];
 $_SESSION['itemprice'][$_SESSION['item']] = $_SESSION['price'];
 
}
 
 
 
 
?>
<table>
<tr>
<td>
<img src="eric.jpg"/>
</td>
<td>This product is on clearance. New stock needs to come in and all the old stock needs to go. Hurry till offers end!!</td></tr>
<tr><td><form action="erricsson.php" method="post"><input type="submit"
name="submit" value="Add to cart"/><input type="hidden" name="h1"
value="k508i" /><input type="hidden" name="h2" value="400"/></form> </td></tr>
</table>
 
<a href="cart.php">View your cart</a>
</body>
</html>
 
 
 

Re: some code i cant understand please help

Posted: Tue Sep 22, 2009 2:37 am
by deejay

Code: Select all

 
if($_SESSION['item']==$_POST['h1']) //does this actually do anything?
 
i think this is checking to see if it's the same item as you had previously in your array. if so it adds an extra 1 to the quantity if not then it sets up a new item in the session. which is

Code: Select all

 
#  $_SESSION['item'] = $_POST['h1']; //what do these two lines below do?
#   $_SESSION['price']= $_POST['h2'];
 

would it help you if you echo'd your value and print_r the arrays - then you could see what values your getting and help you understandf the code.