|
Posted by linda on 11/01/06 17:56
"Steve" <no.one@example.com> wrote in message
news:js42h.92$AR2.71@newsfe04.lga...
>| Hi Martin,
> |
> | I did actually an if along those lines:
> |
> | [php]
> |
> | // Fetch and print all the records.
> |
> | while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
> | $n = ($row['price']);
> | if ($n) {
> | number_format($n,2);
>
> hey again, linda!
>
> you are almost doing this correctly...and you're coming along well. what
> you're doing is only printing related information if there is a non-null
> price. notice that null evaluates to 0 as does '' ... this relates to your
> IF statement. what i gather from your op is that you only want to do such
> evaluation such that you don't return 0 if the price is ''...but you
> ALWAYS
> want to show the data for the row regardless of price. am i correct?
>
> to fix this, remove your IF block but keep the html that is inbetween the
> block. however, before printing the html output, simply do this to format
> correctly:
>
> $n = $row['price'];
> $n = $n == '' ? '' : ($n == 0 ? 0 : number_format($row['price'], 2));
> //begin outputing your data in html here
>
> two other tips. first, make your variable names more meaningful...mixed in
> with a bunch of code, $n makes the programmer eventually get pissed and/or
> confused when editing/reviewing the code.
>
> second, there is no need to have $n...and you can meet the first
> suggestion's requirements by doing this:
>
> $row['price'] = $row['price'] == '' ? '' : ($row['price'] == 0 ? 0 :
> number_format($row['price'], 2));
> //begin outputing your data in html here using $row['price'] in your code
> rather than $n.
>
> hth,
>
> me
>
I'm going to need to polish that hallo for you ;-) You've come to the
rescue once again. This one was driving me nuts! I tried all sorts of
search patterns for how to not display null data with a number_format, but
couldn't find anything. And when I tried myself with if statement, I just
kept getting the same result, i.e entire data for that id being removed. I
actually woke up in the middle of the night last night with a "I wonder if
this will work" scenario, and then couldn't go back to sleep until I got up
and tried it. ;-)
Ok now that you've shown me how to do it, could you translate what's
happening for me, if you don't mind that is? I think I know what's going
on, but not entirely sure, ;-) and don't wont to show the world my lack of
knowledge in case I'm wrong by posting what I think. lol... vanity and all
that!
Point taken and noted for the $n variable, for me it denoted number, but I
understand what you mean, for others it was meaningless.
Best wishes as always, and much gratitude!
Linda
Navigation:
[Reply to this message]
|