使用CSS并排显示foreach数组

时间:2011-07-19 20:49:32

标签: php css arrays

我很确定如果我在css中使用这样的东西

#list {
float:left;
}

我可以将数组的显示格式设置为

A B C 

而不是

A
B
C

问题是我在使用正确的<div>来格式化php的问题时给了我想要的东西。

foreach ( $model->address as $address ) {
    if ($address->Address) echo $address->Address.'<br />';
    if ($address->city->Name) echo $address->city->Name;
}

有人可以帮我修改上面的foreach来实现不同的布局。我还需要能够将Address格式化为Name

编辑:澄清......

A =

地址
城市

B =

地址
    城市

C =

地址
    城市

3 个答案:

答案 0 :(得分:3)

您需要将每个地址包装在div中并使用类来浮动它:

PHP / HTML:

foreach ( $model->address as $address ) {
    echo '<div class="address-item">';
    if ($address->Address) echo $address->Address.'<br />';
    if ($address->city->Name) echo $address->city->Name;
    echo '</div>';
}

CSS:

.address-item {
     float: left;
}

答案 1 :(得分:0)

$out = "<ul>";
foreach($model->address as $address){
    if(!isset($address->Address) && !isset($address->city->Name)) continue;
    $out .= "<li>" . isset($address->Address) ? $address->Address : "" . " "
        . isset($address->city->Name) ? $address->city->Name : "" . "</li>";
}
$out .= "</ul>";
print $out;

CSS

li {
    float: left;
}

答案 2 :(得分:0)

类似的东西:

foreach ( $model->address as $address ) {
   echo '<div class="list">';
   if ($address->Address) echo $address->Address.'<br />';
   if ($address->city->Name) echo $address->city->Name;
   echo '</div>';
}

其中,CSS将是:

.list {float:left;}