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!

1
  • sorry friends, but i cant understand your problem...it's my mistake..if you elaborate then i will help you as possible. Commented Sep 16, 2012 at 15:48

4 Answers 4

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> 
<? }; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks eugen! this worked perfectly! ;) knittl did the same thing and im using this code now u guys provided :) thanks a lot!
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> 
<? }; ?>

Comments

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.

Comments

-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.