student 100 Posted March 24, 2007 Share Posted March 24, 2007 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 I have this in my form page: Please select one: Return/First-Class <input type="radio" name="type" value="return_first_class"/> Return/Economy <input type="radio" name="type" value="return_economy"/> One-way/First-Class <input type="radio" name="type" value="one_way_first_class"/> One-way/Economy <input type="radio" name="type" value="one_way_economy"/> 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 https://forums.phpfreaks.com/topic/44148-calculation-statements/ Share on other sites More sharing options...
paul2463 Posted March 24, 2007 Share Posted March 24, 2007 for a start off with your code above the instantiation of $type=$_POST['type']; is incorrect you are missing the "$" sign from the front, if that is justa typo on here try a switch statement switch ($type) { case "return_first_class": $final_price = $normal * (($prices * 1.25)*1.75); break; case "return_economy": $final_price = ($normal * ($prices + $prices) * 0.75); break; case "one_way_first_class": $final_price = $normal * ($prices * 1.25); break; default: $final_price = $normal * $prices; break; } print "<p>final price is $final_price.</p>"; Link to comment https://forums.phpfreaks.com/topic/44148-calculation-statements/#findComment-214371 Share on other sites More sharing options...
Barand Posted March 24, 2007 Share Posted March 24, 2007 or <?php $price = 100; $return = 1; // 1 or 0 $first = 1; $factor = 1; if ($return) $factor *= 2 * 0.75; if ($first) $factor *= 1.25; $price *= $factor; echo "Price per person : $price"; ?> Link to comment https://forums.phpfreaks.com/topic/44148-calculation-statements/#findComment-214411 Share on other sites More sharing options...
Barand Posted March 24, 2007 Share Posted March 24, 2007 How many times do you intend to ask this same same question and waste people's time? Do NOT double (or triple) post. Link to comment https://forums.phpfreaks.com/topic/44148-calculation-statements/#findComment-214422 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.