0

我使用创建了三列和三行li;我想li按 id 在每个订单中显示一个成员。

<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。

4

3 回答 3

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>';
?>
于 2013-10-13T18:51:52.987 回答
0

在 for each 循环中添加一个计数器并测试该计数器是否可被 2 整除...如果它是偶数。如果是偶数,则添加 class="middle"。

于 2013-10-13T18:54:41.160 回答
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>';

?>

于 2013-10-13T18:55:11.707 回答