PHP帮助显示前3个结果的结果不同,其余7个默认值看起来都相同

时间:2016-05-23 10:31:17

标签: php

我想要为第一个结果制作一些东西,它会显示一些独特的东西,第二个和第三个位置看起来会有所不同,然后最后的7个结果将只显示如下所述的相同格式,我将如何处理这种形式的代码呢?

<?php
$evenOdd = 'even';
$user_a = mysql_query("SELECT * FROM users WHERE rank < 4 ORDER BY credits             DESC LIMIT 10");
while ($users = mysql_fetch_assoc($user_a)) {
    $row = mysql_fetch_assoc($row = mysql_query("SELECT * FROM users WHERE id = '" . $users['id'] . "' LIMIT 10"));
    $online = $row['online'];
    if ($evenOdd == 'even') {
        $evenOdd = 'odd';
    } else {
        $evenOdd = 'even';
    }
    if ($online == 0) {
        $aanwezig = "<img   src='http://localhost/app/tpl/skins/site/images/icons/offline.gif'/>";
    } else {
        $aanwezig = "<img src='http://localhost/app/tpl/skins/site/images/icons/online.gif'/>";
    }
    echo '
    <a href="http://localhost/home/' . $row['username'] . '"><div class="head-' . $evenOdd . '" style="background-image: url(https://avatar-retro.com/habbo-imaging/avatarimage?figure=' . $row['look'] . '&headonly=1&head_direction=3&gesture=sml&size=s);"></div>
    <div class="username-' . $evenOdd . '">' . $row['username'] . '' . $aanwezig . '</a></div>
    <div class="stat-' . $evenOdd . '"><b><font size="2">' . number($row['credits']) . '</b></font> <font size="1">Credits</font></div>
    ';
}       
?>

我有另一个代码可以从朋友那里做类似的事情,但我无法弄清楚如何将它实现到我的代码中,因为他的编码与我自己的编码有点不同

public function topCredits() {
    $content = '';
    $users = MangoEngine::$object['users']->find_by_sql("SELECT * FROM users WHERE rank < 2 ORDER BY credits DESC LIMIT 5");
    $odd = '';
    $first = true;
    $num = 0;
    foreach($users as $user) {
        $num++;
        if($odd == "background-color:#fff;") {
            $odd = "background-color:#ECECEC;border-top:1px dashed #CCCCCC;border-bottom:1px dashed #CCCCCC;";
        } else {
            $odd = "background-color:#fff;";
        }
        $content .= "<table width=\"102%\" height=\"60px\" style=\"margin-left:-3px;{$odd}\">
            <tbody>
            <tr>
                <td valign=\"middle\" width=\"25\" height=\"50px\">";
                if($first) {
                    $content .= "<img style=\"margin-top:-100px;margin-bottom:-10px;\" src=\"{$user->getFigure("&amp;gesture=sml&amp;direction=2&amp;size=sml")} \" />";
                } else {
                    $content .= "<img style=\"margin-top:-10px;margin-bottom:-10px;\" src=\"{$user->getFigure("&amp;headonly=1&amp;gesture=sml&amp;direction=2&amp;size=sml")} \" />";
                }
                $content .= "</td>
                <td valign=\"top\">
                    <p style=\"margin-top:5px;\"><strong><a style=\"color:black;\" href=\"/home/{$user->username}\">{$user->username}</a></strong></p>
                    <p style=\"margin-top:-7px;\"><font color=\"grey\">" . MangoCore::shortNumber($user->credits) . " %creditsAmount%</font></p>";
                    if($num == 1) {
                        $content .= "<img style=\"float:right;margin-top:-35px;margin-right:10px;\" src=\"/Mango/skins/Default/images/stats/gold.png\" />";
                    } else if($num == 2) {
                        $content .= "<img style=\"float:right;margin-top:-35px;margin-right:10px;\" src=\"/Mango/skins/Default/images/stats/silver.png\" />";
                    } else if($num == 3) {
                        $content .= "<img style=\"float:right;margin-top:-35px;margin-right:10px;\" src=\"/Mango/skins/Default/images/stats/bronze.png\" />";
                    } else {

                    }
                $content .= "</td>
            </tr>
            </tbody>
        </table>";
        $first = false;
    }
    return $content;
}

2 个答案:

答案 0 :(得分:1)

您可以通过在循环中为行实现计数器来实现此目的,如第二个代码所示,$ num&#39;。 在你的情况下,你将有三个条件。

if($num==1) else
if($num ==2 || $num ==3)else
if($num >3) 

答案 1 :(得分:0)

想象一下,你将以下表放在php循环中

<table>
    <tr>
        <td>Some data</td>
        <td>Some more data</td>
    </tr>
</table>
<table>
    <tr>
        <td>Some data</td>
        <td>Some more data</td>
    </tr>
</table>
<table>
    <tr>
        <td>Some data</td>
        <td>Some more data</td>
    </tr>
</table>
<table>
    <tr>
        <td>Some data</td>
        <td>Some more data</td>
    </tr>
</table>
<table>
    <tr>
        <td>Some data</td>
        <td>Some more data</td>
    </tr>
</table>

您可以使用CSS来设置表的样式,避免将代码与服务器端混合在一起!

<style>
    /* default table styles */
    table {
        font-weight: bold;
        font-family: 'Open sans', arial, tahoma, sans-serif;
        font-size: 15px;
    }

    /* overridden styles for first three tables */
    table:nth-of-type(1),
    table:nth-of-type(2),
    table:nth-child(3) {
        color: red;
    }
</style>

这也很可能表格中有一个更好的选择器,我只是想不到一个:)