Home  >  Article  >  Backend Development  >  Array loop: loop through multiple li, each li fixes N pieces of data, li_PHP tutorial

Array loop: loop through multiple li, each li fixes N pieces of data, li_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:00:54769browse

Array loop: loop multiple li, each li fixes N pieces of data, li

PHP code:

   <span>$arr</span> = <span>array</span>(1,2,3,4,5,6,7<span>); 
  </span><span>$x</span> = 1<span>;
    </span><span>$y</span> = 0<span>;
    </span><span>foreach</span>(<span>$arr</span> <span>as</span> <span>$k</span> => <span>$v</span><span>){
        </span><span>$data</span>[<span>$y</span>][] = <span>$v</span><span>;
     </span><span>//</span><span>4 就是没个li显示4条数据,如果显示3条,改成3即可。</span>
        <span>if</span>(<span>$x</span> == 4<span>){
            </span><span>$y</span>++<span>;
            </span><span>$x</span>=1<span>;
            </span><span>continue</span><span>;
        }
        </span><span>$x</span>++<span>;
    }

  </span><span>//</span><span>$data  是最终的结果集,发送到页面循环即可.</span>

HTML code (mine is a smarty template):

<span><</span><span>ul</span><span>></span><span>   
    {foreach from=$data item=vo}
        </span><span><</span><span>li</span><span>></span><span>
            {foreach from=$vo item=val}
                </span><span><</span><span>a </span><span>href</span><span>="#"</span><span>><</span><span>img </span><span>src</span><span>="{$val.xxxx}"</span><span>></</span><span>a</span><span>></span><span>
            {/foreach}
        </span><span></</span><span>li</span><span>></span><span>
    {/foreach}
</span><span></</span><span>ul</span><span>></span>

Result:

·1234
·567

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1091266.htmlTechArticleArray loop: loop multiple li, each li fixes N pieces of data, li PHP code: $arr = array ( 1,2,3,4,5,6,7 ); $x = 1 ; $y = 0 ; foreach ( $arr as $k = $v ){ $data [ $y ][] = $v ; / / 4 Just...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn