我有一个 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&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>';
}
它在一个函数中,基本上每一行都应该有另一个背景:)
您希望所有偶数值显示其他内容;这将使用模运算符来做到这一点:
$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
已经定义了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)