selected - PHP Preselect check boxes from a String -


i have string $eventdays holds information regarding days selected.

the format of data is:

monday = mo  tuesday = tu wedneday = thursday = th friday = fr saturday = sa sunday = su 

mo,tu,we,th,fr,sa,su

so if example tuesday , friday selected, string be:

tu,fr 

if monday, wednesday, , saturday selected be:

mo,we,sa 

note: combination of days can selected.

i wondering how information, , preselect checkboxes. 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 preselect checkbox (checked = "yes"), question how can parse string , select correct checkboxes information?

you can use strpos , dynamically generate checkboxes.

$eventdays = "tu,fr"; // selected days  $days = array( "monday"   => "mo",                "tuesday"  => "tu",                "wedneday" => "we",                "thursday" => "th",                "friday"   => "fr",                "saturday" => "sa",                "sunday"   => "su"             );  foreach ($days $day => $shortday) {     // there event on 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 /> 

Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -