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?

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.