Jump to content

comparison help cheers.


redarrow

Recommended Posts

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";

}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

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;
}

?>

 

Link to comment
Share on other sites

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 />";
	}
}
}

?>

Link to comment
Share on other sites

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 ";
}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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
	}
}
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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