Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    auto selecting a SELECT with PHP

    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:
    Code:
    <?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?
    Last edited by Bobafart; 07-12-2007 at 05:23 PM.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Code:
    <select>
    			<option>0%</option>
    			<option>1%</option>
    			<option selected="selected">2%</option>						
    			<option>3%</option>
    			<option>4%</option>
    			<option>5%</option>	
    	</select>

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    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>
    Last edited by mlseim; 07-12-2007 at 05:35 PM.

  • #4
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    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:

    Code:
    $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?

  • #5
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    You can write out the <option>'s in a loop comparing if the user tax rate equals the current loop value.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #6
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    PHP Code:
    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
    Last edited by StupidRalph; 07-12-2007 at 05:53 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #7
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or a slilghtly "cleaner" version

    Code:
    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.
    PHP / CSS / Perl / XML / XHTML

    For cheap ipod touch go to protella.com

  • #8
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry i noticed that the bit after : should read

    : $selected = '';
    PHP / CSS / Perl / XML / XHTML

    For cheap ipod touch go to protella.com

  • #9
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    I got it.

    thanks guys.. you rock!

  • #10
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by daveyand View Post
    Or a slilghtly "cleaner" version

    Code:
    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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •