php - 样式化 "order by desk"查询表

标签 php css sql

我已经制作了一个脚本,它通过 desc 0,5 sql 查询根据大多数积分为我提供前 5 名玩家 这是在一个表中,但它并没有真正按照我的意愿去做,它所做的是它为表标题提供的每一行,而不是将其作为 1 个表(see this screenshot)

这是我的 php 和 css:

<?php
    include('connect.php');
    $result = $db->prepare("SELECT * FROM `tbl_users` WHERE `credits` >'0' ORDER BY `credits` DESC LIMIT 0,5");
    $result->execute();
    for($i=0; $row = $result->fetch(); $i++){
?>

<!DOCTYPE html>
<html>

    <head>
        <link rel="stylesheet" href="style2.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <link href='https://fonts.googleapis.com/css?family=Great+Vibes' rel='stylesheet' type='text/css'>
    </head>

    <body>

    <div class="floated_elements">

    <table>

    <thead>
    <tr>
    <th> ID </th>
    <th> Wie </th>
    <th> Hoeveel </th>
    <th> email </th>
    </tr>
    </thead>

    <tbody>

    <tr>
    <td><?php echo $row['userID']; ?></td>
    <td><?php echo $row['userName']; ?></td>
    <td><?php echo $row['credits']; ?></td>
    <td><?php echo $row['userEmail']; ?></td>
    </tr>

    </tbody>

    </table>


    <?php
    }
    ?>

    </div> <!-- Closing floated elements -->

    <script>
    $('table tr').each(function(){
    $(this).find('th').first().addClass('first');
    $(this).find('th').last().addClass('last');
    $(this).find('td').first().addClass('first');
    $(this).find('td').last().addClass('last');
    });

    $('table tr').first().addClass('row-first');
    $('table tr').last().addClass('row-last');
    </script>

    </body>
</html>

我发现 for 规则并尝试应用它但没有真正给出正确的结果

编辑以提供额外的代码原因要求

最佳答案

您已经启动了 for 循环并将所有内容放入其中。这不是正确的方法。尝试如下:

<?php
include('connect.php');
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style2.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Great+Vibes'
    rel='stylesheet' type='text/css'>
</head>
<body>
    <div class="floated_elements">
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Wie</th>
                    <th>Hoeveel</th>
                    <th>email</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $result = $db->prepare("SELECT * FROM `tbl_users` WHERE `credits` >'0' ORDER BY `credits` DESC LIMIT 0,5");
                $result->execute();
                for($i=0; $row = $result->fetch(); $i++){ ?>
                <tr>
                    <td><?php echo $row['userID']; ?></td>
                    <td><?php echo $row['userName']; ?></td>
                    <td><?php echo $row['credits']; ?></td>
                    <td><?php echo $row['userEmail']; ?></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
    <script>
    $('table tr').each(function(){
        $(this).find('th').first().addClass('first');
        $(this).find('th').last().addClass('last');
        $(this).find('td').first().addClass('first');
        $(this).find('td').last().addClass('last');
    });
    $('table tr').first().addClass('row-first');
    $('table tr').last().addClass('row-last');
    </script>
</body>
</html>

关于php - 样式化 "order by desk"查询表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34676601/

相关文章:

PHP:在静态变量中引用另一个静态变量

php - 站点用户可以更改 cookie 或 session 数据吗?

javascript - 使用 Javascript/jQuery 使用 spritesheet 从一个类淡化到另一个类

css - 带有可选和可变参数的 SASS Mixin

javascript - CUFON 不适用于超大 slider

sql - 从数据库中的每个用户获取最后一个条目

php - 基于 PHP 和 jQuery 等开源的 Web 应用程序的含义

php - 我还能做些什么来优化这个 MySQL 查询吗?

c# - 如何使用C#检查SQL查询是否成功

mysql - 从 SQL 中的单列返回所有唯一可能组合的 PAIR