简体   繁体   English

与 CSS 并排显示 foreach 数组

[英]Display foreach array side-by-side with CSS

I am pretty sure that if I use something like this in css我很确定如果我在 css 中使用这样的东西

#list {
float:left;
}

I can format the display of my array to look like我可以格式化我的数组的显示看起来像

A B C 

rather than而不是

A
B
C

The problem is I am having problems formatting the php of the with the proper <div> 's to give me what I want.问题是我在用正确的<div>格式化 php 给我想要的东西时遇到问题。

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

Can someone help me modify the foreach above to achieve the different layout.有人可以帮我修改上面的 foreach 以实现不同的布局。 I also need to be able to format the Address differently then the Name我还需要能够以不同于Name的方式格式化Address

EDIT : For clarification...编辑:为了澄清......

A =一个=

Address地址
City城市

B =乙 =

Address地址
City城市

C = C =

Address地址
City城市

You need to wrap each address in a div and use a class to float it:您需要将每个地址包装在一个 div 中并使用 class 来浮动它:

PHP/HTML: 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: CSS:

.address-item {
     float: left;
}
$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 CSS

li {
    float: left;
}

something like:就像是:

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>';
}

wherein, the CSS will be:其中,CSS 将是:

.list {float:left;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM