Concat a for Loop - PHP

php concatenate in loop

Replace this

$add = "OR month = '$m' ";

with

$add .= "OR month = '$m' ";

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 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 concatenate an array with a 'foreach' loop?

If you need foreach loop then use next code:

$res = [];

foreach($a as $ind=>$val){
$res[$ind] = $val." ".$b[$ind]." ".$c[$ind];
}

Demo

concat a for loop - php

$stringd = "xxx";
for($i=1;$i<=$_POST['cc'];$i++)
{
$stringd .= $_POST[$i]." ";
}
$stringd .= "hello";

PHP: Concat a string in a foreach loop

Try this - you have to concatenate each line generated in the foreach loop.

$carrier = array (
'SAS' => array('alias' => 'sas', 'name' => 'SAS'),
'British Airways' => array('alias' => 'british_airways', 'name' => 'British Airways')
);

$body['{{body}}'] = 'Line one';

foreach ($carrier as $key=>$value) {
$body['{{body}}'] .= '<option value=' . $value['alias'] . '>' . $value['name'] . '</option>';
}

$body['{{body}}'] .= 'Line two';
$body['{{body}}'] .= 'Line three';
$body['{{body}}'] .= 'Line four';

print_r($body);

PHP: How do I concatenate a foreach inside a string?

It isn't completely clear what you "want to concatenate" within the PDF but you may do it directly within the document as it is being built. Included below is a portion of your $html variable with the inclusion of foreach loops based on your description of what you are trying to do.

$html = '</tr>
<tr>
<td style="border: 1px solid black; border-collapse: collapse;">';

foreach($arr as $key) {
$html .= $key;
}

$html .= '</td>
<td style="border: 1px solid black; border-collapse: collapse;">';
foreach($arr as $key) {
$html .= $key;
}
$html .= '</td>
<td style="border: 1px solid black; border-collapse: collapse;">';
foreach($arr as $key) {
$html .= $key;
}
$html .= '</td>';

Is there any way or method to concatenate while loop inside the string of HTML?

If you need to return html code, we can form that code first, put it into a variable and then return the variable.

Try this code

function staffSectionOutput($props) {
$args = array(
'post_type' => 'staff_section',
'category_name' => 'senior'
);

$loop = new WP_Query( $args );
// enable output buffer
ob_start();
?>
<section class="staff">

<div class="container">
<div class="row">
<?php while ( $loop->have_posts() ) {
$loop->the_post();
$employeeName = get_post_meta( get_the_ID() , 'staff-employee-name', false );
$employeJob = get_post_meta( get_the_ID() , 'staff-employee-job-title', false );
$employeDescription = get_post_meta( get_the_ID() , 'staff-employee-description', false );
$employeUrl = get_post_meta( get_the_ID() , 'staff-employee-url', false );
?>
<div class="col-12 col-sm-6 col-md-4">
<div data-description="<?php echo $employeUrl ?>">
<div class="card-head">
<img src="<?php echo $employeUrl ?>">
</div>
<div class="card-body">
<div class="text">
<h4><?php echo $employeeName ?></h4>
<span><?php echo $employeJob ?></span>
</div>
<div class="btn-a">Learn More</div>
</div>
</div>
</div>

<?php } ?>

<?php wp_reset_postdata(); ?>
</div>
</div>
</section>
<?php
// save everything in the buffer to the variable $content
$content = ob_get_contents();

// disable and clear the buffer
ob_end_clean();
return $content;
}

PHP- concatenate string with iterator to get original variable name

Use it this way.

Try this code snippet here

<?php
$encoded1=10;
$encoded2=20;
$encoded3=30;

for($i =1;$i<=3;$i++)
{
echo ${"encoded".$i};
}


Related Topics



Leave a reply



Submit