11

I'm relatively new to php and I've been trying all day long to get this to work. I have a multiple array and want to echo each one in a specific format, and in groups. So i've gone through stackoverflow and found this help:

<? foreach(array_chunk($projetos, 6) as $curta ) { ?>


        <li style='display:block'>


                     <div class="film"> <?php echo $curta[0]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[1]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[2]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[3]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[4]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[5]['titulo']; ?></div>


             </li> 

      <? }; ?>

And this returns what I want but the last items of the array doesnt fill up to 6 and creates 2 extras empty divs and messes up the design.

This is a single example of the array i have:

<?php
 $projetos = array ( 

            "ugm" => array (
                    "id" => "ugm",
                    "titulo" => "Una Guerra Más",
                    "video" => "imagem",
                    "videoid" => "",
                    "height" => "$video_height_wide",
                    "sinopse" => "Um soldado moribundo deseja enviar sua última carta. Curta indisponível por exibição em festivais. Feito em parceria com a Universidad del Cine e LightBox Studios.",
                    "elenco" => "Ignacio J. Durruty - Rodrigo Soler - Ulisses Levanavicius - Aron Matschulat Aguiar",
                    "idioma" => "Inglês - Português",
                    "camera" => "Sony EX1",
                    "formato" => "HD",
                    "duracao" => "9'55''",
                    "ano" => "2012",
                    "tipo" => "Curta",
                    "credito" => "Direção - Edição - Produção - Roteiro",
                                 ), (...)

I want to be able to edit just one div that will be the master for the others... and using the implode I've read on another question but didn't worked to echo the strings I wanted..

Would please someone help out? thanks in advance!

Aron
  • 135
  • 1
  • 1
  • 5
  • sorry friends, but i cant understand your problem...it's my mistake..if you elaborate then i will help you as possible. – TheFoxLab Sep 16 '12 at 15:48

4 Answers4

28
<?php foreach(array_chunk($projetos, 6) as $curta ) { ?>
    <li style='display:block'>
       <?php foreach($curta as $detail) { ?>
                 <div class="film"> <?php echo $detail['titulo']; ?></div>
       <?php } ?>
    </li> 
<? }; ?>
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • thanks eugen! this worked perfectly! ;) knittl did the same thing and im using this code now u guys provided :) thanks a lot! – Aron Sep 16 '12 at 15:57
0

Why not use a loop to iterate over $curta?

<? foreach(array_chunk($projetos, 6) as $curta ) { ?>
  <li style='display:block'>
    <? foreach($curta as $c) { ?>
      <div class="film"><? echo $c['titulo']; ?></div>
    <? } ?>
  </li> 
<? }; ?>
knittl
  • 246,190
  • 53
  • 318
  • 364
0

but the last items of the array doesnt fill up to 6

As this is going to happen most of the time, you can't assume that every chunk has 6 elements, so you'll have to iterate over the chunk:

<? foreach(array_chunk($projetos, 6) as $curta ) { ?>

<li style='display:block'>

<? foreach($curta as $c) { ?>       
    <div class="film"> <?php echo $c['ugm']['titulo']; ?></div>
<? }; ?>

</li> 

<? }; ?>

This way you are sure to display no empty divs.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
-1

This lines:

<div class="film"> <?php echo $curta[0]['titulo']; ?></div>

should be like this:

<div class="film"> <?php echo $curta[0]['ugm']['titulo']; ?></div>

that should do what you want.

Nelson
  • 49,283
  • 8
  • 68
  • 81