【发布时间】: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> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<th scope="row">2</th>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<th scope="row">3</th>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<th scope="row">4</th>
<td> </td>
<td> </td>
<td> </td>
<td> </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 行时。我的代码有效,我从数据库中获取结果,但我不知道怎么用php做表。
-
像从数据库中检索它们一样输出行是否重要(在这种情况下,答案必须改进 mysql 查询)否则您可以在检索它们之后和输出它们之前对行进行排序
-
您目前的问题是什么?您是否正在为前端、PHP 和 MySQL 苦苦挣扎?
-
好吧,我从数据库中获得了我需要的所有信息,但我不知道如何制作 Money + Bank 并显示总金额,然后按总金额进行排名,如排名。
标签: javascript php jquery mysql