如何做一个水平循环的PHP [英] How to do a Horizontal looping in PHP

查看:67
本文介绍了如何做一个水平循环的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的当前循环产生如下结果:

 <?php 
foreach($ response-> ; items-> item as $ value){
echo(< img src ='。$ value-> imageUrl。'width = 200>< br>)
echo($ value-> description。&br>);
echo($ value-> url。< br>);
};

?>

//结果

 < 1st row> 
(image)
description
(url)
< / end第一行>

< 2nd row>
(image)
description
(url)
< / end第二行>

< 3rd row>
(image)
description
(url)
< / end 3rd row>

我需要完成的是循环结果,所以每个项目从左到右是水平的。例如

 < 1st col> < 2nd col> < 3rd col> 
(image)(image)(image)
描述描述
(url)(url)(url)
< / end 1st col> < / end 2nd col> < / end 3rd col>

条件应为:

    $
  1. 如果有更多项目,则每个项目将显示在下一行,如下所示:


  2. 一个项目包括:

     (image)
    description

    有多少项?



    它可以达到20或更多。


    结束问题


    大家,结束的问题。希望得到这方面的帮助,那是有效的。提前致谢。

    解决方案

    您可以做很多事情来完成它。最好的方式(我认为..)是创建一个UL并打破每个第三个元素:

     < ul& 
    <?php
    $ i = 0;
    foreach($ response-> items-> item as $ value):?>
    < li<?php if($ i%3 == 0)echo'class =break'?
    <?php echo< img src ='。$ value-> imageUrl。'width = 200>< br>; ?>
    <?php echo $ value-> description?>
    < / li>
    <?php $ i ++; //增量计数器
    endforeach?>
    < / ul>

    然后你需要指定这个列表必须是水平的,元素:

     < style> 
    ul li {
    float:left;
    }
    ul li.break {
    clear:right;
    }
    < / style>


    The current loop below yields result as this:

    <?php  
        foreach ($response->items->item as $value) {
            echo("<img src='".$value->imageUrl."' width=200><br>");
            echo($value->description."<br>");
            echo($value->url."<br>");
        };
    
    ?>
    

    //Result

    <1st row>
    (image)
    description
    (url)
    </end 1st row>
    
    <2nd row>
    (image)
    description
    (url)
    </end 2nd row>
    
    <3rd row>
    (image)
    description
    (url)
    </end 3rd row>
    

    What I need to accomplish is Looping the result so each item is horizontal from left-to-right. e.g.

     <1st col>                 <2nd col>              <3rd col>
     (image)                   (image)                (image)
     description               description            description
     (url)                     (url)                  (url)
     </end 1st col>            </end 2nd col>         </end 3rd col>
    

    The conditions should be:

    1. each row as shown above, can only have 3 columns, as shown right above.
    2. when there are more items, each item will display on the next row just like how it displays on first row.

    What is an item?

    One Item includes:

     (image)
     description
     (url)
    

    How many items?

    It can be up to 20 or more.

    end of question

    Ok, everyone, end of the question. Hope to get help with this, that is effective. Thanks in advance.

    解决方案

    You can do a lot of things to accomplish it. The best way (I think..) is creating a UL and breaking at every third element:

    <ul>
    <?php
    $i = 0;
    foreach ($response->items->item as $value) : ?>
    <li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
        <?php echo "<img src='".$value->imageUrl."' width=200><br>"; ?>
        <?php echo $value->description ?>
    </li>
    <?php $i++; // Increment counter
    endforeach ?>
    </ul>
    

    Then you need to specify that this list must be horizontal and specify to break the line in the .break elements:

    <style>
    ul li {
        float:left;
    }
    ul li.break {
        clear: right;
    }
    </style>
    

    这篇关于如何做一个水平循环的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆