can anyone pls help me im still an IT student,
and we have this case study Pizza Delivery system

which the user can choose and check all the text box of the pizza size

<INPUT TYPE=checkbox NAME="pica10" VALUE="S">10" Php300  
      <INPUT TYPE=checkbox NAME="pica14" VALUE="M">14" Php450  
      <INPUT TYPE=checkbox NAME="pica18" VALUE="L">18" Php650

then they can also input the quantity

Quantity: <input type="text" size="3">

my problem is how can i compute for the total of those.
please help me....

Member Avatar for iamthwee

Each checkbox should be assigned a name.

Go through each name and see if it is checked:

if (isset($_POST['chbox'])) 
{
  /*add values*/
}

If it is checked add the values up.

Hi,

You can add <input> to your form and give them a name unique to the pizza size. Fore example, take a look at the codes below..

<INPUT TYPE=checkbox NAME="pica10" VALUE="S">10" Php300 
<input type="text" name="sq" value=""/> 
      <INPUT TYPE=checkbox NAME="pica14" VALUE="M">14" Php450 
	  <input type="text" name="mq" value=""/> 
      <INPUT TYPE=checkbox NAME="pica18" VALUE="L">18" Php650
	  <input type="text" name="lq" value=""/>

Then on your form processor, you need to make sure that specific pizza has been selected, otherwise you might end up charging the customer for something they did not order. Use jquery to validate your form for the quantity.. it has to be an integer.

So, look for something like

if(($_POST['pica10']) && ($_POST['sq'])){
## set condition if the quantity is not empty then proceed below

## calculate the sum of the small pizza ordered
$total_Spizza = 1 * ($_POST['sq']);
## calculate the price of the small pizza
## WARNING! DO NOT put any integer inside the quotes..it don't matter if single or double
$total_Sprice = 300 * ( $total_Spizza );

## if quantity is empty $total_Spizza = 0

}

if(## this is for medium pizza){
## Do the same for the medium pizza exact format as above
$total_Mpizza ="";
$total_Mprice ="";

}

## do the same for the large
if(){

}

## calculate the sum of the total bills to be paid by custumer

$total_AmountDue = $total_Mprice + $total_Mprice + $total_Mprice ; 

## calculate the total Pizza you need to bake and deliver to the customer

echo "Need to bake".$total_Spizza." Small Pizza ".$total_Mpizza." Medium Pizza. ".$total_Lpizza." Large Pizza";

Try doing that and post your code progression here...

correction!

Change this...

$total_AmountDue = $total_Mprice + $total_Mprice + $total_Mprice ;

to this

$total_AmountDue = $total_Sprice + $total_Mprice + $total_Lprice ; 
echo "Must Pay ".$total_AmountDue." Immediately, or else I will eat these Pizza";

if you will be adding sales tax to your cutomer total amount due, then it should changed to

$gross_AmountDue = $total_Sprice + $total_Mprice + $total_Lprice ; 
$tax_Rate = 9.5 ; ## this is for a sales tax rate of 9.5 percent
$total_AmountDue = round((($gross_AmountDue) * ($tax_Rate / 100)), 2 );
echo "Must Pay ".$total_AmountDue." Immediately, or else I will eat these Pizza";

I'd use javascript for that or does it have to be php?

Gosh!!thanks guys veedeoo and iamthwee!
the codes looks alot helpful.
im gonna try the codes as soon as i can,maybe tomorrow, im busy now with my other subjects.(finals week)

i just hope you guys still there if i still have questions!:)
and Biiim, i dont knw much about javascript yet,and our subjct is PhP.
if u also have idea (PhP)pls reply here thanks :)

cool, just in case sales tax needs to be included. I overlooked the calculation for the total amount due it should read

echo "Must Pay ".( $total_AmountDue + $gross_AmountDue)." Immediately, or else I will eat these Pizza";

correction!

Change this...

$total_AmountDue = $total_Mprice + $total_Mprice + $total_Mprice ;

to this

$total_AmountDue = $total_Sprice + $total_Mprice + $total_Lprice ; 
echo "Must Pay ".$total_AmountDue." Immediately, or else I will eat these Pizza";

if you will be adding sales tax to your cutomer total amount due, then it should changed to

$gross_AmountDue = $total_Sprice + $total_Mprice + $total_Lprice ; 
$tax_Rate = 9.5 ; ## this is for a sales tax rate of 9.5 percent
$total_AmountDue = round((($gross_AmountDue) * ($tax_Rate / 100)), 2 );
echo "Must Pay ".$total_AmountDue." Immediately, or else I will eat these Pizza";

so i have to repeat this to all of the pizza right?
we have 6 pizza.

here is the code i have made,
i put "1" at the variables for pizza number1.

<?php
	if(($_POST['pica10']) && ($_POST['sq1'])){
	$total_S1pizza = 1 * ($_POST['sq1']);
	$total_S1price = 300 * ( $total_S1pizza );
	}
	if(($_POST['pica14']) && ($_POST['mq1'])){
	$total_M1pizza = 1 * ($_POST['mq1']);
	$total_M1price = 450 * ( $total_M1pizza );
	}
	if(($_POST['pica18']) && ($_POST['lq1'])){
	$total_L1pizza = 1 * ($_POST['lq1']);
	$total_L1price = 650 * ( $total_L1pizza );
	}
 
	
	$total_AmountDue = $total_S1price + $total_M1price + $total_L1price ; 
	echo "Need to bake= ".$total_S1pizza." Small Pizza ".$total_M1pizza." Medium Pizza. ".$total_L1pizza." Large Pizza <br>";
	echo "Must Pay= ".$total_AmountDue."";
	
	$gross_AmountDue = $total_S1price + $total_M1price + $total_L1price ; 
	$tax_Rate = 9.5 ; ## this is for a sales tax rate of 9.5 percent
	$total_AmountDue = round((($gross_AmountDue) * ($tax_Rate / 100)), 2 );
	echo "Must Pay ".$total_AmountDue." Immediately, or else I will eat these Pizza";	
	?>

there's no error if i have checked all of the boxes,
but if i checked only 1 or 2, here's the error

Notice: Undefined index: pica14 in C:\wamp\www\ord1.php on line 239

it is awkward if the costumer needs to order all of the sizes right?
do you know wht to do here......
like, program wont have error if only one/two is being checked.

You can use this construct:

if(isset($_POST['pica10']) && isset($_POST['sq1'])){
//...
if(isset($_POST['pica14']) && isset($_POST['mq1'])){
//...
if(isset($_POST['pica18']) && isset($_POST['lq1'])){
//...

isset() checks if the given variable has an actual value. This will not show the notice you get.

You can use this construct:

if(isset($_POST['pica10']) && isset($_POST['sq1'])){
//...
if(isset($_POST['pica14']) && isset($_POST['mq1'])){
//...
if(isset($_POST['pica18']) && isset($_POST['lq1'])){
//...

isset() checks if the given variable has an actual value. This will not show the notice you get.

the codes:

if(isset($_POST['pica10']) && isset($_POST['sq1'])){
	$total_S1pizza = 1 * ($_POST['sq1']);
	$total_S1price = 300 * ( $total_S1pizza );
	}
	if(isset($_POST['pica14']) && isset($_POST['mq1'])){
	$total_M1pizza = 1 * ($_POST['mq1']);
	$total_M1price = 450 * ( $total_M1pizza );
	}
	if(isset($_POST['pica18']) && isset($_POST['lq1'])){
	$total_L1pizza = 1 * ($_POST['lq1']);
	$total_L1price = 650 * ( $total_L1pizza );
	}
 
	$total_AmountDue = $total_S1price + $total_M1price + $total_L1price ; 
	echo "Need to bake= ".$total_S1pizza." Small Pizza ".$total_M1pizza." Medium Pizza. ".$total_L1pizza." Large Pizza <br>";
	echo "Must Pay= ".$total_AmountDue."<br>";
	
	
	$tax_Rate = 9.5 ; ## this is for a sales tax rate of 9.5 percent
	$total_AmountDue = round((($total_AmountDue) * ($tax_Rate / 100)), 2 );
	echo "Must Pay ".$total_AmountDue." Immediately, or else I will eat these Pizza";

in the form i choose only the small pizza.
the prob now is this:

Notice: Undefined variable: total_M1price in C:\wamp\www\ord1.php on line 250
Notice: Undefined variable: total_L1price in C:\wamp\www\ord1.php on line 250
Notice: Undefined variable: total_M1pizza in C:\wamp\www\ord1.php on line 251
Notice: Undefined variable: total_L1pizza in C:\wamp\www\ord1.php on line 251
then:
Need to bake= 2 Small Pizza Medium Pizza. Large Pizza
Must Pay= 600
Must Pay 57 Immediately, or else I will eat these Pizza

its now on the price..mybe it cant read it cause its not being processed.
i think we should put in every pizza sizes the computation, not at the end.

what do you think guys?
i still hope this will be solved this week or much better earlier...:(

Hi,

The sample script I have provided you is just a sample on how to do things roughly. However, you need to write a simple programming flowchart of how the pizza orgering process may take place, and then based on this flowchart, you create the php codes. It is not the other way, making the flowchart bending around the php codes. It has to be the other way around.

Here is a simple flowchart for the pizza ordering system.
1. Client is presented with the form
2. Client select items and quantities on the form selection
3. Client submit the form

The processing php file for the above, could be something like this
1. Check if the form is submitted and not empty
is form enpty? ->make the client know
is form not empty? -> process the client's input by way of either get or post
2. Filter the processed data from the client's input
condition One: Which pizza size is selected?
Condition two: how many pizza of this size has been selected?

3. Create a switch where we can monitor the clients selections.. Meaning if the client wants 3 small, 2 large, and 7 medium pizza, we are able to process the form.

4. Present the client with the summary of the order.


So, what I want to see in your codes is something like this

if(Small pizza quantity > 0){

## do your stuffs here
}

else if (Medium pizza quantity > 0){

## do you stuffs here for the medium pizza

}

else if(large pizza quantity is > 0){

## do stuffs here for the large pizza

}

else{

echo "your pizza cart is empty";

}

You can also use the php switch function. The possibilities is just too many.

Hi,

The sample script I have provided you is just a sample on how to do things roughly. However, you need to write a simple programming flowchart of how the pizza orgering process may take place, and then based on this flowchart, you can create the php codes. We cannot make the flowchart to bend around the php codes. It has to be the other way around.

Here is a simple flowchart for the pizza ordering system.
1. Client is presented with the form
2. Client select items and quantities on the form selection
3. Client submit the form

The processing php file for the above, could be something like this
1. Check if the form is submitted and not empty
is form enpty? ->make the client know
is form not empty? -> process the client's input by way of either get or post
2. Filter the processed data from the client's input
condition One: Which pizza size is selected?
Condition two: how many pizza of this size has been selected?

3. Create a switch where we can monitor the clients selections.. Meaning if the client wants 3 small, 2 large, and 7 medium pizza, we are able to process the form.

4. Present the client with the summary of the order.


So, what I want to see in your codes is something like this

if(Small pizza quantity > 0){

## do your stuffs here
}

else if (Medium pizza quantity > 0){

## do you stuffs here for the medium pizza

}

else if(large pizza quantity is > 0){

## do stuffs here for the large pizza

}

else{

echo "your pizza cart is empty";

}

You can also use the php switch function. The possibilities are just too many.

WARNING!
you cannot set the default value on the quantity, because it will give the client an automatic 1 of each as order. It could be either blank or 0.. Now, using zero is sometimes can cause errors. However, in small application like this, there should be no problem.

here's what the code i have revised:
i didnt put checkboxes anymore,instead i just used txtboxes
if the customer want that inch they just have to input the quantity.

10" Php300 <input type="text" name="sq1" size="2" value=""/> 
14" Php450 <input type="text" name="mq1" size="2" value=""/> <br>
18" Php650 <input type="text" name="lq1" size="2" value=""/>

then,

if(isset($_POST['sq1'])){
$quant = $_POST['sq1'];
$total_S1pizza = 1 * ($_POST['sq1']);
$total_S1price = 300 * ( $total_S1pizza );
echo  "Pizza CarneNorte 10".$quant."pieces has total of: ".$total_S1price."<br>";

}
if(isset($_POST['mq1'])){
$quant = $_POST['mq1'];
$total_M1pizza = 1 * ($_POST['mq1']);
$total_M1price = 450 * ( $total_M1pizza );
echo "Pizza CarneNorte 14".$quant."pieces has total of:".$total_M1price."<br>";
}
if(isset($_POST['lq1'])){
$quant = $_POST['lq1'];
$total_L1pizza = 1 * ($_POST['lq1']);
$total_L1price = 650 * ( $total_L1pizza );
echo "Pizza CarneNorte 18".$quant."pieces has total of:".$total_L1price."<br>";
}

then for pizza 2:

if(isset($_POST['sq2'])){
$total_S2pizza = 1 * ($_POST['sq2']);
$total_S2price = 650 * ( $total_S2pizza );
echo "Total: ".$total_S2price."<br>";
}

then the total summary of orders at the last part.

$total_AmountDue = $total_S1price + $total_M1price + $total_L1price ; 
echo "Need to bake= ".$total_S1pizza." Small Pizza ".$total_M1pizza." Medium Pizza. ".$total_L1pizza." Large Pizza <br>";
echo "Must Pay= ".$total_AmountDue."<br>";

it doesn't have any error.but even they did not put quantity,
it was still on the summary..
feels redundant

Hi,

I don't have the chance to test my codes below, because I am also busy at school.. NOOO it is not about programming. It should give you what you are trying to make.

PHP functions use in this script.. just in case your professor ask you.. empty(), $_POST, for loop, if/else, and mathematical operators addition, multiplication and !not.

<?php
## A simple pizza delivery system Written 

## layout our pizza options
 # define your maximum quantity inside the select option
 $max_q = 10;
 # define your pizza prices 
 $small = 300;
 $medium = 450;
 $large  = 650;
 
## first we detect if the submit button has been submitted, if not we show our client the form

if (isset($_POST['submit'])){
## the following prevent undefined error beyond the php 5.2.X versions.
$s_quant = "";$m_quant = "";$l_quant = "";$s_cost = "";$m_cost = "";$l_cost = "";
 ## process the form. What matters here the most is to know if the checkbox is ticked
 ## process small pizza if it is tick.
 if(!empty($_POST['pica10'])){
 $s_quant = $_POST['sq'];
 echo "<b>Small pizza</b><br/>Quantity: ".$s_quant."<br/>";
 ## always wrap var for mathematical calculation with parenthesis
 $s_cost = ($small) * ( $s_quant);
 echo "Price: ".$s_cost."<br/>";
 }
 if(!empty($_POST['pica14'])){
 $m_quant = $_POST['mq'];
 echo "<b>Medium pizza</b><br/>Quantity: ".$m_quant."<br/>";
 ## always wrap var for mathematical calculation with parenthesis
 $m_cost = ($medium) * ( $m_quant);
 echo "Price: ".$m_cost."<br/>";
 }
 if(!empty($_POST['pica18'])){
 $l_quant = $_POST['lq'];
 echo "<b>Large pizza</b><br/>Quantity: ".$l_quant."<br/>";
 ## always wrap var for mathematical calculation with parenthesis
 $l_cost = ($large) * ( $l_quant);
 echo "Price: ".$l_cost."<br/>";
 }
 
 $total_gross = (($s_cost) + ($m_cost) + ($l_cost));
 echo "form has been submitted<br/>";
 echo "<h2>Order Summary</h2>";

 echo "We need to make<br/> <b>".$s_quant."</b>Small Pizza. <br/>".$m_quant." Medium Pizza. <br/>".$l_quant." Large Pizza.<br/>";
 
 echo "<h2>Total Amount Due</h2>";
 echo "You must pay ".$total_gross."<br/>";
 echo '<a href="pizza.php" >Order Again</a>';
 
 }
 else{
## form is not submitted, we need to show the form
## we close the php so that we can use html tags
?>
<!-- we show the form to our client -->
<form name="order" action="" method="post">
<!-- we give the first option which is the small pizza -->
<INPUT type=checkbox name="pica10" value="S">10" Php300 
<select name="sq">
<!-- instead of breaking our fingers typing the quantity value, we will use for loop function incrementing the integer by 1 from 1 -->
<?php
for($s_q = 1; $s_q <= $max_q ; $s_q++){
?>
<option><?php echo $s_q;?></option>
<?php
}
?>
</select> 
<br/>
      <INPUT type=checkbox name="pica14" value="M">14" Php450 
	  <select name="mq">
<!-- increment to 10 again for medium-->
  
<?php
for($m_q = 1; $m_q <= $max_q ; $m_q++){
?>
<option><?php echo $m_q;?></option>
<?php
}
?>
</select> 
<br/> 
<INPUT type=checkbox name="pica18" value="L">18" Php650
<select name="lq">
<!-- increment to 10 again for medium-->
	<?php
for($l_q = 1; $l_q <= $max_q ; $l_q++){
?>
<option><?php echo $l_q;?></option>
<?php
}
?>
</select> 
<br/>
	  <input type="submit" name="submit" value="Buy"/>
	  </form>
	  <?php
	  }
	  ?>

please do not hesitate to change this and the rest of your pizza sizes..

if(!empty($_POST['pica14']))

to

if(isset($_POST['pica14']))

or

if($_POST['pica14'])

it won't matter, because everything are wrapped in the if isset clause.

Just a minor correction with the codes..

find line 14

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

change to

if ((isset($_POST['submit'])) && ( (isset($_POST['pica10'])) || (isset($_POST['pica14'])) ||(isset($_POST['pica18']))))

That should take care of the form being submitted without a tick. Now, we have added another php operator used in the script. && (AND) and the || (double pipe or OR)

Thank you so much! Ive done it!
wht im gonna do now is im gonna fix the spacings and stuffs

thank u yr a great help and
sorry for the disturbance:)

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.