Text before variables inside foreach loop

I installed a small script to process the page where I have three checkboxes.

The HTML for the checkboxes is as follows:

<input type="checkbox" value="Ex1" name="Check[]" />Ex1
<input type="checkbox" value="Ex2" name="Check[]" />Ex2
<input type="checkbox" value="Ex3" name="Check[]" />Ex3

And a PHP script for them:

if($_POST['Check'] == true){
foreach($_POST['Check'] as $value){
$check_msg .= "$value";
}
}

Now, what I want to do is insert "Ex1, Ex2" into my database if the "Ex1" and "Ex2" checkboxes are checked. But the problem is that if I put "," before "$ value", it will insert ", Ex1, Ex2" into the database. But, as I said, I want to insert it without a comma and space at the beginning ... How could I do something like this? It doesn't matter if it is a foreach loop or another method, because I really don't know another method to check which checkboxes were checked.

I tried several combinations, but could not get the desired results ...

Thank.

+3
2

:

foreach ( $_POST['Check'] as $key => $value ) {
  $check_msg .= ", {$value}";
}

$check_msg = substr($check_msg,2);

"," , . [IMHO], $check_msg , .

0

foreach implode. , $_POST['Check'] .

:

$csv = implode(', ', $_POST['Check']);

, ​​ , SQL.

+4

Source: https://habr.com/ru/post/1752966/


All Articles