我不是一个精通程序员,所以我不想使用复杂的分页。有没有像我的查询中使用分页的替代方法我可以限制他们说20个结果然后如果他们超过20个结果我可以显示“下一个”按钮然后该按钮将进一步加载下一个结果??
我只想要“下一步”显示是否有20>结果如何?任何帮助将不胜感激。谢谢。
代码:
$query="SELECT * FROM actresses where actress_id = '$actressid' and production_full_name LIKE '%$q%'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "";
$i=0;
while ($i < $num) {
$1=mysql_result($result,$i,"production_full_name");
$2=mysql_result($result,$i,"production_id");
$3=mysql_result($result,$i,"actress");
echo "<br><div id=linkcontain><a id=slink href=$data/actress.php?id=$2>$3</a><div id=production>$1</div></div>";
echo "";
$i++;
}
答案 0 :(得分:3)
您可以为查询添加限制。
$lower_limit = $results_per_page * $page_number;
$upper_limit = $lower_limit + $results_per_page
..and production_full_name LIKE '%$q%' LIMIT $lower_limit, $upper_limit
然后制作条件“下一页”链接
if ($upper_limit > 20) echo '<a href="{page url}?page_number='.($page_number+1).'">Next</a>;
答案 1 :(得分:2)
您可以在Google上找到许多搜索示例。 例如http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx。
您只需编辑 $ rowsPerPage 了解结果数量,如果您想进一步修改从数据库获取的内容, $ query 。
但不一定要包括。
编辑:注意使用会对数据库查询产生影响的$ _GET的含义。你必须小心不要在其中允许危险值( mysql注入)。在网页的例子中,它使用ceil,所以我相信它会为非数值输出0,这样更安全。
答案 2 :(得分:1)
您可以使用PEAR包。 它们更容易使用类似于框架。 但在使用它们之前,您需要检查PEAR是否在您的服务器上运行。 如果您在本地使用它,则可以在本地计算机上安装PEAR包。有关安装PEAR的更多说明。 Click here查看安装步骤。
安装PEAR软件包后,请按照说明配置PAGER类。http://www.codediesel.com/php/simple-pagination-in-php/
Ĵ
答案 3 :(得分:1)
类似的东西:
$data = mysql_query("SELECT * FROM `table`");
$total_data = mysql_num_rows($data);
$step = 30;
$from = $_GET['p'];
$data = mysql_query("SELECT * FROM `table` LIMIT '.$from.','.$step.'"
创建链接:
$p=1;
for ($j = 0 ; $j <= $total_data+$step; $j+=$step)
{
echo ' <a href="page.php?p='.$j.'">'.$p.'</a> ';
$p++;
} ?>
未经测试。
答案 4 :(得分:1)
您可以执行的操作是获取数据集中有多少行的COUNT(*)
。然后决定每页需要多少项。 $numberOfRows / $itemsPerPage
为您提供了页数。在MySQL中,要限制使用的结果LIMIT X, Y
X是范围的开头,Y是要获取的项目数。因此,要获取页面上的所有项目,您的查询将为LIMIT ($currentPage * $itemsPerPage), $itemsPerPage
然后由您决定编写视图逻辑,具体取决于您希望如何格式化分页。只需从后到前:如果当前页面为1,则禁用上一个按钮。如果页面不是1,则当前页码是从头开始的页数。如果总页数大于当前页面,则从当前页面开始计数,直到最后一页(除非您想在某个时刻切断页面数量)
无论方向如何,我都使用以下方法来帮助创建页面列表。我可以插入5作为当前页面,0作为目标页面,它将返回一个5页的列表供我按顺序迭代。
/*
* Generates a list of pages based on the current page to the goal page number
*/
function generatePageList($currentPage, $goal) {
$span = $goal - $currentPage;
$step = abs($span) / $span;
$list = array();
for($x = $currentPage; $x !== $goal;) {
// We want to add the step at the beginning of the loop, instead of
// at the end
// Fixes bug when there are only two pages
$x += $step;
$list[] = $x;
}
return $list;
}
答案 5 :(得分:1)
如果您的查询设置为
"SELECT SQL_CALC_FOUND_ROWS * FROM actresses where actress_id = '$actressid' and production_full_name LIKE '%$q%' LIMIT ".($page*$lpage).",".$lpage
...下一个语句可以SELECT FOUND_ROWS()
来获得实际的结果数。
不要忘记将mysql_real_escape_string()
放在这些变量上。
永远不要将SELECT *
与mysql_num_rows一起使用;如果它是一张大桌子,它会浪费很多时间。
答案 6 :(得分:0)
这是创建数据分页的更简单方法
<?php
require_once 'db_connect.php';
$limit = 20;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$sql = "SELECT * FROM employee ORDER BY id ASC LIMIT $start_from, $limit";
$rs_result = mysqli_query($con, $sql);
?>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Salary</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($rs_result)) {?>
<tr>
<td><?php echo $row["employee_name"]; ?></td>
<td><?php echo $row["employee_salary"]; ?></td>
<td><?php echo $row["employee_age"]; ?></td>
</tr>
<?php
};
?>
</tbody>
</table>
<?php
$sql = "SELECT COUNT(id) FROM employee";
$rs_result = mysqli_query($con, $sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
$pagLink = "<nav><ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++){
$pagLink .= "<li><a href='index.php?page=".$i."'>".$i."</a></li>";
};
echo $pagLink . "</ul></nav>";
?>
<script type="text/javascript">
$(document).ready(function(){
$('.pagination').pagination({
items: <?php echo $total_records;?>,
itemsOnPage: <?php echo $limit;?>,
cssStyle: 'light-theme',
currentPage : <?php echo $page;?>,
hrefTextPrefix : 'index.php?page='
});
});
</script>