我对这一切都很陌生,所以如果这不可能,请纠正我。
我正试图用以下SQL语句显示数据:
SELECT * FROM trail101 WHERE activities IN(\'Murals\',\'Concert\',\'Theater\',\'Museums\') ORDER BY id DESC LIMIT 0, 9
它只显示了我拥有的前9个数据片段,但我希望能够导航到下一个9,在这个9中,单击分页将更新下一页的SQL语句,以此类推。我该如何编写脚本呢?我有以下导航代码:
<div class="w3-center w3-padding-32">
<div class="w3-bar">
<a href="#" class="w3-bar-item w3-button w3-hover-black" class="w3-bar-item w3-button w3-hover-black">«</a>
<a href="#" class="w3-bar-item w3-black w3-button">1</a>
<a href="#" class="w3-bar-item w3-button w3-hover-black">2</a>
<a href="#" class="w3-bar-item w3-button w3-hover-black">3</a>
<a href="#" class="w3-bar-item w3-button w3-hover-black">4</a>
<a href="#" class="w3-bar-item w3-button w3-hover-black">»</a>
</div>
最佳答案
在查询分页时得到了一个简单的代码示例,请查看
<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'password';
$rec_limit = 10;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test_db');
/* Get total number of records */
$sql = "SELECT * FROM trail101 WHERE activities IN(\'Murals\',\'Concert\',\'Theater\',\'Museums\') ORDER BY id";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT * ".
"FROM trail101 ".
"WHERE activities IN(\'Murals\',\'Concert\',\'Theater\',\'Museums\') ORDER BY id ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "ID :{$row['id']} <br> ".
"ACTIVITY NAME : {$row['activity_name']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 ) {
$last = $page - 2;
echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a> |";
echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
}else if( $page == 0 ) {
echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a>";
}
mysql_close($conn);
?>
</body>
</html>
关于javascript - 在页面导航时更改SQL语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49291637/