0
$timetable = array();
for($i = 0; $i <= 23; $i++) {
    $timetable[$i.':00'] = $i.':00';
    $timetable[$i.':15'] = $i.':15';
    $timetable[$i.':30'] = $i.':30';
    $timetable[$i.':45'] = $i.':45';
}

$fields["ready"] = array("label" => "Opbouwklaar", "type" => "select", "options" => $timetable);

How can you set a default value to the following array? I have already tried set a "blank" or "default" in the array but this didn't work unfortunately.

Now the default value is standard 0:00, this is because that is the first option in the for statement.

3
  • What do you mean by default value of the array ? I think you are displaying those hours in a select and it's in this select that you wan't to display a default value, right ? Commented Jun 14, 2016 at 9:25
  • if default value is not dynamic, you can directly declare before $timetable loop and include it in $fields['ready'] Commented Jun 14, 2016 at 9:27
  • What about add to $fields["ready"] array another index called "selected" and value to check and mark as selected ?? Commented Jun 14, 2016 at 9:31

2 Answers 2

1

You can not set a default value for an array in a selectbox. If there is no 'selected' value then the 1st selection will show.

Sign up to request clarification or add additional context in comments.

Comments

0

You're not really clear. So I made some assumptions.

I guess you want a specific value in the select-element being selected when the page is loaded?

With this example down below you set the 'selected' (default?) value in the if() statement. In this example, value '10:15' is selected by default.

<?php

$timetable = array();
for($i = 0; $i <= 23; $i++) {
    $timetable[$i.':00'] = $i.':00';
    $timetable[$i.':15'] = $i.':15';
    $timetable[$i.':30'] = $i.':30';
    $timetable[$i.':45'] = $i.':45';
}

$fields["ready"] = array("label" => "Opbouwklaar", "type" => "select", "options" => $timetable);

?>

<select>
    <?php foreach($fields['ready']['options'] as $option){
        if ($option = "10:15"){ $selected = "selected";}
        echo "<option $selected value='$option'>$option</option>";
    }?>
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.