While loop, statement for every other loop?

一曲冷凌霜 提交于 2019-12-31 04:56:08

问题


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>

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?

my code as requested:

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 :)


回答1:


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



回答2:


You want all even values to display something else; this will do that using the modulo operator:

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

If possible, you could change to a for loop since you'd have your $index already defined.




回答3:


Or better still

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

Then put the colors into the style sheet - it has the advantage you can even change the fonts etc. You can even give it a fresh look without needing to change the code




回答4:


You can do something like the following:

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

For neatness you could also turn your while loop into a for loop. For example:

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


来源:https://stackoverflow.com/questions/7359932/while-loop-statement-for-every-other-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!