Jump to content

[SOLVED] Replace blank field with a zero


rjsws

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ;
}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 ;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ;

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.