3

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 the 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.

Jakob Jingleheimer
  • 30,952
  • 27
  • 76
  • 126
  • 2
    The way you're calling the function (with single quotes) *will* pass the literal string. Also, if you need the value of `$NYC_min` in function `cityMinMet` then just **pass** the value to the function. Don't make your code jump through variable variable/global hoops. – webbiedave Sep 15 '11 at 19:49
  • It's perfectly legitimate to use `` for tabular data.
    – Herbert Sep 15 '11 at 20:06
  • @webbiedave: oh cool thanks! how else do I pass the value of $NYC_min to the function without doing it this way? – Jakob Jingleheimer Sep 15 '11 at 20:12
  • @Herbert: I want to stylise the table, and `` tags accept almost no css :(
    – Jakob Jingleheimer Sep 15 '11 at 20:12
  • @jacob: Ah! I see. Some people read articles about not using `` for layout and assume it means "never us `
    ` _anywhere_ for _any_ reason _whatsoever_". Just thought I'd point out that it's okay to use it for it was intended (in case you didn't know). :)
    – Herbert Sep 15 '11 at 22:37

5 Answers5

12

Wrap it in parenthesis:

" . ($NYC - $NYC_min) . "
Sam
  • 26,946
  • 12
  • 75
  • 101
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
2

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

sberry
  • 128,281
  • 18
  • 138
  • 165
2

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.

sillyMunky
  • 1,260
  • 8
  • 13
1

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';
webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • ahh, gotcha. that last one is pretty cool too! Do you know which one would be more efficient? (I'll be running at least 21 of these). – Jakob Jingleheimer Sep 15 '11 at 21:24
  • 1
    If you're doing it numerous times then definitely go with the function. That way if you have to change, say, `'yes'` to `'Y'`, you can do it in just one place. – webbiedave Sep 15 '11 at 21:37
  • btw I ended up condensing everything into a for loop and the functions are called inside it ;) – Jakob Jingleheimer Sep 16 '11 at 14:04
0

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>...";
Adam Bailin
  • 552
  • 4
  • 15