PHP div for every 12 images

2019-07-18 02:55发布

i have this piece of code:

<?
include( "http://api.flickr.com/services/feeds/photos_public.gne?id=22352410@N07&lang=en-us&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!!!

6条回答
做自己的国王
2楼-- · 2019-07-18 03:32

About the double div: Move the if statement, that checks whether there is a remainder down, below the statement that increments the counter, and it will not create the double div:

<?
include( "http://api.flickr.com/services/feeds/photos_public.gne?id=22352410@N07&lang=en-us&format=php" );
$i = 0;
echo "<div class='container'>";
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";
     $i++;
     if($i % 12 === 0)
          print "</div><div class='container'>";
}
print "</div>";
?>
查看更多
老娘就宠你
3楼-- · 2019-07-18 03:33

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.

查看更多
老娘就宠你
4楼-- · 2019-07-18 03:38

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.

查看更多
闹够了就滚
5楼-- · 2019-07-18 03:39

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

查看更多
对你真心纯属浪费
6楼-- · 2019-07-18 03:41

Should do it!

<?
include( "http://api.flickr.com/services/feeds/photos_public.gne?id=22352410@N07&lang=en-us&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>";
?>
查看更多
狗以群分
7楼-- · 2019-07-18 03:47

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
查看更多
登录 后发表回答