【问题标题】:Print PHP table from database [closed]从数据库中打印 PHP 表 [关闭]
【发布时间】:2020-10-27 15:32:28
【问题描述】:

我想从我的数据库“xxx”中打印/回显一个“用户”表,按照谁拥有最大金额(数量/整数)的顺序,但我需要该表“钱”中两列之间的总和 + “银行”。

所以我们有一个数据库名称:'xxx' 表名:'用户' 列:“钱”和“银行”

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

此处为示例:

到目前为止我所做的是这样的:


<?php 

$servername = "svname.com";
$username = "";
$password = "";
$dbname = "xxx";

$conn = mysqli_connect($servername, $username, $password, $dbname) OR DIE ('Unable to connect to database! Please try again later.');
<?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">
            <?php
                $sql = "SELECT * FROM users LIMIT 10";
                $result = mysqli_query($conn, $sql);
                if (mysqli_num_rows($result) > 0) {
                    while ($row = mysqli_fetch_assoc($result)) {
                        echo "<p>" . "<span>Name</span>" . $row['firstname'] . "<span>Money</span>" . $row['money'] . "<span>Bank</span>" . $row['bank'] . "</p>";
                    }
                } else {
                    echo "There are no users!";
                }
            ?>
            
            <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>
                <tr>
                    <th scope="row">1</th>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <th scope="row">2</th>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <th scope="row">3</th>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <th scope="row">4</th>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                </tbody>
            </table>
        </div>

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

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

    $rankingNewCount = $_POST['rankingNewCount'];

    $sql = "SELECT * FROM users LIMIT $rankingNewCount";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<p>" . "<span>Name</span>" . $row['firstname'] . "<span>Money</span>" . $row['money'] . "<span>Bank</span>" . $row['bank'] . "</p>";
        }
    } else {
        echo "There are no users!";
    }
?>

【问题讨论】:

  • 你到底想要什么?? money &bank 的总和?
  • 我不知道如何使用 PHP 使表格动态化,当我按下按钮在表格上加载更多 10 行时。我的代码有效,我从数据库中获取结果,但我不知道怎么用ph​​p做表。
  • 像从数据库中检索它们一样输出行是否重要(在这种情况下,答案必须改进 mysql 查询)否则您可以在检索它们之后和输出它们之前对行进行排序
  • 您目前的问题是什么?您是否正在为前端、PHP 和 MySQL 苦苦挣扎?
  • 好吧,我从数据库中获得了我需要的所有信息,但我不知道如何制作 Money + Bank 并显示总金额,然后按总金额进行排名,如排名。

标签: javascript php jquery mysql


【解决方案1】:

您可以更改查询以计算总数: SELECT *, (money + bank) AS total FROM users LIMIT 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>```

【讨论】:

    【解决方案2】:

    你每次都需要追加结果,所以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;
    

    ?>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多