【发布时间】:2018-03-18 21:00:38
【问题描述】:
我正在寻找有关此分页尝试出错的地方的一些指导,下面是我的 index.php 文件的示例 - 加载 index.php 后,一切似乎都按预期工作,我得到了我的结果从数据库中,它被限制在设置的限制内,并且底部有正确的链接数,问题是当我点击链接去 index.php?page=1 或 index.php?page=2 等它向我抛出一个错误,提示我的 $query 为 0。
这是我用来自学语言的项目 - 所以任何一般性的提示也很感激。
感谢您的宝贵时间。
<?php
error_reporting(E_ERROR);
require_once 'core/init.php';
$page_index = "index";
$output = NULL;
$page=$_GET["page"]; // get page ID
$page_limit = 8; // set limit variable to restrict data returned
if($page == "" | !$page=="1"){ // if page is 1 or first time loading set
starting variable for pagination to 0
$page1=0;
} else {
$page1 = ($pages*$page_limit)-$page_limit; // adjust limit variable
}
if(Session::exists('home')) {
echo '<p>' . Session::flash('home') . '</p>';
}
include('config.php');
include('header.php');
$query=$conn->query("SELECT * FROM `rif_listings` LIMIT $page1,$page_limit");
if($query->num_rows > 0) {
while($rows = $query->fetch_assoc())
{
$prop_id = $rows['auto_id'];
$prop_postcode = $rows['prop_pc'];
$prop_address= $rows['prop_add1'];
$prop_town= $rows['prop_add2'];
$prop_description = $rows['prop_desc'];
$prop_description = substr($prop_description, 0, 90);
$prop_rent= $rows['prop_rent'];
$prop_image = $rows['prop_imagethumb'];
$prop_postcode = strtoupper($prop_postcode);
$output .= "<div id='product_display'>
<a href='properties.php?id=$prop_id' target='_self'>
<img class='default_thumbnail' src='core.includes/core.imgbin/listings/$prop_id/$prop_image' />
<h5>Post Code: $prop_postcode</h5>
<b>$prop_address,</b><br /><b>$prop_town</b><br /><br />
<span class='description'>$prop_description...</span><br /><br />
<span class='price'>£$prop_rent pcm</span><br /><br />
<input class='basket_button' type='submit' value='View Listing' /><br /><br />
<span>Unique ID: $prop_id</span>
</a>
</div>";
}
} else {
$output = "Whoops! Something went wrong, please try again.<br />If the problem persists please contact the Administrator.";
}
?>
<?php include('nav.php'); ?>
<?php include('sidebar.php'); ?>
<section id="core_content">
<?php
//page pagination
$query=$conn->query("SELECT * FROM `rif_listings`");
$count = $query->num_rows;
$pages = $count/$page_limit;
$pages = ceil($pages);
// echo out result to check it is getting correct output
echo $pages . " pages found";
echo ("<br />");
// echo out db data
echo $output;
echo ("<br />");
// create links to paginate through data
for($a=1;$a<=$pages;$a++) {
?> <a href="index.php?page=<?php echo $a; ?>"> <?php echo $a. " "; ?></a> <?php
}
include('banner.php');
?>
</section>
<?php include('footer.php'); ?>
【问题讨论】:
-
if($page == "" | !$page=="1")- 那应该做什么?我很确定你想要if($page == "" || $page != "1")。 -
原来我不想要!总之,感谢您抽出宝贵时间回复
-
你根本不需要第二个条件。使用
$page=="1"else的情况会很好:$page1 = (1*$page_limit)-$page_limit = 0 -
你是对的,这是我正在重新查看的旧代码,这是错误捕获的前兆,如果我有 2 个页面并且有人试图访问 ?page=3 我想发送它们到第 1 页,但这只会使每个页面都等于 1
if($page == "" || $page>$pages)
标签: php mysql pagination