开发者

How to do a Horizontal looping in PHP

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.


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>


I built this a little while ago for a client, I think it's exactly what you need: Column-focused Tables

Basically, you modify the addItem() function to properly fit the data you want to display (links, images, etc.) and then just call addItem() for each item to display. Then outputItemTable() will output a table with the data grouped together properly.


    <?php  
        echo '<div style="width:100%;">';
        foreach ($response->items->item as $value) {
            echo('<div style="display:inline-block;">
<img src="'.$value->imageUrl.'" width=200><br>');
            echo($value->description."<br>");
            echo($value->url."</div>");
        };
        echo '</div>';

    ?>

i hope this helps


<?php
$array = array();
$array[] = array('name'=>'Name 1', 'id'=>'w', 'spec'=>'rrr');
$array[] = array('name'=>'Name 2', 'id'=>'x', 'spec'=>'sss');
$array[] = array('name'=>'Name 3', 'id'=>'y', 'spec'=>'ttt');
$array[] = array('name'=>'Name 4', 'id'=>'z', 'spec'=>'uuu');

  function horizontal_table($array, $key){
    $count = count($array);
    $return = "";
    for($i=0; $i<$count; $i++){
      $return .= "<td>";
      if(isset($array[$i][$key])){
        if(!empty($array[$i][$key])){
          $return .= $array[$i][$key];
        } else {
          $return .= "&nbsp;";
        }
      } else {
        $return .= "&nbsp;";
      }
      $return .= "</td>";
    }
    return $return;     
  }
?>

<table border="1px solid #000">
  <tr>
    <td>&nbsp</td>
    <?php echo horizontal_table($array, 'name'); ?>
  </tr>
  <tr>
    <td>Item Id</td>
    <?php echo horizontal_table($array, 'id'); ?>
  </tr>
  <tr>
    <td>Item Spec</td>
    <?php echo horizontal_table($array, 'spec'); ?>
  </tr>
</table>

This is how i do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜