joesoaper Posted January 11, 2017 Share Posted January 11, 2017 This is most probably very simple but after 3 hours I admit I need help <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 Quote Link to comment https://forums.phpfreaks.com/topic/302908-echo-when-greater-than-zero/ Share on other sites More sharing options...
requinix Posted January 11, 2017 Share Posted January 11, 2017 Have you tried a, you know, comparison? With >? Quote Link to comment https://forums.phpfreaks.com/topic/302908-echo-when-greater-than-zero/#findComment-1541275 Share on other sites More sharing options...
joesoaper Posted January 11, 2017 Author Share Posted January 11, 2017 I have tried everything I know.. which admittedly is not that much.. when I ask for help it is only because I have absolutely no idea how to get it right. I generally prefer resolving problems myself. Quote Link to comment https://forums.phpfreaks.com/topic/302908-echo-when-greater-than-zero/#findComment-1541276 Share on other sites More sharing options...
Jacques1 Posted January 11, 2017 Share Posted January 11, 2017 What is the exact value of $total1? var_dump($total1); What exactly have you tried? var_dump($total1 > 0.0); Quote Link to comment https://forums.phpfreaks.com/topic/302908-echo-when-greater-than-zero/#findComment-1541277 Share on other sites More sharing options...
ginerjm Posted January 11, 2017 Share Posted January 11, 2017 (edited) 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 January 11, 2017 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/302908-echo-when-greater-than-zero/#findComment-1541281 Share on other sites More sharing options...
Zane Posted January 12, 2017 Share Posted January 12, 2017 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"; Quote Link to comment https://forums.phpfreaks.com/topic/302908-echo-when-greater-than-zero/#findComment-1541290 Share on other sites More sharing options...
joesoaper Posted January 13, 2017 Author Share Posted January 13, 2017 Thank you all very much, it is very much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/302908-echo-when-greater-than-zero/#findComment-1541333 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.