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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to show php variable in Select Box

    Hi!
    I have an html comboBox for batchyear on a php page.
    The batchyear is read from MySql database and saved as php variable
    $batchyear . Now i want to set the selected value of this <Select> comboBox as $batchyear.
    How to do this?

    Thanks in advance!

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    Quite simple, just echo the values out and construct a select tag and its options.

    Say you have constructed an array of the data you have pulled from the database. than you can do the following


    PHP Code:
    <?php
    for( $i 0$i count$array_of_data); $i++ )
    {
        echo 
    '<option value=" ' $array_of_data$i ] . ' "> ' $array_of_data$i ] . '</option>';
    # ends for loop
    ?>
    don't forget to start the process of building your select tag before the for loop and close it once the for loop is complete.

    good luck


  •  

    Posting Permissions

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