Formatting zero results

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • garfieldsevilla
    New Member
    • Feb 2010
    • 37

    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:

    Code:
    //Present code
          <td><?php echo $row_Recordset1['Price02']; ?></td>
    ..
          <td><?php echo $row_Recordset1['Price10']; ?></td>
    
    //best I can do
          <td><?php if ($row_Recordset1['Price02'] == 0) {echo “-“;} else {echo $row_Recordset1['Price05']; }?></td>
    ..
          <td><?php if ($row_Recordset1['Price10'] == 0) {echo “-“;} else {echo $row_Recordset1['Price05']; }?></td>
    Thanks
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    I guess you could do:

    Code:
    echo($row_Recordset1['Price02'])?($row_Recordset1['Price05']):("-");
    Look at the Comparison Operators and scroll down to Ternary Operators

    Comment

    • garfieldsevilla
      New Member
      • Feb 2010
      • 37

      #3
      hi, that's neat, but I could not get this to work:

      Code:
      <td><?php echo empty($row_Recordset1['Price02']))? '-'; ?></td>
      <td><?php echo empty($row_Recordset1['Price03']))? '-'; ?></td>

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        because you didn’t use the ternary operator.

        Comment

        • garfieldsevilla
          New Member
          • Feb 2010
          • 37

          #5
          I understood the first '?' to be the 'ternary operator'
          Sorry, this is my 3rd day with PHP.. thanks!

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            then where does the "ternary" come from? btw. it has been exactly explained in the given link.

            the ternary operator:
            expression ? expression(true ) : expression(fals e);

            as of PHP 5.3 you could also use
            expression ?: expression(fals e);

            Comment

            • garfieldsevilla
              New Member
              • Feb 2010
              • 37

              #7
              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:

              Code:
              <td><?php echo empty($row_Recordset1['P02']) ? " " : $row_Recordset1['P02']; ?></td>
              <td><?php if ($row_Recordset1['P03'] == 0) {echo " ";} else {echo $row_Recordset1['P03'];} ?></td>
              The 'ternary' version is rather cool!

              Comment

              Working...