0

Hi, I have one page session-bom.php, in which computer parts are availlable for shopping. e.g. mouse , keyboard ect. with the checkbox. and textboxes to enter quantity and label of prices. When user hits 'submit' button, only selected parts goes to the bill.php page and showing their price multiplied by quantity. , and grand total.
Everything works with session except grand total. plz. guide me that why my grand total gets wrong.
Here are attachments.

Attachments
<?php
session_start();
$_SESSION['cpu'] = $_POST['cpu'];
$_SESSION['text_cpu'] = $_POST['text_cpu'];

$_SESSION['ups'] = $_POST['ups'];
$_SESSION['text_ups'] = $_POST['text_ups'];

$_SESSION['keyboard'] = $_POST['keyboard'];
$_SESSION['text_keyboard'] = $_POST['text_keyboard'];

$_SESSION['mouse'] = $_POST['mouse'];
$_SESSION['text_mouse'] = $_POST['text_mouse'];

$_SESSION['motherboard'] = $_POST['motherboard'];
$_SESSION['text_motherboard'] = $_POST['text_motherboard'];

$_SESSION['moniter'] = $_POST['moniter'];
$_SESSION['text_moniter'] = $_POST['text_moniter'];

$_SESSION['harddisk'] = $_POST['harddisk'];
$_SESSION['text_harddisk'] = $_POST['text_harddisk'];

$_SESSION['speaker'] = $_POST['speaker'];
$_SESSION['text_speaker'] = $_POST['text_speaker'];

$_SESSION['speaker'] = $_POST['speaker'];
$_SESSION['text_speaker'] = $_POST['text_speaker'];

$_SESSION['pendrive'] = $_POST['pendrive'];
$_SESSION['text_pendrive'] = $_POST['text_pendrive'];

$_SESSION['modem'] = $_POST['modem'];
$_SESSION['text_modem'] = $_POST['text_modem'];

$submit = $_POST['submit'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bill</title>
</head>

<body bgcolor="#D9ECEC">
<h1 align="center" style="color:#039">Your selected items and bill</h1>
<table width="60%" align="center" bordercolor="#434365">
<tr>
<td><strong>Selected Euipment</strong></td>
<td><strong>Quantity</strong></td>
<td><strong>Price</strong></td>
</tr>
<tr><td colspan="3"><hr color="#434365"/></td></tr>
<tr><td colspan="3">&nbsp;</tr>
    <tr>
    <td><?php echo $_SESSION['cpu']; ?></td>
    <td><?php echo $_SESSION['text_cpu']; ?></td>
    <td><?php if(isset($_SESSION['cpu']))
	{
		$_SESSION['bill_cpu'] = $_SESSION['text_cpu'] * 4050;
		echo $_SESSION['bill_cpu']; 
	}
  
	?></td>
  </tr>
  <tr>
    <td><?php echo $_SESSION['ups']; ?></td>
    <td><?php echo $_SESSION['text_ups']; ?></td>
    <td><?php if(isset($_SESSION['ups']))
	{
		$_SESSION['bill_ups'] = $_SESSION['text_ups'] * 2000;
		echo $_SESSION['bill_ups']; 
	}
  
	?></td>
  </tr>
  <tr>
    <td><?php echo $_SESSION['keyboard']; ?></td>
    <td><?php echo $_SESSION['text_keyboard']; ?></td>
    <td><?php if(isset($_SESSION['keyboard']))
	{
		$_SESSION['bill_keyboard'] = $_SESSION['text_keyboard'] * 1075;
		echo $_SESSION['bill_keyboard']; 
	}
  
	?></td>
  </tr>
  <tr>
    <td><?php echo $_SESSION['mouse']; ?></td>
    <td><?php echo $_SESSION['text_mouse']; ?></td>
    <td><?php if(isset($_SESSION['mouse']))
	{
		$_SESSION['bill_mouse'] = $_SESSION['text_mouse'] * 560;
		echo $_SESSION['bill_mouse']; 
	}
  
	?></td>
  </tr>
  <tr>
     <td><?php echo $_SESSION['motherboard']; ?></td>
    <td><?php echo $_SESSION['text_motherboard']; ?></td>
    <td><?php if(isset($_SESSION['motherboard']))
	{
		$_SESSION['bill_motherboard'] = $_SESSION['text_motherboard'] * 3075;
		echo $_SESSION['bill_motherboard']; 
	}
  
	?></td>
  </tr>
  <tr>
     <td><?php echo $_SESSION['moniter']; ?></td>
    <td><?php echo $_SESSION['text_moniter']; ?></td>
    <td><?php if(isset($_SESSION['moniter']))
	{
		$_SESSION['bill_moniter'] = $_SESSION['text_moniter'] * 14000;
		echo $_SESSION['bill_moniter']; 
	}
  
	?></td>
  </tr>
  <tr>
     <td><?php echo $_SESSION['harddisk']; ?></td>
    <td><?php echo $_SESSION['text_harddisk']; ?></td>
    <td><?php if(isset($_SESSION['harddisk']))
	{
		$_SESSION['bill_harddisk'] = $_SESSION['text_harddisk'] * 4067;
		echo $_SESSION['bill_harddisk']; 
	}
  
	?></td>
  </tr>
  <tr>
     <td><?php echo $_SESSION['speaker']; ?></td>
    <td><?php echo $_SESSION['text_speaker']; ?></td>
    <td><?php if(isset($_SESSION['speaker']))
	{
		$_SESSION['bill_speaker'] = $_SESSION['text_speaker'] * 1200;
		echo $_SESSION['bill_speaker']; 
	}
  
	?></td>
  </tr>
  <tr>
   <td><?php echo $_SESSION['pendrive']; ?></td>
    <td><?php echo $_SESSION['text_pendrive']; ?></td>
    <td><?php if(isset($_SESSION['pendrive']))
	{
		$_SESSION['bill_pendrive'] = $_SESSION['text_pendrive'] * 645;
		echo $_SESSION['bill_pendrive']; 
	}
  
	?></td>
      </tr>
  <tr>
   <td><?php echo $_SESSION['modem']; ?></td>
    <td><?php echo $_SESSION['text_modem']; ?></td>
    <td><?php if(isset($_SESSION['modem']))
	{
		$_SESSION['bill_modem'] = $_SESSION['text_modem'] * 2056;
		echo $_SESSION['bill_modem']; 
	}
  
	?></td>
  </tr>
  <tr>
    <td>Total</td>
    <td>&nbsp;</td>
    <td><?php
    $total =  $_SESSION['bill_cpu']+$_SESSION['bill_ups']+$_SESSION['bill_keyboard']+$_SESSION['bill_mouse']+$_SESSION['bill_motherboard']+$_SESSION['bill_moniter']+$_SESSION['bill_harddisk']+$_SESSION['bill_speaker']+$_SESSION['bill_pendrive']+$_SESSION['bill_modem'];
	echo $total;
  
	?></td>
  </tr>
 
</table>

</body>
</html>
<?php
session_start();
/*$_SESSION['cpu'] = $_POST['cpu'];
$_SESSION['text_cpu'] = $_POST['text_cpu'];

$_SESSION['ups'] = $_POST['ups'];
$_SESSION['text_ups'] = $_POST['text_ups'];

$_SESSION['keyboard'] = $_POST['keyboard'];
$_SESSION['text_keyboard'] = $_POST['text_keyboard'];

$_SESSION['mouse'] = $_POST['mouse'];
$_SESSION['text_mouse'] = $_POST['text_mouse'];

$_SESSION['motherboard'] = $_POST['motherboard'];
$_SESSION['text_motherboard'] = $_POST['text_motherboard'];

$_SESSION['moniter'] = $_POST['moniter'];
$_SESSION['text_moniter'] = $_POST['text_moniter'];

$_SESSION['harddisk'] = $_POST['harddisk'];
$_SESSION['text_harddisk'] = $_POST['text_harddisk'];

$_SESSION['speaker'] = $_POST['speaker'];
$_SESSION['text_speaker'] = $_POST['text_speaker'];

$_SESSION['speaker'] = $_POST['speaker'];
$_SESSION['text_speaker'] = $_POST['text_speaker'];

$_SESSION['pendrive'] = $_POST['pendrive'];
$_SESSION['text_pendrive'] = $_POST['text_pendrive'];

$_SESSION['modem'] = $_POST['modem'];
$_SESSION['text_modem'] = $_POST['text_modem'];
*/

/*if(!empty($submit))
{
	if(isset($_SESSION['cpu']))
	{
		$_SESSION['bill_cpu'] = $_SESSION['text_cpu'] * 4050;
		//echo "Bill of cpu :".$bill_cpu;
		//echo "<br/>";
	}
	if(isset($_SESSION['ups']))
	{
		$_SESSION['bill_ups'] = $_SESSION['text_ups'] * 2000;
		/*echo "Bill of ups :".$bill_ups;
		echo "<br/>";
	}
	if(isset($_SESSION['keyboard']))
	{
		$_SESSION['bill_keyboard'] = $_SESSION['text_keyboard'] * 1075;
		/*echo "Bill of keyboard :".$bill_keyboard;
		echo "<br/>";
	}
	if(isset($_SESSION['mouse']))
	{
		$_SESSION['bill_mouse'] = $_SESSION['text_mouse'] * 560;
		/*echo "Bill of mouse :".$bill_mouse;
		echo "<br/>";
	}
	if(isset($_SESSION['motherboard']))
	{
		$_SESSION['bill_motherboard'] = $_SESSION['text_motherboard'] * 3075;
		/*echo "Bill of motherboard :".$bill_motherboard;
		echo "<br/>";
	}
	if(isset($_SESSION['moniter']))
	{
		$_SESSION['bill_moniter'] = $_SESSION['text_moniter'] * 14000;
		/*echo "Bill of keybord :".$bill_moniter;
		echo "<br/>";
	}
	if(isset($_SESSION['harddisk']))
	{
		$_SESSION['bill_harddisk'] = $_SESSION['text_harddisk'] * 4067;
		/*echo "Bill of keybord :".$bill_harddisk;
		echo "<br/>";
	}
	if(isset($_SESSION['speaker']))
	{
		$_SESSION['bill_speaker'] = $_SESSION['text_speaker'] * 1200;
		/*echo "Bill of speaker :".$bill_speaker;
		echo "<br/>";
	}
	if(isset($_SESSION['pendrive']))
	{
		$_SESSION['bill_pendrive'] = $_SESSION['text_pendrive'] * 645;
		/*echo "Bill of pendrive :".$bill_pendrive;
		echo "<br/>";
	}
	if(isset($_SESSION['modem']))
	{
		$_SESSION['bill_modem'] = $_SESSION['text_modem'] * 2056;
		/*echo "Bill of modem :".$bill_modem;
		echo "<br/>";
	}
	$_SESSION['total'] = $_SESSION['bill_cpu']+$_SESSION['bill_ups']+$_SESSION['bill_keyboard']+$_SESSION['bill_mouse']+$_SESSION['bill_motherboard']+$_SESSION['bill_moniter']+$_SESSION['bill_harddisk']+$_SESSION['bill_speaker']+$_SESSION['bill_pendrive']+$_SESSION['bill_modem'];
	//echo "Total is :".$total;
	
}*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BOM</title>
</head>
<body bgcolor="#D9ECEC">
<form action="bill.php" id="bom" name="bom" enctype="multipart/form-data" method="post">
<h1 align="center" style="color:#003871">Online Computer Shoppy</h1>
<table width="70%" align="center" bordercolor="#434365">
<tr>
<td><strong>Computer Equipments</strong></td>
<td><strong>Cost</strong></td>
<td><strong>Quantity</strong></td>
</tr>
<tr><td colspan="3"><hr color="#434365"/></td></tr>
<tr><td colspan="3">&nbsp;</tr>
<tr><td>
      <input type="checkbox" name="cpu" value="cpu" id="cpu" />
      CPU</td>
      <td>4050/- only</td>
      <td><input name="text_cpu" type="text" id="text_cpu" /></td>
      </tr>
      <tr><td><input type="checkbox" name="ups" value="ups" id="ups" />
      UPS</td>
      <td>2000/- only</td>
      <td><input type="text" name="text_ups" id="text_ups" /></td>
      </tr>
      <tr><td><input type="checkbox" name="keyboard" value="keyboard" id="keyboard" />
      Keyboard</td>
      <td>1075/- only</td>
      <td><input type="text" name="text_keyboard" id="text_keyboard" /></td>
      </tr>
      <tr><td><input type="checkbox" name="mouse" value="mouse" id="material_3" />
      Mouse</td>
      <td>560/- only</td>
      <td><input type="text" name="text_mouse" id="text_mouse" /></td>
      </tr>
      <tr><td><input type="checkbox" name="motherboard" value="motherboard" id="motherboard" />
      Mother Board</td>
      <td>3075/- only</td>
      <td><input type="text" name="text_motherboard" id="text_motherboard" /></td>
      </tr>
      <tr><td><input type="checkbox" name="moniter" value="moniter" id="moniter" />
      Moniter</td>
      <td>14000/- only</td>
      <td><input type="text" name="text_moniter" id="text_moniter" /></td>
      </tr>
      <tr><td><input type="checkbox" name="harddisk" value="harddisk" id="harddisk" />
      Hard Disk</td>
      <td>4067/- only</td>
      <td><input type="text" name="text_harddisk" id="text_harddisk" /></td>
      </tr>
      <tr><td><input type="checkbox" name="speaker" value="speaker" id="speaker" />
      Speaker</td>
      <td>1200/- only</td>
      <td><input type="text" name="text_speaker" id="text_speaker" /></td>
      </tr>
      <tr><td><input type="checkbox" name="pendrive" value="pendrive" id="pendrive" />
      Pen Drive</td>
      <td>645/- only</td>
      <td><input type="text" name="text_pendrive" id="text_pendrive" /></td>
      </tr>
      <tr><td><input type="checkbox" name="modem" value="modem" id="modem" />
      Modem</td>
      <td>2056/- only</td>
      <td><input type="text" name="text_modem" id="text_modem" /></td>
      </tr>
      <tr><td colspan="3">&nbsp;</td></tr>
      <tr><td colspan="3">&nbsp;</td></tr>
      <tr><td colspan="3" align="center"><input type="submit" name="submit" id="submit" value="Submit" /></td></tr>
 </table>
 </form>
</body>
</html>
2
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by as.bhanuprakash
0

Can you please quote for which particular case, the total goes wrong ?
I tested by selected all the components with 1 quantity each and it gave me a correct total.

0

ok..dnt select each item with one quantity.. select any no of item(but not all), with more than one quantity. and see total.. it shows wrong total..

0

OK, i figured what the problem is. It is because of the session.
Say in the first run you select CPU - 2 and UPS - 3. Then i get the correct total - 14100.

Now if you run the session-bom.php in the same browser window again, the session will not have been destroyed. So now if you select
UPS - 2 and monitor - 1, then you get the total as 26100 instead of 18000. The extra 8100 is because of the session value $_SESSION which was calculated in the first run.

Now if you run the session-bom.php in a new browser window, then again it will work fine for the very first time, since there will be a new session created.

So in order to fix this, you can do some thing like this in your bill.php.

<td><?php if(isset($_SESSION['cpu']))
	{
		$_SESSION['bill_cpu'] = intval($_SESSION['text_cpu']) * 4050;
		echo $_SESSION['bill_cpu']; 
	} else {
		$_SESSION['bill_cpu'] = 0;
	}
  
	?></td>
  </tr>
  <tr>
    <td><?php echo $_SESSION['ups']; ?></td>
    <td><?php echo $_SESSION['text_ups']; ?></td>
    <td><?php if(isset($_SESSION['ups']))
	{
		$_SESSION['bill_ups'] = intval($_SESSION['text_ups']) * 2000;
		echo $_SESSION['bill_ups']; 
	} else {
		$_SESSION['bill_ups'] = 0;
	}

i have added the 'else' part to make the $_SESSION varaibles to ZERO. I have done this only for 2 components. Please do the same for others as well, then it will work fine.

0

Thanks, u r suggestion worked. Now bill.php shows correct total while running in same browser, but my question is, why r u taking inval() function even if all work can also be done without it? May I know the reason behind it?

0

OK and very very thanks for looking my thread and immediately solving it.

0

Hi again, can above thread solved by using small but smart technique? 'coz by using if...else , i think it is lengthy as well as simple programming. I want to do it with other better techniques to become professional in programming.

0

Yes you can do something like this..

<?php
session_start();
if (isset($_SESSION['bill'])) {
	unset($_SESSION['bill']);
}
..
..
..
<td><?php if(isset($_SESSION['cpu']))
	{
		$_SESSION['bill']['cpu'] = intval($_SESSION['text_cpu']) * 4050;
		echo $_SESSION['bill']['cpu']; 
}

..
..
$total =  $_SESSION['bill']['cpu']+$_SESSION['bill']['ups']+....;
?>

Instead of using $_SESSION, you can split it as a 2-dimensional array like $_SESSION and then at the begining of bill.php, you can unset() that variable. That way everytime only the new selected values will be set.

This question has already been answered. 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.