【问题标题】:styling a "order by desk" query table设置“order by desc”查询表的样式
【发布时间】:2016-01-08 20:18:05
【问题描述】:

我制作了一个脚本,通过 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 循环在哪里?
  • for loop.. 对我来说有些新东西,我在谷歌上搜索 :)
  • 你会发布完整的代码吗?

标签: php css sql


【解决方案1】:

您已经启动了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>

【讨论】:

  • 太棒了!现在 FOR 规则是什么导致表格知道如何显示?
  • 它做到了 :) 但只是为了让我学习它,for 规则会告诉表格它应该如何表现?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
相关资源
最近更新 更多