我有一个图片库,目前我将结果限制为 10 个。
我想通过 jQuery 做的是单击“加载更多结果”按钮, 在数据库中加载接下来的 10 行(图像)。
目前我使用类 query.php 进行查询调用,然后在我的 index.php 中用 foreach 循环显示数据。
我在想也许可以使用查询字符串来更改点击时第 10-20 行的限制,但在这方面遇到了困难。
我们将不胜感激。
类query.php
public function Query($db_table, $limit) {
$this->db_query = mysql_query("SELECT * FROM $db_table ORDER BY time DESC limit $limit");
while($row = mysql_fetch_array($this->db_query)) {
$rows[] = $row;
}
return $rows;
}
索引.php
$lim = $_GET['limit'];
$Results = $Database->Query('info', $lim);
if(is_array($Results)) {?>
foreach ($Results as $Result) {
//echo results
j查询
$("#my_container").load("../lib/class.database.php?limit=10,20" );
亲切的问候, 亚当
最佳答案
很简单:
1.) 您对 PHP 脚本进行 AJAX 调用
$.ajax(
{
url: "get-ajax-images.php",
data: "mode=ajax&start=10&end=20",
success: function() { /* ... */ },
error: function() { /* ... */ }
});
2.) PHP:根据start
和end
从数据库加载数据
<?php
header("Content-type: application/json");
// Please use intval() to avoid SQL injections!!
$start = isset($_GET['start']) ? intval($_GET['start']) : exit('{"error": true}');
$end = isset($_GET['end']) ? intval($_GET['end']) : exit('{"error": true}');
/* valid start and end? */
if ( $start < 0 || $end < 0 )
exit('{"error": true}');
if ( ($end - $start) <= 0 ||
($end - $start) > 15 )
exit('{"error": true}');
/* All right! */
$sql = "SELECT id, imagepath FROM images LIMIT $start, $end";
$result = mysql_query($sql) or exit('{"error": true}');
$data = array();
while ( $row = mysql_fetch_assoc($result) )
{
$data[] = $row;
}
echo json_encode($data);
关于PHP通过JQuery请求MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8150383/