Jump to content

Recommended Posts

This is most probably very simple but after 3 hours I admit I need help  :sweat:

 <tr>
                  <td ><input name="qty1"   size="1"   type="text"    value="<?php echo !empty($qty1)?$qty1:'';?>"     > </td>
                  <td ><input name="desc1"  size="70" type="text"     value="<?php echo !empty($desc1)?$desc1:'';?>"   > </td>
                  <td ><input name="unit1"  size="5"   type="text"    value="<?php echo !empty($unit1)?$unit1:'';?>"   ></td>
                  <td ><input name="disc1"  size="1"   type="text"    value="<?php echo !empty($disc1)?$disc1:'';?>"   ></td>
                  <td ><input name="total1" size="6"   type="text"    READONLY value="<?php echo !empty($total1)?$total1:'';?>" ></td>
                  </tr>

In this row I want to only echo the total1 only IF it is greater than Zero. It currently shows 0.00 which i would rather not have there. For some reason I just cannot get it right.  I am new to PHP PDO so maybe that might be the problem. If you need more of the code please let me know and I will post it

 

Link to comment
https://forums.phpfreaks.com/topic/302908-echo-when-greater-than-zero/
Share on other sites

Things could be easier to achieve and understand if you separate your php logic completely from your html. Do your tests/comparison and assign your output vals to vars and only include them in your html code, rather than burying snippets of php code here and there.

 

if (!empty($qty1)
$show_qty = $qty1;
else
$show_qty = '';
(etc.etc.etc.)
$code=<<<heredocs
<tr>
<td><input name="qty1" size="1" type="text" value="$show_qty1"></td>
<td><input name="desc1" size="70" type="text" value="$show_desc"></td>
<td><input name="unit1" size="5" type="text" value="$show_unit1"></td>
<td><input name="disc1" size="1" type="text" value="$show_disc1"></td>
<td><input name="total1" size="6" type="text" value="$show_total1" READONLY></td>
</tr>
heredocs;
echo $code
..
..

Edited by ginerjm

If you didn't know already, the way you are outputting those values is by the use of what's known as a ternary operator.  It generally works like a miniature IF statement.  You have a condition clause, a true clause, and a false clause. 

(condition) ? (what to do if true) : (what to do if false);

In your case right now, you're simply checking to see if a value is empty or not, by using PHP's empty function.  And, if you look in the manual, you'll see that what the empty() function takes a variable and returns to you a true/false value depending on if the variable is in fact empty or not.  Or, in the words of the manual:
 

A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.


What you're asking for is how to change that condition to mean "is it greater than zero?" and that's as simple as changing the condition to be $total1 > 0

 

Essentially making it this

$var = $total1 > 0 ? true : false;

You're wanting to echo things so using simply true or false in the above example won't work.  You'll have to replace those with strings.

echo $total1 > 0 ? "greater than zero" : "not greater than zero";
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.