Pregunta

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!

¿Fue útil?

Solución

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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top