Reputation: 117
I have two div for example div1 and div2. Both div is in same loop but having different contents. I want to display all the elements of div1 and then all the elements of div2 in foreach loop. Right now it is showing div1 content and then div2 content. I want to display all the content of div1 first and then all the contents of div2. How to do this in php loop?
<?php foreach($array as $val){ ?>
<div id="div1">test</div>
<div id="div2">code</div>
<?php } ?>
if array is having three elements :-
//current output :-
test code test code test code
//Expected outpur :-
test test test code code code
Upvotes: 1
Views: 100
Reputation: 2059
Just do like this way..
<?php
$div1_content = '';
$div2_content = '';
foreach($array as $val){
$div1_content .= '<div id="div1">test</div>';
$div2_content .= '<div id="div2">test</div>';
}
echo $div1_content;
echo $div2_content;
?>
Upvotes: 0
Reputation: 46900
Run two loops, first one for div1 and second for div2, that way you will get this
I want to display all the content of div1 first and then all the contents of div2
Ok so you still want to run only 1 loop: You can take some help from variables
$div1=$div2="";
foreach($array as $val){
$div1.="<div id=\"div1\">$val</div>"; // store in a temporary variable
$div2.="<div id=\"div2\">$val</div>";
}
echo $div1; //print 1
echo $div2; //print 2
However in HTML you cant really have multiple elements with same id
Upvotes: 1