Jump to content

Form Select Box with Selected Entry based on DB query


bschultz

Recommended Posts

I have a page that allows a user to edit mysql data.  It displays the current info in a form text field...but I want to switch that to a select box...with the current info "selected" in the form.

 

I know that I can put "selected" in the form...but is there a better way than having almost 100 if statements (it's for the time...every 15 minutes would be the select options...so 24x4)?

 

    <option value="01:23:00" selected>TBA</option>
    <option value="00:00:00">12:00am</option>
    <option value="00:15:00">12:15am</option>
    <option value="00:30:00" <?php if ($row[time] = "00:30:00") { echo "selected='00:30:00' } ?>>12:30am</option>
    <option value="00:45:00">12:45am</option>

 

This would select 12:30am...but like I said, is there a way to do this WITHOUT an if statement for each value?

Link to comment
Share on other sites

Try this man...

 

<form>
<?php $temp = $row['time'];?>
     <select name="time" id="time">
        <option value="" selected="selected"></option>
        <option value="00:00:00" <?php selrec($temp,"00:00:00"); ?>>12:00am</option>
        <option value="00:15:00" <?php selrec($temp,"00:15:00"); ?>>12:15am</option>
        <option value="00:30:00" <?php selrec($temp,"00:30:00"); ?>>12:30am</option>
        etc..............
     </select>
</form>

<?php
function selrec($temp,$val)
{
if($temp==$val)
{
	echo "selected";
}
}
?>

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.