Javascript 从数据库打印PHP表

Javascript 从数据库打印PHP表,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我想从我的数据库“xxx”中打印/回显一个“users”表,顺序是谁拥有最大的货币数量/int,但我需要该表money+bank中两列之间的总和 所以我们有一个数据库名:“xxx” 表名:“用户” 专栏:“货币”和“银行” 我想制作一个非常简单的HTML表格,其中包含用户总收入的排名。 另外,我想给表指定位置,比如1,2,3,4 示例如下: 到目前为止,我所做的是: 您可以更改查询以计算总数: 从用户限制10中选择*,货币+银行作为总计 关于索引,您可以在创建表时将其添加到while循环中 例

我想从我的数据库“xxx”中打印/回显一个“users”表,顺序是谁拥有最大的货币数量/int,但我需要该表money+bank中两列之间的总和

所以我们有一个数据库名:“xxx” 表名:“用户” 专栏:“货币”和“银行”

我想制作一个非常简单的HTML表格,其中包含用户总收入的排名。 另外,我想给表指定位置,比如1,2,3,4

示例如下:

到目前为止,我所做的是:


您可以更改查询以计算总数: 从用户限制10中选择*,货币+银行作为总计

关于索引,您可以在创建表时将其添加到while循环中

例如:

<?php 
    include 'db.php';
?>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                var rankingCount = 10;
                $("button").click(function () {
                    rankingCount = rankingCount + 10;
                    $("#ranking").load("load-users.php", {
                        rankingNewCount: rankingCount
                    });
                });
            });
        </script>
    </head>

    <body>
        <div id="ranking">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tbody>
                <tr>
                    <th scope="col">No</th>
                    <th scope="col">Name</th>
                    <th scope="col">Money</th>
                    <th scope="col">Bank</th>
                    <th scope="col">Total</th>
                </tr>
                <?php
                    $sql = "SELECT *, (money + bank) AS total FROM users LIMIT 10";
                    $result = mysqli_query($conn, $sql);
                    if (mysqli_num_rows($result) > 0) {
                        $index = 1;
                        while ($row = mysqli_fetch_assoc($result)) {
                            echo '<tr>
                                <td scope="row">' . $index . '</td>
                                <td>' . $row['firstname'] . '</td>
                                <td>' . $row['money'] . '</td>
                                <td>' . $row['bank'] . '</td>
                                <td>' . $row['total'] . '</td>
                            </tr>';
                            $index++;
                        }
                    } else {
                        echo '<tr>
                            <td colspan="4">There are no users!</td>
                        </tr>';
                        echo ;
                    }
                ?>
                </tbody>
            </table>
        </div>

        <button>More 10 results here</button>
    </body>

</html>```

您每次都需要追加结果,因此JS代码可能是:

$(document).ready(function() {
     var rankingCount = 10;
     $("button").click(function () {
         rankingCount = rankingCount + 10;
         $.ajax({url:'load-users.php',
                 data: {rankingCount: rankingCount},
                 complete: function(resp){
                     $("#ranking").html( $('#ranking').html() + resp );

         }});
  });
Php应该简单到:

<?php
$offset = $_POST['rankingCount'] || 0;
$sql = "SELECT name as Name, money as Money, bank as Bank, (money+bank) as total FROM users LIMIT 10, ".$offset;
$result = mysqli_query($conn, $sql);
$buff = '<table width=""><thead><col>'. implode('</col><col>', array_keys($result[0]) ).'</col></thead><tr>';

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)){
        $buff .= implode('</tr><tr>', array_map( function($i){ return '<span>'.$i.'</span>'; }, $row ) );
    }
} else {
    $buff .= '<td colspan="4">There are no users!</td>';
}
$buff .= '</tr></table>';
echo $buff;

?>

你到底想要什么??钱和银行的总和?我不知道如何使用PHP使表动态化,当我按下按钮在表上加载更多的10行时。。我的代码工作正常,我从数据库中获得结果,但我不知道如何使用php生成表。像从数据库中检索行那样输出行是否很重要?在这种情况下,答案必须改进mysql查询,否则您可以在检索行之后和输出行之前对行进行排序您当前的问题是什么?你在为前端、PHP和MySQL而挣扎吗?好吧,我从数据库中获得了我需要的所有信息,但我不知道如何赚钱+银行,显示总数,然后按照总金额排名,就像排名一样。
<?php 
    include 'db.php';
?>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                var rankingCount = 10;
                $("button").click(function () {
                    rankingCount = rankingCount + 10;
                    $("#ranking").load("load-users.php", {
                        rankingNewCount: rankingCount
                    });
                });
            });
        </script>
    </head>

    <body>
        <div id="ranking">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tbody>
                <tr>
                    <th scope="col">No</th>
                    <th scope="col">Name</th>
                    <th scope="col">Money</th>
                    <th scope="col">Bank</th>
                    <th scope="col">Total</th>
                </tr>
                <?php
                    $sql = "SELECT *, (money + bank) AS total FROM users LIMIT 10";
                    $result = mysqli_query($conn, $sql);
                    if (mysqli_num_rows($result) > 0) {
                        $index = 1;
                        while ($row = mysqli_fetch_assoc($result)) {
                            echo '<tr>
                                <td scope="row">' . $index . '</td>
                                <td>' . $row['firstname'] . '</td>
                                <td>' . $row['money'] . '</td>
                                <td>' . $row['bank'] . '</td>
                                <td>' . $row['total'] . '</td>
                            </tr>';
                            $index++;
                        }
                    } else {
                        echo '<tr>
                            <td colspan="4">There are no users!</td>
                        </tr>';
                        echo ;
                    }
                ?>
                </tbody>
            </table>
        </div>

        <button>More 10 results here</button>
    </body>

</html>```
$(document).ready(function() {
     var rankingCount = 10;
     $("button").click(function () {
         rankingCount = rankingCount + 10;
         $.ajax({url:'load-users.php',
                 data: {rankingCount: rankingCount},
                 complete: function(resp){
                     $("#ranking").html( $('#ranking').html() + resp );

         }});
  });
<?php
$offset = $_POST['rankingCount'] || 0;
$sql = "SELECT name as Name, money as Money, bank as Bank, (money+bank) as total FROM users LIMIT 10, ".$offset;
$result = mysqli_query($conn, $sql);
$buff = '<table width=""><thead><col>'. implode('</col><col>', array_keys($result[0]) ).'</col></thead><tr>';

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)){
        $buff .= implode('</tr><tr>', array_map( function($i){ return '<span>'.$i.'</span>'; }, $row ) );
    }
} else {
    $buff .= '<td colspan="4">There are no users!</td>';
}
$buff .= '</tr></table>';
echo $buff;