Reply to Re: not displaying null data when using number_format

Your name:

Reply:


Posted by Steve on 11/01/06 17:02

| 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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация