While 循环,每个其他循环的语句? [英] While loop, statement for every other loop?

查看:22
本文介绍了While 循环,每个其他循环的语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个while循环,每个循环在列表中显示一个

  • ,有没有办法告诉php每个其他循环都应该回声说:

    I have a while loop and each one displays a <li></li> in a list, is there a way to tell php that every other loop should echo say:

    <li style="background: #222;"></li>
    

    并且在我的 css 中,我会将另一种颜色 #111 设置为默认值,以便我网站的用户可以轻松确定他们看到的是哪一行数据?

    and in my css I would have another colour #111 set as default so users of my site can easily determine which row of data they are seeing?

    我要求的代码:

    function user_details($dbc, $q, $details) {
    echo '<ul class="userlist">';
    while ($user = $q -> fetch(PDO::FETCH_ASSOC)) {
    
        echo '<li><span>(' . $user['id'] . ')</span> <a href="/viewaccount?id=' . $user['id'] . '">' . $user['username'] . '</a><div>';
    
        if ($user['admin'] > 0) {
            echo '<img class="Tooltip" src="gameimages/admin.png" width="18" height="18" title="Is game admin." alt="" />';
        }
    
        echo '<img class="Tooltip" src="gameimages/' . $user['gender'] . '.png" width="18" height="18" title="' . $user['gender'] . '" alt="" />';
    
        $last_active = time() - $user['last_active'];
        $seconds = $last_active % 60;
            $minutes = ($last_active - $seconds) / 60;
        if ($minutes <= 4) {
        echo '<img class="Tooltip" src="gameimages/active5.png" width="18" height="18" alt="" title="';
            if ($seconds == 0) {
                echo 'Last active ' . $minutes . ' minutes ago." />';
            }
            elseif ($minutes == 0) {
                echo 'Last active ' . $seconds . ' seconds ago." />';
            }
            else {
                echo 'Last active ' . $minutes . ' minutes and ' . $seconds . ' seconds ago." />';
            }
        }
        elseif ($minutes <= 9) {
            echo '<img class="Tooltip" src="gameimages/active10.png" width="18" height="18" alt="" title="';
            if ($seconds == 0) {
                echo '<strong>Last active ' . $minutes . ' minutes ago." />';
            }
            else {
                echo 'Last active ' . $minutes . ' minutes and ' . $seconds . ' seconds ago." />';
            }
        }
        elseif ($minutes <= 14) {
            echo '<img class="Tooltip" src="gameimages/active15.png" width="18" height="18" alt="" title="';
            if ($seconds == 0) {
                echo 'Last active ' . $minutes . ' minutes ago." />';
            }
            else {
                echo 'Last active ' . $minutes . ' minutes and ' . $seconds . ' seconds ago." />';
            }
        }
    
        if ($user['subscriber'] > 0) {
            echo '<img class="Tooltip" src="gameimages/subscriber.png" width="18" height="18" title="Subscriber with ' . $user['subscriber'] . ' days left." alt="" />';
        }
    
        echo '</div>';
    
        echo '<a href="#"><img class="Tooltip" src="gameimages/sendmessage.png" width="18" height="18" title="Send ' . $user['username'] . ' a message." alt="" /></a>';
    
        $is_friend = $dbc -> prepare("SELECT id, friend_id FROM friends WHERE id = ? && friend_id = ?");
        $is_friend -> execute(array($details['id'], $user['id']));
        $friend_request = $dbc -> prepare("SELECT id, id_to FROM friend_requests WHERE id = ? || id_to = ? && id = ? || id_to = ?");
        $friend_request -> execute(array($user['id'], $details['id'], $details['id'], $user['id']));
        if ($is_friend -> rowCount() > 0) {
            echo '<img class="Tooltip" src="gameimages/isfriend.png" width="18" height="18" title="Friend request sent to ' . $user['username'] . '." alt="" />';
        }
        elseif ($friend_request -> rowCount() > 0) {
            echo '<img class="Tooltip" src="gameimages/friendrequested.png" width="18" height="18" title="There is a request pending to be friends with ' . $user['username'] . '." alt="" />';
        }
        else {
            echo '<a href="/confirm?action=addfriend&amp;id=' . $user['id'] . '"><img class="Tooltip" src="gameimages/addfriend.png" width="18" height="18" title="Add ' . $user['username'] . ' as a friend." alt="" /></a></li>';
        }
    }
    echo '</ul>';
     }
    

    它在最小的函数中,基本上每一行应该有另一个背景:)

    It is in a function at the min, basically each other row should have another background :)

    推荐答案

    您可以使用布尔标志来跟踪它是奇数行还是偶数行,即

    You could use an boolean flag to keep track is it odd or even row, ie

    $isOdd = true;
    while(...){
      if($isOdd) echo '<li style="background: #222;"></li>';
      else echo '<li></li>';
      $isOdd = ! $isOdd;
    }
    

    这篇关于While 循环,每个其他循环的语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆