Here is the code I’m working on.
function calculate_cuisine_type() {
if ($max == $total2) {
echo $cItalian;
} elseif ($max == $total3) {
echo $cFrench;
} elseif ($max == $total4) {
echo $cChinese;
} elseif ($max == $total5) {
echo $cSpanish;
} elseif ($max == $total6) {
echo $cIndian;
}
}
What I’m trying to do is calculate ‘cuisine types’ for different recipes, my small program uses the google API to return the number of ‘hits’ for a particular recipes title – i.e Wontons – and then calculates a ‘cohesion’ ratio by dividing that number i.e 100, by the returned hits of another query – i.e (Wontons + Chinese = 50). So the cohesion factor would be 2, if you catch my drift.
Now, I can get that to display and calculate fine, but the problem comes when trying to add the final cuisine type to the XML document, would this code have to be in some sort of function? In order it to be called here: (The actual XML addition code works fine)
$ctNode = $xdoc ->createTextNode (*stuff to be added*);
So, essentially what I’m asking is there a way for the final output of an IF statement to be assigned to another variable for use elsewhere, or does this have to be done through a Function, so when the function is called; it returns the final result of the IF statement.
EDIT:
Problem was solved thanks to https://stackoverflow.com/users/1044644/ivo-pereira
Final Code if anyone is interested.
$total = array(
2 => $total2,
3 => $total3,
4 => $total4,
5 => $total5,
6 => $total6
);
$max = max(array($total2, $total3, $total4, $total5, $total6));
echo'The highest cohesion factor is: ' . $max;
function calculate_cuisine_type($max,$total) {
if ($max == $total[2]) {
$type = 'Italian';
} elseif ($max == $total[3]) {
$type = 'French';
} elseif ($max == $total[4]) {
$type = 'Chinese';
} elseif ($max == $total[5]) {
$type = 'Spanish';
} elseif ($max == $total[6]) {
$type = 'Indian';
}
return $type;
}
$type = calculate_cuisine_type($max,$total);
This would help you to organize your code better, in real situation view:
Try to organize this kind of content in arrays, it will help you a lot! And do not forget to pass your data as arguments to the function, otherwise they will not be read in a situation like this.