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
https://forums.phpfreaks.com/topic/42435-comparison-help-cheers/
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

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

?>

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

?>

 

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

?>

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

?>

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

?>

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.

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.

Archived

This topic is now archived and is 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.