rjsws Posted August 7, 2008 Share Posted August 7, 2008 If input field number_field is blank how do I insert a zero in there rather then leave it empty? For example: $number_field = $_POST['number_field'] ; $blank = 0 ; if ($number_field == "") { $number_field = $blank ; } else { $a_lot_price = ???; } I'm not really sure what I'm doing here... If the field is blank I want it to think there's a zero in the field. If the form is not blank then use the origional numbers. I think that makes since... -Rob Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/ Share on other sites More sharing options...
ratcateme Posted August 7, 2008 Share Posted August 7, 2008 what you had is good just like this if ($number_field == "") { $number_field = $blank ; } your file will remain what it was originally if it is not empty Scott. Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610510 Share on other sites More sharing options...
rjsws Posted August 7, 2008 Author Share Posted August 7, 2008 what you had is good just like this if ($number_field == "") { $number_field = $blank ; } your file will remain what it was originally if it is not empty Scott. Wow... Didn't know it was that easy. I have been trying to figure this out for about an hour... Thank you so much Scott for your help and quick reply!!! That worked perfect! Now if I have two fields number_field and number_field_plus on my forms, and want the number_field_plus plus field to be zero if the number_field is zero, how do i do that? $number_field = $_POST['number_field'] ; $blank = 0 ; $number_field_plus = $number_field+500 ; if ($number_field == "") { $number_field = $blank ; $number_field_plus = $blank ; } This is the last question i have on the subject -Rob Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610516 Share on other sites More sharing options...
ratcateme Posted August 7, 2008 Share Posted August 7, 2008 thats good and you dont have to define $blank befor hand you can just write $number_field_plus = 0; php can take most random coding you throw at it unlike some other lanuages Scott. Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610518 Share on other sites More sharing options...
papaface Posted August 7, 2008 Share Posted August 7, 2008 Easiest thing to do is: foreach ($_POST as $k => $v) { if (trim($v) == "") { $_POST[$k] = 0; } } Place it at the top of your code. It will automatically replace any blank form fields with a 0 value. Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610525 Share on other sites More sharing options...
rjsws Posted August 7, 2008 Author Share Posted August 7, 2008 Thank you everyone fo your help with this. I decided to use if ($number_field == "") { $number_field = $blank ; $number_field_plus = $blank ; } This makes both fields $0 Thanks again everyone -Rob Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610540 Share on other sites More sharing options...
papaface Posted August 7, 2008 Share Posted August 7, 2008 Thank you everyone fo your help with this. I decided to use if ($number_field == "") { $number_field = $blank ; $number_field_plus = $blank ; } This makes both fields $0 Thanks again everyone -Rob Not very good use of code tbh as $number_field may have a value, but $number_field_plus may not and will therefore remain blank. My code is the best. Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610542 Share on other sites More sharing options...
rjsws Posted August 7, 2008 Author Share Posted August 7, 2008 Not very good use of code tbh as $number_field may have a value, but $number_field_plus may not and will therefore remain blank. My code is the best. Ok... Here 's the code that i am working with... $r_prop_price = $_POST['r_prop_price'] ; $r_prop_base = 5000 ; $r_prop_rent = 300 ; $r_prop_sum_a = $r_prop_price+$r_prop_base ; $r_prop_sum = $r_prop_sum_a/$r_prop_rent ; if ($r_prop_price == "") { $r_prop_price = $blank ; $r_prop_sum = $blank ; } $r_prop_sum_for = number_format($r_prop_sum, 0); How can I clean that up and make it work with your code? -Rob Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610544 Share on other sites More sharing options...
papaface Posted August 7, 2008 Share Posted August 7, 2008 if ($_POST) { foreach ($_POST as $k => $v) { if (trim($v) == "") { $_POST[$k] = 0; } } $r_prop_price = $_POST['r_prop_price'] ; $r_prop_base = 5000 ; $r_prop_rent = 300 ; $r_prop_sum_a = $r_prop_price+$r_prop_base ; $r_prop_sum = $r_prop_sum_a/$r_prop_rent ; } Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610545 Share on other sites More sharing options...
rjsws Posted August 7, 2008 Author Share Posted August 7, 2008 if ($_POST) { foreach ($_POST as $k => $v) { if (trim($v) == "") { $_POST[$k] = 0; } } $r_prop_price = $_POST['r_prop_price'] ; $r_prop_base = 5000 ; $r_prop_rent = 300 ; $r_prop_sum_a = $r_prop_price+$r_prop_base ; $r_prop_sum = $r_prop_sum_a/$r_prop_rent ; } That code doesn't leave my r_prop_sum or my r_prop_price field with $0 It leaves the price field blank and the sum field with $17 (500/300). I need both fields to be $0. -Rob Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610548 Share on other sites More sharing options...
papaface Posted August 7, 2008 Share Posted August 7, 2008 Try: if ($_POST) { foreach ($_POST as $k => $v) { if ($v == "") { $_POST[$k] = 0; } } $r_prop_price = $_POST['r_prop_price'] ; $r_prop_base = 5000 ; $r_prop_rent = 300 ; $r_prop_sum_a = $r_prop_price+$r_prop_base ; $r_prop_sum = $r_prop_sum_a/$r_prop_rent ; } Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610549 Share on other sites More sharing options...
rjsws Posted August 7, 2008 Author Share Posted August 7, 2008 Try: if ($_POST) { foreach ($_POST as $k => $v) { if ($v == "") { $_POST[$k] = 0; } } $r_prop_price = $_POST['r_prop_price'] ; $r_prop_base = 5000 ; $r_prop_rent = 300 ; $r_prop_sum_a = $r_prop_price+$r_prop_base ; $r_prop_sum = $r_prop_sum_a/$r_prop_rent ; } Damn your quick! I'm sorry the previous one that i tested was blank as well. I forgot that i added $r_prop_sum_for = number_format($r_prop_sum, 0); in with yours. Both are blank without that extra line but with both of them come up with $17 for the sum field. -Rob -Rob Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610552 Share on other sites More sharing options...
papaface Posted August 7, 2008 Share Posted August 7, 2008 What are you putting in the form fields when you submit? Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610553 Share on other sites More sharing options...
rjsws Posted August 7, 2008 Author Share Posted August 7, 2008 Ahhhh.... Good call... <td class="c"><input type=text class=lot_value name=r_prop_price size=20 value="<? echo $r_prop_price; ?>"></td> <td class="d"><input type=text class=lot_value name=r_prop_per size=20 value="$<? echo $r_prop_sum_for; ?>"></td> -Rob Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610554 Share on other sites More sharing options...
papaface Posted August 7, 2008 Share Posted August 7, 2008 change to: <td class="c"><input type="text" class="lot_value" name="r_prop_price" size="20" value="<?php echo $r_prop_price; ?>"></td> <td class="d"><input type="text" class="lot_value" name="r_prop_per" size="20" value="$<?php echo $r_prop_sum_for; ?>"></td> and try: foreach ($_POST as $k => $v) { if ($v == "") { $_POST[$k] = 0; } } $r_prop_price = $_POST['r_prop_price'] ; $r_prop_base = 5000 ; $r_prop_rent = 300 ; $r_prop_sum_a = $r_prop_price+$r_prop_base ; $r_prop_sum = $r_prop_sum_a/$r_prop_rent ; Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610556 Share on other sites More sharing options...
rjsws Posted August 7, 2008 Author Share Posted August 7, 2008 Check your PM's please Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610560 Share on other sites More sharing options...
papaface Posted August 7, 2008 Share Posted August 7, 2008 Try: <HTML> <TITLE> RJSWS - mafia Wars </TITLE> <body> <font face=verdana size=2> <?php foreach ($_POST as $k => $v) { if (!isset($_POST[$k]) || trim($_POST[$k]) == "" || $_POST[$k] == "" ) { $_POST[$k] = 0; } } $a_lot_price = $_POST['a_lot_price'] ; $a_lot_base = 0 ; $a_lot_rent = 100 ; $a_lot_sum_a = $a_lot_price+$a_lot_base ; $a_lot_sum = $a_lot_sum_a/$a_lot_rent ; $a_lot_sum_for = number_format($a_lot_sum, 0); $c_lot_price = $_POST['c_lot_price'] ; $c_lot_base = 0 ; $c_lot_rent = 300 ; $c_lot_sum_a = $c_lot_price+$c_lot_base ; $c_lot_sum = $c_lot_sum_a/$c_lot_rent ; $c_lot_sum_for = number_format($c_lot_sum, 0); $p_lot_price = $_POST['p_lot_price'] ; $p_lot_base = 0 ; $p_lot_rent = 2000 ; $p_lot_sum_a = $p_lot_price+$p_lot_base ; $p_lot_sum = $p_lot_sum_a/$p_lot_rent ; $p_lot_sum_for = number_format($p_lot_sum, 0); $b_lot_price = $_POST['b_lot_price'] ; $b_lot_base = 0 ; $b_lot_rent = 8000 ; $b_lot_sum_a = $b_lot_price+$b_lot_base ; $b_lot_sum = $b_lot_sum_a/$b_lot_rent ; $b_lot_sum_for = number_format($b_lot_sum, 0); $r_prop_price = $_POST['r_prop_price'] ; $r_prop_base = 5000 ; $r_prop_rent = 300 ; $r_prop_sum_a = $r_prop_price+$r_prop_base ; $r_prop_sum = $r_prop_sum_a/$r_prop_rent ; ?> <head> <title></title> <style type="text/css"> tr.a { background-color: Aqua; } tr.a1 { background-color: lime; } td.b { color: black; width: 200px; } .lot_value { color: red; text-align: center; } td.c_title { color: black; width: 150px; text-align: center; } tr.blank { background-color: black; height: 3px; } </style> </head> <body> <form method=post action=mafia_prices.php> <table> <tr class="a"> <td class="c_title"></td> <td class="c_title">Price Per Lot</td> </tr> <tr class="a"> <td class="b">Abandoned Lot</td> <td class="c"><input type="text" class="lot_value" name=a_lot_price size="20" value="<? echo $a_lot_price; ?>"></td> <td class="d"><input type="text" class="lot_value" name=a_lot_per size="20" value="$<? echo $a_lot_sum_for; ?>"></td> </tr> <tr class="a1"> <td class="b">Commercial Block</td> <td class="c"><input type="text" class="lot_value" name=c_lot_price size="20" value="<? echo $c_lot_price; ?>"></td> <td class="d"><input type="text" class="lot_value" name=c_lot_per size="20" value="$<? echo $c_lot_sum_for; ?>"></td> </tr> <tr class="a"> <td class="b">Prime Downtown Lot</td> <td class="c"><input type="text" class="lot_value" name=p_lot_price size="20" value="<? echo $p_lot_price; ?>"></td> <td class="d"><input type="text" class="lot_value" name=p_lot_per size="20" value="$<? echo $p_lot_sum_for; ?>"></td> </tr> <tr class="a1"> <td class="b">Beachfront Property</td> <td class="c"><input type="text" class="lot_value" name=b_lot_price size="20" value="<? echo $b_lot_price; ?>"></td> <td class="d"><input type="text" class="lot_value" name=b_lot_per size="20" value="$<? echo $b_lot_sum_for; ?>"></td> </tr> <tr class="blank"> <td></td> <td></td> </tr> <tr class="a"> <td class="b">Rent House</td> <td class="c"><input type="text" class="lot_value" name="r_prop_price" size="20" value="<?php echo $r_prop_price; ?>"></td> <td class="d"><input type="text" class="lot_value" name="r_prop_per" size="20" value="$<?php echo $r_prop_sum_for; ?>"></td> </tr> <tr class="a1"> <td class="b">Italian Restaurant</td> <td class="c"><input type="text" class="lot_value" name="i_prop" size="20" value=""></td> </tr> <tr class="a"> <td class="b">Apartment Complex</td> <td class="c"><input type="text" class="lot_value" name="a_prop" size="20" value=""></td> </tr> <tr class="a1"> <td class="b">Valu-Mart</td> <td class="c"><input type="text" class="lot_value" name="v_prop" size="20" value=""></td> </tr> <tr class="a"> <td class="b">Office Building</td> <td class="c"><input type="text" class="lot_value" name="o_prop" size="20" value=""></td> </tr> <tr class="a1"> <td class="b">5-Star Hotel</td> <td class="c"><input type="text" class="lot_value" name="5_prop" size="20" value=""></td> </tr> <tr class="a"> <td class="b">Mega Casino</td> <td class="c"><input type="text" class="lot_value" name="m_prop" size="20" value=""></td> </tr> </table> <input type=submit value="Submit" > </form> </body> </html> </font> </body> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610570 Share on other sites More sharing options...
rjsws Posted August 7, 2008 Author Share Posted August 7, 2008 Well... I thought I had it all worked out... But I don't. The code above works great but the $17 is still in the r_prop_per field. All the other zeros appear. It's still taking the 5000/300=17 -Rob Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610582 Share on other sites More sharing options...
rjsws Posted August 7, 2008 Author Share Posted August 7, 2008 I was messing around with it some more and noticed that if I put dollar amounts in any of the "prop_base =" then the dollar amount comes up. It's not just with the r_prop_per field. It's all of it. It just wasn't noticed because they were set to zero. Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-610583 Share on other sites More sharing options...
rjsws Posted August 8, 2008 Author Share Posted August 8, 2008 i just used the code from page 1. Thanks again for your help -Rob Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-611456 Share on other sites More sharing options...
Andy-H Posted August 8, 2008 Share Posted August 8, 2008 <?php $number_field = $_POST['number_field']; $number_field = intval($number_field); ?> Quote Link to comment https://forums.phpfreaks.com/topic/118589-solved-replace-blank-field-with-a-zero/#findComment-611504 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.