I'm building a table (tho not using <table>
) and populating it with data from mysql. I have the following code to build the "table":
$NYC = // data from mysql, working fine
$NYC_min = 300;
switch($cou_mun){
case "New York":
$NYC++;
break;
//etc
}
function cityMinMet($city){
$city_min= "$city" . "_min";
if($city>$city_min){return "yes";}
else{return "no";};
}
echo "<h3>Table 6: Recruitment by target area</h3>";
echo "<ul>\n<li><span>Target Area</span><span>Number Recruited</span><span>Amount under minimum</span><span>Minimum</span><span>Minimum met?</span></li>";
echo "<li><span>12 Jurisdictions with Highest AIDS Prevalance</span><span></span><span></span><span></span><span></span></li>";
echo "<li><span>New York</span><span>$NYC</span><span>" . $NYC_min-$NYC . "</span><span>$NYC_min</span><span>"; cityMinMet('$NYC'); echo "</span></li>";
I encounter a problem with " . $NYC_min-$NYC . "
: it breaks t开发者_JAVA百科he row (the row gets interpreted as ending just before " . $NYC_min-$NYC . "
. However, if I have <span>$NYC_min-$NYC</span>
(not as an additional component of the echo), the value of the cell is printed as 300-500
instead of 200
.
Also I'm not sure I've setup my function properly (but this is not breaking the table/row). From cityMinMet('$NYC')
I want the literal string $NYC
(and not its value) sent to the function. Inside the function I need to append _min
to $NYC
and then call $NYC_min
and it return with its value.
EDIT: I changed the order of $NYC
and $NYC_min
in the equation.
Wrap it in parenthesis:
" . ($NYC - $NYC_min) . "
For the " . $NYC-$NYC_min . "
, change to " . ($NYC-$NYC_min) . "
To pass the literal string $NYC
use "\$NYC"
. Note, you need to use double quotes "
instead of single quotes '
here, otherwise it will pass the string literal \$NYC
As @webbiedave points out in his comment, passing it as '$NYC'
should already be sending the string literal $NYC
Your first problem: $NYC - $NYC_min
is being interpreted as a string (subtly cast as such) because you are concatenating it with a string. You should perform the maths before the echo and store it in a variable which you write in there (clean) or you can put it in brackets so its interpreted before the php parser considers the string concatenation context.
From cityMinMet('$NYC') I want the literal string $NYC (and not its value) sent to the function.
The way you're calling the function (with single quotes) will pass the literal string.
However, if you need the value of $NYC_min
in function cityMinMet
then just pass the value to the function:
function cityMinMet($city, $city_min)
{
if ($city > $city_min) {
return "yes";
} else {
return "no";
}
}
In your calling code do:
echo cityMinMet($NYC, $NYC_min);
Or you can forgo the function all together and just do:
echo ($city > $city_min) ? 'yes' : 'no';
Might be easiest to make a quick function to do what you need to do:
function calculate($nyc, $nyc_min) {
return $nyc - $nyc_min;
}
then replace what you have with:
echo "<li><span>New York</span><span>$NYC</span><span>" . calculate($NYC,$NYC_min) . "</span>...";
精彩评论