Jump to content

help:loops


student 100

Recommended Posts

Hi, needing hepl with loops. I think I'd need the if else loop.

I have been given a table of flight prices.Prices from and too 6 different places eg JHB, CT, Durbs, Bloem, E London and Nelspruit.

E.d Ct to JHB R500.

In the form page the customer can chioce between First class and economy. And they can choice between one way or return.

 

But I have been informed that first class flights are 25 % more expensive per traveller and return flights have a discount of 25%. E.g one person from CT to Joburg = (500+500) X 0.75

 

therefore I have for groups:

 

one-way return:

number of people X price

 

One way first class

Number of people x (300 x 1,25)

 

Return economy

number of people X ((300 + 300) x 0.75

 

Return-first class

number of people x (300 x 1,25) x 1.75

 

How would I go about doing this calc? I have my arrays.

Please help

 

Thanks

xxx

Link to comment
Share on other sites

You wouldn't need a loop, if-elseif-else are statements, you would need that.

 

Try something like this:

<?php

$group = $_POST['group'];
$num_people = $_POST['num_people'];

if($group == "one-way"){
$final_price = $num_people * 100;	//your price here
}elseif($group == "one-way-first"){
$final_price = $num_people * (300 * 125);
}else{
echo "Group unknown...";
}

?>

 

hope that helps

Link to comment
Share on other sites

Thanks, I'm almost there can u help:

I have this in my form page:

 

Please select one:<br/>

Return/First-Class <input type="radio" name="type" value="return_first_class"/><br/>

Return/Economy <input type="radio" name="type" value="return_economy"/><br/>

One-way/First-Class <input type="radio" name="type" value="one_way_first_class"/><br/>

One-way/Economy <input type="radio" name="type" value="one_way_economy"/><br/>

 

 

 

Therefore I have four groups, each needing a different calc in my hande_form page.

I have set my arrays out on this page as follows:

 

$Cape_Town=array ('JoBurg'=>'500', 'Durban'=>'600','Bloemfontein'=>'700','East_London'=>'500','Nelspruit'=>'800');

  $JoBurg=array ('Cape_Town'=>'500','Durban'=>'300','Bloemfontein'=>'400','East_London'=>'500','Nelspruit'=>'400');

  $Durban=array ('Cape_Town'=>'600','Joburg'=>'300','Bloemfontein'=>'300','East_London'=>'200','Nelspruit'=>'300');

  $Bloemfontein=array ('Cape_Town'=>'700','Joburg'=>'400','Durban'=>'300','East_London'=>'400','Nelspruit'=>'500');

  $East_London=array ('Cape_Town'=>'500','Joburg'=>'500','Durban'=>'200','Bloemfontein'=>'400','Nelspruit'=>'400');

  $Nelspruit=array ('Cape_Town'=>'800','Joburg'=>'400','Durban'=>'300','Bloemfontein'=>'500','East_London'=>'400');

 

 

  $prices=array('Cape_Town'=>$Cape_Town, 'JoBurg'=>$JoBurg, 'Durban'=>$Durban, 'Bloemfontein'=>$Bloemfontein,

  'East_London'=>$East_London, 'Nelspruit'=>$Nelspruit);

 

 

 

Now I need calculations.(please note $normal refers to the number of people.

 

type = $_POST['type'];

$normal = $_POST['normal'];

 

if($type == "return_first_class"){

$final_price = $normal * (($prices * 1.25)*1.75);

 

 

}elseif($type == "return_economy"){

$final_price = ($normal * ($prices + $prices) * 0.75);

 

 

}elseif($type == "one_way_first_class"){

$final_price = $normal * ($prices * 1.25);

 

 

}else{

$final_price = $normal * $prices;

}

print "<p>final price is  $final_price.</p>";

?>

 

 

 

Its not working. Could u please help?

MUCH appreciated!

 

 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.