Bobafart
07-12-2007, 04:20 PM
this is more of a PHP/HTML question I suppose
I have a taxation rate for different users stored in mySQL.
Based on the user that is logged in I SELECT the tax rate for the user's session.
I have a <OPTION> <SELECT> drop down which has a list of taxation rates.. the values can only be 1, 2, 3, 4 and 5%.
How do I make it so that the taxation rate of the user is SELECTED by default in the drop down menu?
my code so far:
<?php
$sql="select countryTax from c_countryTreasury where userid=1";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){echo $row[countryTax].'%'; }
?>
<p>Select Tax Rate:</p>
<p><select>
<option>0%</option>
<option>1%</option>
<option>2%</option>
<option>3%</option>
<option>4%</option>
<option>5%</option>
</select></p>
So if the user's database profile has a tax rate of 2% then 2% will be the default value.. now what I mean?
PappaJohn
07-12-2007, 04:27 PM
<select>
<option>0%</option>
<option>1%</option>
<option selected="selected">2%</option>
<option>3%</option>
<option>4%</option>
<option>5%</option>
</select>
mlseim
07-12-2007, 04:29 PM
I think the first item listed is the one that shows up before the
user pulls-down the menu ... that would be the default one.
Oh ... I see now I messed it up. I was reading this wrong.
You'll have to figure out what the user's countryTax is by
fetching that ... which that variable would be the first one
in your <option> list.
<?php
$sql="select countryTax from c_countryTreasury where userid=1";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){echo $row[countryTax].'%'; }
?>
<p>Select Tax Rate:</p>
<p><select>
<option><?=$row[countryTax]?>%</option>
<option>0%</option>
<option>1%</option>
<option>2%</option>
<option>3%</option>
<option>4%</option>
<option>5%</option>
</select></p>
Bobafart
07-12-2007, 04:36 PM
I don't think I explained myself properly. sorry. this is difficult.
I am trying to make it so that the tax rate is AUTO SELECTED to the user's stored tax rate.
which means there has to be an IF condition.
something like:
$userTaxRate = $row[countryTax]; // get this from mySQL table of course...
<p>Select Tax Rate:</p>
<p><select>
<option <?php if($userTaxRate == 0){ echo 'selected'; }?>>0%</option>
<option <?php if($userTaxRate == 1){ echo 'selected'; }?>>1%</option>
<option <?php if($userTaxRate == 2){ echo 'selected'; }?>>2%</option>
<option <?php if($userTaxRate == 3){ echo 'selected'; }?>>3%</option>
<option <?php if($userTaxRate == 4){ echo 'selected'; }?>>4%</option>
<option <?php if($userTaxRate == 5){ echo 'selected'; }?>>5%</option>
</select></p>
I guess what I am asking is, is there a cleaner/better way of doing this?
StupidRalph
07-12-2007, 04:40 PM
You can write out the <option>'s in a loop comparing if the user tax rate equals the current loop value.
StupidRalph
07-12-2007, 04:50 PM
for ($i=0; $i < 6; $i++){
if ( $row[countryTax] == $i ) {
echo "<option selected=\"selected\">$i%</option>\n"; //user's tax rate
}
else {
echo "<option>$i%</option>\n"; //not the user's tax rate
}
}
Something like this
daveyand
07-12-2007, 05:03 PM
Or a slilghtly "cleaner" version
for ($i=0; $i < 6; $i++){
$row[countryTax] == $i ? $selected = 'selected=selected;' : '';
echo "<option $selected>$i%</option>\n";
}
The above should work ouit of the box.
Should be self explanitory as to what is happening. But willing to go into if you want.
daveyand
07-12-2007, 05:06 PM
sorry i noticed that the bit after : should read
: $selected = '';
Bobafart
07-12-2007, 05:07 PM
I got it.
thanks guys.. you rock!
StupidRalph
07-12-2007, 05:13 PM
Or a slilghtly "cleaner" version
for ($i=0; $i < 6; $i++){
$row[countryTax] == $i ? $selected = 'selected=selected;' : '';
echo "<option $selected>$i%</option>\n";
}
The above should work ouit of the box.
Should be self explanitory as to what is happening. But willing to go into if you want.
Why didn't I use the ternary operator? You win. :p