我使用li
创建了三列和三行;我希望按ID显示每个li
订单中的一个成员。
<li> // Style: margin-bottom:10px;
Show first member result here...
</li>
<li class="middle_li"> // Style: margin:0px 10px 10px;
Show second member result here...
</li>
<li> // Style: margin-bottom:10px;
Show third member result here...
</li>
<li> // Style: margin-bottom:10px;
Show first member result here...
</li>
<li class="middle_li"> // Style: margin:0px 10px 10px;
Show second member result here...
</li>
<li> // Style: margin-bottom:10px;
Show third member result here...
</li>
<li> // Style: margin-bottom:10px;
Show first member result here...
</li>
<li class="middle_li"> // Style: margin:0px 10px 10px;
Show second member result here...
</li>
<li> // Style: margin-bottom:10px;
Show third member result here...
</li>
我的PHP代码:
<?php
require 'initialize.inc.php'; // Classes: connection & members
echo '<ul>';
$members = $member->display_members();
foreach ($members as $member)
{
$html_ouput = '<li>';
$html_ouput .= 'Show member result here...';
$html_ouput .= '</li>';
echo $html_output;
}
echo '</ul>';
?>
这个php代码显示九个结果。三列三行,但我怎样才能给每一秒li
行一个类?我的PHP代码中的任何修改?我不想使用javascript。
答案 0 :(得分:1)
您需要将字符串连接到输出
试试这个:
<?php
require 'initialize.inc.php'; // Classes: connection & members
echo '<ul>';
$members = $member->display_members();
$i = 0;
foreach ($members as $member)
{
$html_ouput = '';
$i++;
if($i == 1)
$html_ouput .= '<li>';
else{
$i = 0;
$html_ouput .= '<li class="middle">';
}
$html_ouput .= 'Show member result here...';
$html_ouput .= '</li>';
echo $html_output;
}
echo '</ul>';
?>
答案 1 :(得分:0)
在每个循环中添加一个计数器并测试该计数器是否可以被2整除...如果它是一个偶数。如果是偶数,则添加class =“middle”。
答案 2 :(得分:0)
这个怎么样:
<?php
require 'initialize.inc.php'; // Classes: connection & members
echo '<ul>';
$members = $member->display_members();
$i = 0;
foreach ($members as $member)
{
$html_ouput = ($i % 3 == 1 ? '<li class="middle">' : '<li>');
$html_ouput .= 'Show member result here...';
$html_ouput .= '</li>';
echo $html_output;
$i++;
}
echo '</ul>';
&GT;