Forum Moderators: coopster

Message Too Old, No Replies

putting implode string back into an array

         

togethercomms

10:53 am on Feb 9, 2010 (gmt 0)

10+ Year Member



Hi All,

I have imploded data into a string with "," in-between them.

What i need to do is put that string back into an array with checkboxes and the ones in the database are pre checked.

Inserted into the database like;
"option1,option2,option3"

They need to come out as;

"option1 (checked)"
"option2 (checked)"
"option3 (checked)"
"option4"
"option5"
etc

Hope this makes sense.

Many Thanks

CyBerAliEn

4:05 pm on Feb 9, 2010 (gmt 0)

10+ Year Member



You can turn your string back into an array using PHP's explode() function as follows:

<?php
//Below takes care of turning string into array.
$str = "option1,option2,option3";
$optionsArray = explode(',',$str);
//Turn array to HTML output
$numOptions = (5);
for ($i=0;$i<$numOptions;$i++)
{
if (array_search("option".($i+1),$optionsArray)===false) { $checked = ''; }
else { $checked = ' checked'; }
echo "<input type=\"checkbox\" name=\"options\"{$checked}>";
}
?>


The array would contain 'option1', 'option2', 'option3' all as separate entries within the array. You could then use that info to "output" your check boxes as HTML by adding additional code (as done above). Note however that your string only contains info that '3' items are present, but your example indicates up to '5' items (maybe more?). You'll need to know or set somewhere how many options there to make it fully dynamic---this is done above via the variable 'numOptions'. The above code should give you an idea of how to achieve what you want/need.