user1233852
user1233852

Reputation: 87

PHP Preselect check boxes from a String

I have a string $eventdays which holds information regarding which days are selected.

The format of the data is:

Monday = Mo 
Tuesday = Tu
Wedneday = We
Thursday = Th
Friday = Fr
Saturday = Sa
Sunday = Su

Mo,Tu,We,Th,Fr,Sa,Su

So if for example Tuesday and Friday were selected, the string would be:

Tu,Fr

If Monday, Wednesday, and Saturday were selected it would be:

Mo,We,Sa

Note: Any combination of days can be selected.

I was wondering how to get this information, and preselect checkboxes. The checkboxes are:

<input type="checkbox" name="days[]" value="Mo" />Monday<br />
<input type="checkbox" name="days[]" value="Tu" />Tuesday<br />
<input type="checkbox" name="days[]" value="We" />Wednesday<br />
<input type="checkbox" name="days[]" value="Th" />Thursday<br />
<input type="checkbox" name="days[]" value="Fr" />Friday<br />
<input type="checkbox" name="days[]" value="Sa" />Saturday<br />
<input type="checkbox" name="days[]" value="Su" />Sunday<br />

I know how to preselect a checkbox (checked = "yes"), but my question is how can I parse the string and then select the correct checkboxes from that information?

Upvotes: 0

Views: 2276

Answers (3)

Adam
Adam

Reputation: 36703

Assuming your input is a string with line-breaks... First process your data into an keyed array to make life easier... I've used regexes to make it more robust against formatting changes.

$eventdays="Monday = Mo
Tuesday = Tu
Wedneday = We
Thursday = Th
Friday = Fr
Saturday = Sa
Sunday = Su";

$lines = explode("\n", $eventdays);
$data = array();
foreach ($lines as $line) {
  if (preg_match("/(\w+)\s*=\s*(\w+)/", $line, $match)) {
    $data[] = array('value'=>$match[2], 'label' => $match[1]);
  }
}

Now just iterate over the structure printing out the keys / labels. Use the in_array function to check if the current one should be selected. Also I used checked="checked" which is the standards-compliant way of selecting checkboxes... See this question.

$selected_test="Mo,We,Sa";   
$select=explode(",", $selected_test);

foreach ($data as $datum) {
  $checked="";
  if (in_array($datum['value'], $select)) {
    $checked = " checked=\"checked\"";
  }
  echo <<< EOF
<input type="checkbox" name="days[]" value="{$datum['value']}"$checked/>{$datum['label']}<br />\n
EOF;
}

Output

<input type="checkbox" name="days[]" value="Mo" checked="checked"/>Monday<br />
<input type="checkbox" name="days[]" value="Tu"/>Tuesday<br />
<input type="checkbox" name="days[]" value="We" checked="checked"/>Wedneday<br />
<input type="checkbox" name="days[]" value="Th"/>Thursday<br />
<input type="checkbox" name="days[]" value="Fr"/>Friday<br />
<input type="checkbox" name="days[]" value="Sa" checked="checked"/>Saturday<br />
<input type="checkbox" name="days[]" value="Su"/>Sunday<br />

Upvotes: 2

Josh
Josh

Reputation: 8191

You can use strpos and dynamically generate your checkboxes.

$eventdays = "Tu,Fr"; // Selected days

$days = array( "Monday"   => "Mo",
               "Tuesday"  => "Tu",
               "Wedneday" => "We",
               "Thursday" => "Th",
               "Friday"   => "Fr",
               "Saturday" => "Sa",
               "Sunday"   => "Su"
            );

foreach ($days AS $day => $shortDay) {
    // Is there an event on this day?
    $checked = strpos($eventdays, $shortDay) !== FALSE ? "checked='checked'" : "";
    // Generate checkbox HTML
    echo "<input type='checkbox' name='days[]' value='{$shortDay}' {$checked} />{$day}<br />"; 
}

Output

<input type='checkbox' name='days[]' value='Mo' />Monday<br />
<input type='checkbox' name='days[]' value='Tu'  checked='checked'/>Tuesday<br />
<input type='checkbox' name='days[]' value='We' />Wedneday<br />
<input type='checkbox' name='days[]' value='Th' />Thursday<br />
<input type='checkbox' name='days[]' value='Fr'  checked='checked'/>Friday<br />
<input type='checkbox' name='days[]' value='Sa' />Saturday<br />
<input type='checkbox' name='days[]' value='Su' />Sunday<br />

Upvotes: 2

kuzditomi
kuzditomi

Reputation: 730

use explode(), it returns an array

$days  = "mon,tu";
$needstobeselected= explode(",", $days);

then you have an array with the days that has to be checked. you can then make a loop on the array and write your logic. i guess crappy method, but build a $checkeddays array, like:

[0] => 'checked', [1] => ' ',

and so on, then use it :

echo '<input type="checkbox" name="days[]" value="Mo" '.$checkeddays[0].'/>Monday<br />'

the main part is explode.

Upvotes: 0

Related Questions