【问题标题】:While loop, statement for every other loop?While循环,每个其他循环的声明?
【发布时间】:2011-09-09 09:57:34
【问题描述】:

我有一个while循环,每个循环都在列表中显示一个<li></li>,有没有办法告诉php每个其他循环都应该回显:

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

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

我的代码按要求:

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

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

【问题讨论】:

  • 我已经读了两遍了——到底是什么问题?
  • 你能告诉我们你的while循环吗?

标签: php while-loop


【解决方案1】:

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

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

【讨论】:

  • 这非常有效,非常感谢:)你能否解释一下你对这里实际发生的事情的回答,这样如果你有时间我可以学习这项技术,谢谢:)
  • 嗯,这真的很简单 - $isOdd 是一个布尔变量,初始化为 true 并在循环结束时在 truefalse 之间切换它的值(@987654326 @ 语句,它读取$isOdd = not $isOdd 即它在真假之间循环)。
【解决方案2】:

您希望所有偶数值都显示其他内容;这将使用模运算符:

$index = 0;
while($user = $q->fetch(PDO::FETCH_ASSOC))
{
   if(++$index%2 == 0) //is index +1 even?
     echo '<li style="background: #222;"></li>';
   else
     echo '<li></li>';
}

如果可能,您可以更改为 for 循环,因为您已经定义了 $index

【讨论】:

    【解决方案3】:

    或者更好

    $isOdd = true;
    while(...){
      echo '<li class="' . ($isodd ? 'odd' : 'even'). '"></li>
      $isOdd = ! $isOdd;
    }
    

    然后将颜色放入样式表 - 它的优点是您甚至可以更改字体等。您甚至可以在无需更改代码的情况下赋予它全新的外观

    【讨论】:

    • 非常好 :) 没想到这个 ;)
    • 如果你很聪明——口袋里有一个 CSS——你可以在 6 ths 中获得提升
    【解决方案4】:

    您可以执行以下操作:

    $colors = array('111', '222');
    $i = 0;
    while (...) {
        echo '<li style="background: #' . $colors[$i % count($colors)] . '"></li>';
        $i++;
    }
    

    为了简洁起见,您还可以将 while 循环转换为 for 循环。例如:

    $colors = array('111', '222');
    for ($i = 0; ...; $i++) {
        echo '<li style="background: #' . $colors[$i % count($colors)] . '"></li>';
    }
    

    【讨论】:

    • 为什么要在每个循环中计算数组的内容? PHP 不是线程化的,因此在循环执行期间您的数组不会更新,直到您从循环中添加数据。如果你的数组很大,这会很痛......
    猜你喜欢
    • 2014-08-21
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2011-12-23
    • 1970-01-01
    • 2011-08-09
    相关资源
    最近更新 更多