0
Hey guys im not sure how this isset code is working and what each if statement is testing.

a clear explanation would help.

cheers


****ViewProductDetails.php****
Code:

<?php
session_start();
?>
<html>
<body>
<?php


if (isset($_POST['submit']))
{

  if($_SESSION['item']==$_POST['h1'])
{
  $_SESSION['qty'] = $_SESSION['qty'] + 1;
}
else
{
  $_SESSION['item'] = $_POST['h1'];
  $_SESSION['price']= $_POST['h2'];
}

 $_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item'];
 $_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>
3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by vibhaJ
0

Please post a description of what the problem is and what errors you are receiving. In case you are wondering how many words are necessary I would say 32 or more.

0

Okay im not getting any specific errors im just trying to study the code itself to better understand what its doing.

if (isset($_POST['submit']))
{
 
if($_SESSION['item']==$_POST['h1'])
{
$_SESSION['qty'] = $_SESSION['qty'] + 1;
}
else
{
$_SESSION['item'] = $_POST['h1'];
$_SESSION['price']= $_POST['h2'];
}
 
$_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item'];
$_SESSION['itemqty'][$_SESSION['item']] = $_SESSION['qty'];
$_SESSION['itemprice'][$_SESSION['item']] = $_SESSION['price'];
 
}
0
if (isset($_POST['submit'])) // when form is submitted
{
 
    if($_SESSION['item']==$_POST['h1']) // if "h1" input's value is same as item stored in session (2*: see below)
    {
    	$_SESSION['qty'] = $_SESSION['qty'] + 1; // then increament qty stored in session
    }
    else // if h1's value is not matching with session (1*: see below)
    {
    	$_SESSION['item'] = $_POST['h1']; // then save "h1"'s value in item session
    	$_SESSION['price']= $_POST['h2']; // save "h2"'s value in price session
    }
    // below is setting session array
    $_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item'];
    $_SESSION['itemqty'][$_SESSION['item']] = $_SESSION['qty'];
    $_SESSION['itemprice'][$_SESSION['item']] = $_SESSION['price'];
     
}

If this is related to cart then below is the meaning:
1*: if you are first time adding product to cart then it will create session
2*: if item is already in cart then increament item's quantity.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.