views:

52

answers:

5

Hi again guys, i have this piece of code:

<?
include( "http://api.flickr.com/services/feeds/photos_public.gne?id=22352410@N07&amp;lang=en-us&amp;format=php" );
$i = 0;
    foreach($feed['items'] as $item) {
    preg_match("/<img src=\"([^\"]+)\" .*? \/>/", $item['description'], $matches);
    $img_html = $matches[0];
    $img_src = $matches[1];
    $medium_url = ereg_replace("_m", "_s", $img_src);
        echo "$img_html";
    }

?>

How I could make that each N number of pictures appear inside of a <div></div> Example:

<div class="container">
<img src="1.jpg" /> 
<img src="2.jpg" /> 
<img src="3.jpg" /> 
<img src="4.jpg" /> 
<img src="5.jpg" /> 
<img src="6.jpg" /> 
<img src="7.jpg" /> 
<img src="8.jpg" /> 
<img src="9.jpg" /> 
<img src="10.jpg" /> 
<img src="11.jpg" /> 
<img src="12.jpg" /> 
</div>
<div class="container">
<img src="13.jpg" /> 
<img src="14.jpg" /> 
<img src="15.jpg" /> 
<img src="16.jpg" /> 
<img src="17.jpg" /> 
<img src="18.jpg" /> 
<img src="19.jpg" /> 
<img src="20.jpg" /> 
<img src="21.jpg" /> 
<img src="22.jpg" /> 
<img src="23.jpg" /> 
<img src="24.jpg" /> 
</div>

etc...

Any Ideas??

Thanks so much!!!

+2  A: 

You can check with %12. In your loop you add a counter $j, at each loop you increment $j.

When $j % 12 === 0 then you have to change your div by adding </div><div class="container">.

Don't forget about the first and last <div> markups.

Colin Hebert
A: 

You have an $i=0 there, but you're not using it. There's no 12-line pagination algorithm either. How about trying some $i++ somewhere in your loop and thinking about where to insert an if ($i==12) or if ($i%12) somewhere. Sorry, I don't intend to spoon-feed. This is a basic problem you'll find in textbooks.

stillstanding
A: 

Instead of doing foreach, use a simple for loop with counter, and then use MOD on the counter to determine if the current iteration is divisible by N.

MOD: http://php.net/manual/en/internals2.opcodes.mod.php

For Loop: http://www.tizag.com/phpT/forloop.php

Brad
A: 

Adding to what others have said

  • You should always check the return value of preg_match before you use $match.
  • ereg_replace is deprecated. Use preg_replace instead. Since you are just doing a text replacement, you should be using str_replace
codaddict
A: 

Should do it!

<?
include( "http://api.flickr.com/services/feeds/photos_public.gne?id=22352410@N07&amp;lang=en-us&amp;format=php" );
    $i = 0;
    echo "<div class='container'>";
    foreach($feed['items'] as $item) 
    {
         if($i % 12 === 0)
              print "</div><div class='container'>";
         preg_match("/<img src=\"([^\"]+)\" .*? \/>/", $item['description'], $matches);
         $img_html = $matches[0];
         $img_src = $matches[1];
         $medium_url = ereg_replace("_m", "_s", $img_src);
         echo "$img_html";
         $i++;
    }
    print "</div>";
?>
Jim Grant
No, it puts a double div :-) <div class='container'></div><div class='container'><img src=...
Alberto
sberry2A
Now i Had only 1 div (and more of 12 images inside :-))
Alberto
if ($i == 12) did it, thanks to all!
Alberto