473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Formatting zero results

I have just written my first php script and now have a dynamic table updated from MySql. The table contains a sequence of numbers which are sometimes zero.

Is there a neat way to ECHO a space or – character when there is a 0 to make the table look more elegant? The best I can do is with an IF ELSE construct:

Expand|Select|Wrap|Line Numbers
  1. //Present code
  2.       <td><?php echo $row_Recordset1['Price02']; ?></td>
  3. ..
  4.       <td><?php echo $row_Recordset1['Price10']; ?></td>
  5.  
  6. //best I can do
  7.       <td><?php if ($row_Recordset1['Price02'] == 0) {echo “-“;} else {echo $row_Recordset1['Price05']; }?></td>
  8. ..
  9.       <td><?php if ($row_Recordset1['Price10'] == 0) {echo “-“;} else {echo $row_Recordset1['Price05']; }?></td>
  10.  
Thanks
Mar 4 '10 #1
6 1239
zorgi
431 Expert 256MB
I guess you could do:

Expand|Select|Wrap|Line Numbers
  1. echo($row_Recordset1['Price02'])?($row_Recordset1['Price05']):("-");
  2.  
Look at the Comparison Operators and scroll down to Ternary Operators
Mar 4 '10 #2
hi, that's neat, but I could not get this to work:

Expand|Select|Wrap|Line Numbers
  1. <td><?php echo empty($row_Recordset1['Price02']))? '-'; ?></td>
  2. <td><?php echo empty($row_Recordset1['Price03']))? '-'; ?></td>
  3.  
Mar 5 '10 #3
Dormilich
8,658 Expert Mod 8TB
because you didn’t use the ternary operator.
Mar 5 '10 #4
I understood the first '?' to be the 'ternary operator'
Sorry, this is my 3rd day with PHP.. thanks!
Mar 5 '10 #5
Dormilich
8,658 Expert Mod 8TB
then where does the "ternary" come from? btw. it has been exactly explained in the given link.

the ternary operator:
expression ? expression(true) : expression(false);

as of PHP 5.3 you could also use
expression ?: expression(false);
Mar 5 '10 #6
Hi dormilich, I see I'm lacking a semicolon. It's confusing, as ternary suggests 3 but several examples only have 2 parts.. anyway, I added the semicolons and here are the equivalent statements:

Expand|Select|Wrap|Line Numbers
  1. <td><?php echo empty($row_Recordset1['P02']) ? " " : $row_Recordset1['P02']; ?></td>
  2. <td><?php if ($row_Recordset1['P03'] == 0) {echo " ";} else {echo $row_Recordset1['P03'];} ?></td>
  3.  
The 'ternary' version is rather cool!
Mar 5 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Pascal | last post by:
Hello, I've a float number 123456789.01 and, I'de like to format it like this "123 456 789.01". Is this possible with % character?
2
by: CSDunn | last post by:
Hello, In an Access 2003 ADP Subform, I am trying to set the BackGround color of three textbox fields (LSScore, RScore, WScore) in the current record of the subform to zero (black) if the value of...
2
by: Sara | last post by:
The problem: Conditional formatting bold, red when field Value < date() sets the field background to white - always - whether condition is met or not. I want the field unfilled and just red/bold...
4
by: Dave Brydon | last post by:
Access 2003 I have a combo box in my personnel table, which draws its data from a trade code table; the original field in the code table, is numeric, Long Integer, and formatted with 5 zero's . ...
10
by: Coleen | last post by:
Hi all :-) I have a weird formatting problem with an HTML table that I am populating using VB.Net and HTML. Here is the snippet of code for the cell I'm trying to format: Dim...
8
by: G.Ashok | last post by:
Hi, I have created CultureInfo object and specified required digit grouping in it. The one of the overloaded ToString methods of Decimal type has parameters to format the value with required...
2
by: jodyblau | last post by:
I'm not certain that what I am trying to do is possible; in any event I haven't been able to figure it out. Here is what I am trying to do: I have one table that has a list of cases I'm working...
8
by: gopal | last post by:
Hi I am trying to write to log file but the formatting of string is not properly aligned in log file result The results looks some thing like this OK move.xml Successful OK ...
8
by: Typehigh | last post by:
I have many text fields with conditional formatting applied, specifically when the condition is "Field Has Focus". Without any events associated with the fields the conditional formatting works...
8
by: victor.herasme | last post by:
Hi, i am building a little script and i want to output a series of columns more or less like this: 1 5 6 2 2 8 2 9 5 The matter is that i don't know in advance how many columns...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.