문제

I have a foreach loop which contains a series of data, but what I need is for that data to display in two seperate locations.

Basically this is because i have:

<div id="maindivofimage">
 <?php foreach($bannerdata as $banner): ?>
    <img src="<?php echo $banner->getImage();?> />
 <?php endforeach; ?>
</div>

<div id="maindivofcontent">
    <h1><?php $banner->getTitle();?> </h1>
    <p><?php $banner->getDescription();?> </p>
</div>

and I only want a single surrounding DIV for each section, so I can't put the section section within the foreach or it'll display it multiple times...

Any help greatly appreciated!

도움이 되었습니까?

해결책

I am sure there is no problem at all with running the same loop twice, but if you really want to, you can run it once and store the html into strings, then use the strings:

<?php 
$imgs='';
$text='';
foreach($bannerdata as $banner){

    $imgs .= '<img src="'.$banner->getImage().'" />';
    $text .= '<h1>'.$banner->getTitle().'</h1>';
    $text .= '<p>'.$banner->getDescription().'</p>';
}; ?>

<div id="maindivofimage">
     <?php echo $imgs;?>
</div>

<div id="maindivofcontent">
    <?php echo $text;?>
</div>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top