0

I have a data array which is separated by a comma in database.

enter image description here now, what i want to do is implode the strings from the column purpose.

enter image description here

and make that checkbox lists in my form echo checked; if the list is in the database.

hope you guys can help me out. Thank You!

curiosity
  • 834
  • 8
  • 20

2 Answers2

1

If you must do this, then:

$checkedPurposes = preg_split('/\s*,\s*/', $purposeColumnFromDatabase);
// then for each checkbox
if (in_array($purposeName, $checkedPurposes)) {
    // echo checked
}

The regex ensures that typos regarding the number of spaces around the commas don't matter.

As other users have said in the comments though, this is bad database design. If you can change the schema to move the purposes into their own table, you probably should.

Erayd
  • 168
  • 7
1

Here's an example (Not the proper way to design your code though) .

$purposesFull=array('a','b','c','d','e');
$purposesFromDB='a,b,d';
$purposesFromDBArray=explode(',',$purposesFromDB);
foreach($purposesFull as $item){
    $checked = in_array($item,$purposesFromDBArray) ? ' checked' : '';
    echo '<input type="checkbox" name="purs[]"'.$checked.'>'.$item;
    echo '<br/>';
}
Ashraf
  • 2,612
  • 1
  • 20
  • 35
  • Your code has the correct output. i will try it first, based on my database. – curiosity Jul 02 '18 at 05:28
  • Thank you very much. you really got the correct answer. You picture out what the kind of output i want to display. without showing my code. – curiosity Jul 02 '18 at 05:44