How to concat foreach loop with string in php

php concatenate string and variable
concatenate string in php foreach
concatenate html with php variable
php concatenate constant and string
how to concatenate two numbers in php
php foreach build string
concat of string in php
if condition concatenate in php

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>';

Can anyone help me?

Thanks in advance

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;

How to concat foreach loop with string in php, Split code into parts that are clear to everyone: $trContent = '<tr> <td style="width:​30%" id="trainingCode_' . $key + 1 . '"> <select class="select2  The foreach loop - Loops through a block of code for each element in an array. The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

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;
?>

PHP string concatenation - PHP, I have set $message variable. Now I wana concatenate foreach loop's results to existing $message <img style="vertical-align: text-bottom"  The ‘foreach’ loop in PHP helps in accessing key value pairs within an array. The ‘foreach’ loop works with arrays only, with the advantage that a loop counter wouldn’t need to be initialized. In addition to this, no condition needs to be set that would be needed to exit out of the loop. The ‘foreach’ loop implicitly does this too.

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>';

PHP: String Operators - Manual, String Operators ¶. There are two string operators. which returns the concatenation of its right and left arguments. Concatenation inside of a for/​foreach loop. PHP Concatenation Operators - The PHP concatenation operator is one of the important operators in PHP, which is used to combine two string values to create one string, and for concatenation assignment.

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>';

Concatenation in PHP Foreach Loop, Concatenation in PHP Foreach Loop for the variables so that they are not literally echoed out as part of the string inside the quotations. If. String Operators. There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.

$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>';

PHP: Implode or Trim – Concatenating strings in a loop – James' Desk, People when trying to concatenate some strings with another string or a character, they simply concatenate with . inside the loop like PHP: Implode or Trim – Concatenating strings in a loop foreach ( $values as $value ) {. If you use Java 8 with Eclipse Collections, you can use the CharAdapter class forEach method with a lambda or method reference to iterate over all of the characters in a String. Strings.asChars("xyz").forEach(c -> System.out.print(c)); This particular example could also use a method reference. Strings.asChars("xyz").forEach(System.out::print)

Re: How to combine a string to get a foreach & run alltogether in a loop, How to combine a string to get a foreach & run alltogether in a loop How to combine / concatenate a *divided* string in order to use this combined Apart from wishing you success in programming in PHP, could you next  The first form loops over the array are given by array_variable. On each iteration, the value of the current element is assigned to a variable, and the internal array pointer is advanced by one (so in the next iteration, you'll be looking at the next element).

The List of the 10 Most Common Mistakes That PHP Developers , Buggy PHP Code: The 10 Most Common Mistakes PHP Developers Make Common Mistake #1: Leaving dangling array references after foreach loops Code that fails to properly handle non-ASCII strings is notorious for introducing In tips #4 and #5, using query() with string concatenation is a textbook example of an  case concatenating strings in a loop). Using a simple projection with Linq also helps here. It is optimized by the framework for performance: In most cases string.Join() will use a StringBuilder internally, so you are not creating multiple strings that are then un-referenced and must be garbage collected. Also see: Do not concatenate strings

PHP: foreach loop assign the values of an array to a concatenated , PHP: foreach loop assign the values of an array to a concatenated string and variable. php merge array values with same keys php merge multidimensional  How to execute two different php prepared statements inside a foreach loop? I would like to get the value of a column, add a string to it, then update the column with the new value, and repeat this for every element of an array.

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