0

How to concat the foreach loop with string in PHP like this

$traning [] = '<tr>
    <td style="width:30%" id="trainingCode_' . $key + 1 . '">
    <select class="select2 form-control" data-live-search= "true" name="trainingCode[]">
        <option value="0">SELECT CODE</option>'.
        foreach ($code=0 as $row) {
            //
        }
    .'</select>
    </td>
   </tr>';
0

5 Answers 5

6

Split code into parts that are clear to everyone:

$trContent = '<tr>
    <td style="width:30%" id="trainingCode_' . $key + 1 . '">
    <select class="select2 form-control" data-live-search= "true" name="trainingCode[]">
        <option value="0">SELECT CODE</option>';

// iterate over array and append to `$trContent`
foreach ($code as $row) {
    $trContent .= '<option></option>';
}

$trContent .= '</select>
    </td>
   </tr>';

$traning [] = $trContent;
Sign up to request clarification or add additional context in comments.

1 Comment

bro, please modify this line '<option></option>' i have also added my answer with few suggestions.
3

You have few issues in your code:

Issue 1: you can't concat function as like a variable, you just need to break your code and store in a variable then use Concat, otherwise, this will give you parse error.

Issue 2: Dont know why are you using $code=0 as $row in your foreach, this will give you Invalid argument error.

Issue 3: This is just my assumption, according to this id="trainingCode_' . $key + 1 . '" you are using this code inside a loop, if yes then this part name="trainingCode[]" also will create an issue when you fetch record, how would you know the correct relation?

Your Code Example:

<?php
$myString = '<tr>
    <td style="width:30%" id="trainingCode_' . $key + 1 . '">
    <select class="select2 form-control" data-live-search= "true" name="trainingCode[]">
        <option value="0">SELECT CODE</option>';

// use your foreach loop here
foreach ($code as $row) {
    $myString .= '<option value="'.$row.'">'.$row.'</option>'; // contact wit  $myString
}

$myString .= '</select></td></tr>';
$traning[] = $myString;
?>

1 Comment

There is absolutely no value (ever) to duplicate the option's text as a value attribute value. Just use ...<option>' . implode('</option><option>', $code) . '</option>...
1

Foreach loop inside string not the best way. But string in string.

$options = "";
foreach ($rows as $code) {
  $options .= '<option value="'.$code '">' . $code . '</option>';
}
$traning [] = '<tr>
    <td style="width:30%" id="trainingCode_' . $key + 1 . '">
    <select class="select2 form-control" data-live-search= "true" name="trainingCode[]">
        <option value="0">SELECT CODE</option>'. $options .
    '</select>
    </td>
   </tr>';

1 Comment

There is absolutely no value (ever) to duplicate the option's text as a value attribute value. Just use ...<option>' . implode('</option><option>', $code) . '</option>...
1

You can do something like:

$options = "";
foreach ($code=0 as $row) {
    $options .= "<option value='$code'>Code</option>";
}

$traning [] = '<tr>
<td style="width:30%" id="trainingCode_' . $key + 1 . '">
<select class="select2 form-control" data-live-search= "true" name="trainingCode[]">
    <option value="0">SELECT CODE</option>'.
    $options
.'</select>
</td>
</tr>';

1 Comment

The hardcoded text Code will make it hard for users to know exactly which value they are selecting because all options will have the same visible text.
1
$htmlToConcat = "";
 foreach ($code=0 as $row) {
        $htmlToConcat .= "";
    }


$traning [] = '<tr>
    <td style="width:30%" id="trainingCode_' . $key + 1 . '">
    <select class="select2 form-control" data-live-search= "true" name="trainingCode[]">
    <option value="0">SELECT CODE</option>'.$htmlToConcat.'</select>
</td>
</tr>';

1 Comment

This code-only post does not produce any additional <option> tags. Low-quality answer, rushed answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.