PHP div for every 12 images

可紊 提交于 2019-12-23 21:14:59

问题


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


回答1:


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>";
?>



回答2:


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.




回答3:


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>";
?>



回答4:


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




回答5:


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



回答6:


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.



来源:https://stackoverflow.com/questions/3653705/php-div-for-every-12-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!