尝试分页查询(第二次实验失败,分页显示成功,但数据库拿到的总数据还是10条,待解决)

This commit is contained in:
flykhan 2023-10-28 15:29:52 +08:00
parent f9fe11d9dc
commit 1e59ddb4d7
2 changed files with 27 additions and 15 deletions

View File

@ -68,8 +68,8 @@ int main(int argc, char const *argv[])
char query_sql[128] = ""; char query_sql[128] = "";
sprintf( sprintf(
query_sql, query_sql,
"select temperature, humidity , th_date_time from temp_hum_info;"); // 构建查询 // "select temperature, humidity , th_date_time from temp_hum_info;"); // 构建查询
// "select temperature, humidity , th_date_time from temp_hum_info order by thid desc limit %d offset %d;", limit, (page - 1) * limit); // 构建查询 "select temperature, humidity , th_date_time from temp_hum_info ORDER BY thid DESC LIMIT %d OFFSET %d;", limit, (page - 1) * limit);
cJSON *result_array = cJSON_CreateArray(); cJSON *result_array = cJSON_CreateArray();

View File

@ -66,17 +66,20 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<!-- 下一页,上一页按钮 <!-- 下一页,上一页按钮 -->
<div class="page"> <div class="page">
<button onclick="prevPage()" id="prevPage">上一页</button> <button onclick="prevPage()" id="prevPage">上一页</button>
<button onclick="nextPage()" id="nextPage">下一页</button> <button onclick="nextPage()" id="nextPage">下一页</button>
</div> --> </div>
<script> <script>
var currentPage = 1; // 当前页码
function loadBlackData() { function loadBlackData() {
var blackData; var blackData;
fetch("../cgi-bin/green_house_info_show.cgi") // fetch("../cgi-bin/green_house_info_show.cgi")
// fetch("../cgi-bin/green_house_info_show.cgi?page="+currentPage+"&limit=10") fetch("../cgi-bin/green_house_info_show.cgi?page=" + currentPage + "&limit=10")
.then(resp => resp.json()) .then(resp => resp.json())
.then(data => { .then(data => {
blackData = data; blackData = data;
@ -101,16 +104,25 @@
loadBlackData(); loadBlackData();
} }
// // HTML 页面中的按钮点击事件处理程序 // HTML 页面中的按钮点击事件处理程序
// function nextPage() {
// currentPage++; // 增加当前页码
// loadBlackData(); // 重新加载数据
// }
// function prevPage() { function nextPage() {
// currentPage--; // 减少当前页码 if (currentPage > 10) {
// loadBlackData(); // 重新加载数据 alert("已经是最后一页了");
// } return;
}
currentPage++; // 增加当前页码
loadBlackData(); // 重新加载数据
}
function prevPage() {
if (currentPage < 1) {
alert("已经是第一页了");
return;
}
currentPage--; // 减少当前页码
loadBlackData(); // 重新加载数据
}
</script> </script>
</body> </body>