redarrow Posted March 13, 2007 Share Posted March 13, 2007 Hi there i am currently learning how to do comparison on bottle's of drinks what i whant to no if i add another two drinks to be compered how do i do the if statement. example. <?php //lieter's $drink1=1.0; $drink2=2.5; $drink3=3.0; $drink4=5.5; //price $price_drink1=1.00; $price_drink2=2.00; $price_drink3=2.50; $price_drink4=1.99; // divession $drink1_result=$drink1 / $price_drink1; $drink2_result=$drink2 / $price_drink2; $drink3_result=$drink3 / $price_drink3; $drink4_result=$drink4 / $price_drink4; //if if($drink1_result < $drink2_result){ echo " drink 1 is a better deal"; }else{ echo" drink 2 is the better deal"; } //if if($drink3_result < $drink4_result){ echo" drink 3 is a better deal"; }else{ echo " drink 4 better deal"; } ?> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 i dont understand what your problem is. why couldnt you just repeat what is currently being done for two new variables? Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 this is the code but the result's are wrong it shows that drink1 and drink 3 are the best deal but i want only drink1 to show unless drink3 or drink4 is the better deal. also your notice that drink 3 is not correct as drink4 is your see. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 i totally lost here. what exactly are you comparing? vol/price? do you only want to compare 1 to 2 then 3 to 4 then 5 to 6 or do you want to compare 1 to 2 then 3 then 4 then 5 etc etc? either way, this may be done better with some functions and arrays - let me know exactly what you are trying to do Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 solved nearly now need it in a function cheers. <?php //lieter's $drink1=1.0; $drink2=2.5; $drink3=3.0; $drink4=5.5; $price_drink1=1.00; $price_drink2=2.00; $price_drink3=2.50; $price_drink4=1.99; $drink1_result=$drink1 / $price_drink1; $drink2_result=$drink2 / $price_drink2; $drink3_result=$drink3 / $price_drink3; $drink4_result=$drink4 / $price_drink4; if($drink1_result < $drink2_result){ echo " drink 1 is a better deal"; exit; }else{ echo" drink 2 is the better deal"; exit; } if($drink3_result > $drink4_result){ echo" drink 3 is a better deal"; exit; }else{ echo " drink 4 is abetter deal"; exit; } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 This is the first stage of learning functions to compare so yes please post an example of the solved solution cheers. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 this way works without me knowing the values so that good hay? solved <?php //lieter's $drink1=1.0; $drink2=2.5; $drink3=3.0; $drink4=5.5; $price_drink1=1.00; $price_drink2=2.00; $price_drink3=2.50; $price_drink4=1.99; $drink1_result=$drink1 / $price_drink1; $drink2_result=$drink2 / $price_drink2; $drink3_result=$drink3 / $price_drink3; $drink4_result=$drink4 / $price_drink4; if($drink1_result < $drink2_result || $drink1_result > $drink2_result){ echo " drink 1 is a better deal"; exit; }else{ echo" drink 2 is the better deal"; exit; } if($drink3_result < $drink4_result || $drink3_result > $drink4_result){ echo" drink 3 is a better deal"; exit; }else{ echo " drink 4 is abetter deal"; exit; } ?> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 try this out <?php function getValue($vol, $price){ return $vol/$price; } $drinks = array('1.0' => '1', '2.5' => '2', '3' => '2.5'); $drink_num = 0; foreach($drinks as $vol => $price){ $drink_num++; $cur_drink_val = getValue($vol, $price); echo '<h3>Drink #'. $drink_num ." Value: ". $cur_drink_val ."</h3>"; $comp_drink_num = 0; foreach($drinks as $v => $p){ $comp_drink_num++; if($vol != $v){ $comp_drink_val = getValue($v, $p); $deal = ($cur_drink_val > $comp_drink_val) ? $comp_drink_num : $drink_num; $no_deal = ($cur_drink_val < $comp_drink_val) ? $comp_drink_num : $drink_num; echo $deal ." is better than ". $no_deal ."<br />"; } } } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 below i marked a bit of the array that dosent work can you tell me why and testing your code cheers. <?php //lieter's $drink=array(); $drink[]=1.0; $drink[]=2.5; $drink[]=3.0; $drink[]=2.0; print_r($drink); $price_drink=array(); $price_drink[]=1.0; $price_drink[]=2.0; $price_drink[]=2.5; $price_drink[]=1.0; print_r($price_drink); $drink_result=array(); // this part wont work why? <<<<<<<<<<<<<<<<<<<<<<<<<< $drink_result[]=$drink/$price_drink; $drink_result[]=$drink/$price_drink; $drink_result[]=$drink/$price_drink; $drink_result[]=$drink/$price_drink; print_r($drink_result); if($drink_result[0] < $drink_result[1] || $drink_result[0] > $drink_result[1] || $drink_result[2] < $drink_result[3] || $drink_result[2] > $drink_result[3]){ echo " Drink "; } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 your codes verry nice but i never learn that will i so let me no what wrong with the above code please and yes cheers i have to study all those statements and codes ok. i need it simple not a scincetist lol Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 oh this is your homework? Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 no it for me i am to old for school i am 33 i am old lol Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 would you like for me to explain what i wrote? Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 yes please you are kind cheers mate that would be grate i no most of it but i need it in baby terms ok cheers realy give it to me like your exsplaining to a dumb ass ok, include the statement like getvalue to me go for it lol Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 <?php /** * this function takes $vol and price and returns $vol/$price */ function getValue($vol, $price){ return $vol/$price; } /** * this drinks array sets the key as the vol and the value as price * array($vol => $price) */ $drinks = array('1.0' => '1', '2.5' => '2', '3' => '2.5', '5.5' => '1.99', '6.2' => '2.14', '8.9' => '6.9'); $drink_num = 0; //the current drink number foreach($drinks as $vol => $price){ //foreach loop through the drinks array $drink_num++; //adds one to the drink number $cur_drink_val = getValue($vol, $price); //calls the function getValue and sets the result to $cur_drink_val echo '<h3>Drink #'. $drink_num ." Value: ". $cur_drink_val ."</h3>"; //echos drink # and its value $comp_drink_num = 0; //the current drink number that the current drink will be compared to foreach($drinks as $v => $p){ //loop through the drinks array again while still inside of the first loop to compare the values $comp_drink_num++; //increse the comp drink number if($vol != $v){ //if $vol = $v do not compare the two becasue they are more than likely the same in the array $comp_drink_val = getValue($v, $p); //calls the function getValue and sets the result to $comp_drink_val $deal = ($cur_drink_val > $comp_drink_val) ? $comp_drink_num : $drink_num; //sets the deal number. this says if cur_drink val is greater than comp_drink val set deal eqal to comp_drink_num if not set it to drink_num $no_deal = ($cur_drink_val < $comp_drink_val) ? $comp_drink_num : $drink_num; //reverse the last set of logic to set the not deal echo $deal ." is better than ". $no_deal ."<br />"; //echo the deal no deal line } } } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 thank you so much that really looks good your a star mate can you try to get rid of them squre brackets so i can print it please cheers m8 study time lol. ps. Have you wrote a book on php ? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 you're welcome, just copy and paste from the box. lol @ writing a book, im still new to programming as a whole. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 I got it in my zend api ok And it is grate can you kindly tell me or post the links that you use to learn php becouse, I have been doing php for around 2 years know and i find it a verry long process to learn all the codes so maybe your hitting the correct information or using the correct links to learn properly can you kindly shere your secreat please cheers. your grate and i will study your code ok thank you. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 i would say go through the tutorials on this site - phpfreaks.com study what you need to know. there are a million "learn php in x days" type books that will teach you the basics. i've learned just by doing and over time, i've gotten better Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 13, 2007 Author Share Posted March 13, 2007 Ok m8 i see what ur saying the same old story there is no quick solution i got all the books that anyone can throw at me, and also got all the dvd learning totural's so i have to study harder ok m8. Thank u for your example cheers all the best redarrow. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.