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.

Recommended Answers

All 8 Replies

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.

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..

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.

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?

OH that is not actually necessary, you can remove it.

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

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.

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.

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.