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>

Recommended Answers

All 3 Replies

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.

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'];
 
}
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.