0
好吧,所以我得到了這個網站即時通訊工作,以更好地與自己的PHP,我得到了一個類別系統工作在一個開關和分頁,但我不能真正得到它的工作一起。分頁和類別不一起工作
到目前爲止,我從我的數據庫中獲取了所有4篇文章,並將其顯示在「blog.php」頁面上,並且分頁使其每次只顯示1個結果,blog.php?page = 2顯示下一個等等,而且這一切都很好。
現在點擊「test」類別將在頁面「blog.php?category = 2」上顯示2條結果,分頁仍顯示1,但點擊下一頁「blog.php?category = 2 ?頁= 2" 將導致它給予警告
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\rusting\blog.php on line 284
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\rusting\blog.php on line 325
我得到的代碼是這樣
<?php switch($category):
case 0: ?>
<?php
\t \t
//Fetch from database first 1 items which is its limit. For that when page open you can see first 10 items.
$query=mysqli_query($db,"
SELECT blog.id, blog.title, blog.date, blog.content, blog.image, blog.author_id, blog.category, blog.short_desc, category.id, category.name
FROM blog
INNER JOIN category
ON blog.category=category.id
ORDER BY blog.id DESC
LIMIT $start, $limit
");
//print 1 items
while($result=mysqli_fetch_array($query))
{
\t
echo "
\t <!-- Begin Post -->
<div class='post'>
<!-- Begin Post Info -->
<div class='post-info'>
<!-- Begin Date -->
<div class='post-date'> <span class='day'>15</span> <span class='month'>FEB</span> <span class='year'>2011</span> </div>
<!-- End Date -->
<!-- Begin Title -->
<div class='post-title'>
<h1><a href='post.php?id=".$result['id']."'>
\t \t \t ".$result['title']."
\t \t \t </a></h1>
<div class='post-meta'> <span class='comments'>13 Comments</span> <span class='categories'><a href='#'>".$result['name']."</a></span> </div>
</div>
<!-- End Title -->
</div>
<!-- End Post Info -->
<div class='post-text'>
\t \t <div class='post-img'><a href='post.php?id=".$result['id']."'><img src='style/images/blog/".$result['image']."' alt='' /></a></div><br/>
<p>".$result['short_desc']." <a class='more' href='post.php?id=".$result['id']."'>Læs mere →</a></p>
</div>
<!-- End Text -->
</div>
<!-- End Post -->
";
\t
}
//fetch all the data from database.
$rows=mysqli_num_rows(mysqli_query($db,"select * from blog"));
//calculate total page number for the given table in the database
$total=ceil($rows/$limit);
?>
<br><br><br><br>
<?php if($page>1)
{
\t //Go to previous page to show previous 10 items. If its in page 1 then it is inactive
\t echo "<li><a href='?page=".($page-1)."' class='button'>PREVIOUS</a></li>";
}
if($page!=$total)
{
\t ////Go to previous page to show next 10 items.
\t echo "<li><a href='?page=".($page+1)."' class='button'>NEXT</a></li>";
}
?>
<br><br><br><br>
<div class="page-navi">
<ul> \t \t
<?php
//show all the page link with page number. When click on these numbers go to particular page.
\t \t for($i=1;$i<=$total;$i++)
\t \t {
\t \t \t if($i==$page) { echo "<li><a class='current'>".$i."</a></li>"; }
\t \t \t
\t \t \t else { echo "<li><a href='?page=".$i."'>".$i."</a></li>"; }
\t \t }
?>
</ul>
</div> \t \t
\t \t
<?php break;?>
<?php case $category: ?>
<?php
//Fetch from database first 1 items which is its limit. For that when page open you can see first 10 items.
$query=mysqli_query($db,"
SELECT blog.id, blog.title, blog.date, blog.content, blog.image, blog.author_id, blog.category, blog.short_desc, category.id, category.name
FROM blog
INNER JOIN category
ON blog.category=category.id
WHERE category=".$category."
ORDER BY blog.id DESC
LIMIT $start, $limit
");
//print 1 items
while($result=mysqli_fetch_array($query))
{
\t
echo "
\t <!-- Begin Post -->
<div class='post'>
<!-- Begin Post Info -->
<div class='post-info'>
<!-- Begin Date -->
<div class='post-date'> <span class='day'>15</span> <span class='month'>FEB</span> <span class='year'>2011</span> </div>
<!-- End Date -->
<!-- Begin Title -->
<div class='post-title'>
<h1><a href='post.php?id=".$result['id']."'>
\t \t \t ".$result['title']."
\t \t \t </a></h1>
<div class='post-meta'> <span class='comments'>13 Comments</span> <span class='categories'><a href='#'>".$result['name']."</a></span> </div>
</div>
<!-- End Title -->
</div>
<!-- End Post Info -->
<div class='post-text'>
\t \t <div class='post-img'><a href='post.php?id=".$result['id']."'><img src='style/images/blog/".$result['image']."' alt='' /></a></div><br/>
<p>".$result['short_desc']." <a class='more' href='post.php?id=".$result['id']."'>Læs mere →</a></p>
</div>
<!-- End Text -->
</div>
<!-- End Post -->
";
\t
\t
}
//fetch all the data from database.
$rows=mysqli_num_rows(mysqli_query($db,"select * from blog WHERE category=".$category.""));
//calculate total page number for the given table in the database
$total=ceil($rows/$limit);
?>
<br><br><br><br>
<?php if($page>1)
{
\t //Go to previous page to show previous 10 items. If its in page 1 then it is inactive
\t echo "<li><a href='?category=".$category."?page=".($page-1)."' class='button'>PREVIOUS</a></li>";
}
if($page!=$total)
{
\t ////Go to previous page to show next 10 items.
\t echo "<li><a href='?category=".$category."?page=".($page+1)."' class='button'>NEXT</a></li>";
}
?>
<br><br><br><br>
<div class="page-navi">
<ul> \t \t
<?php
//show all the page link with page number. When click on these numbers go to particular page.
\t \t for($i=1;$i<=$total;$i++)
\t \t {
\t \t \t if($i==$page) { echo "<li><a class='current'>".$i."</a></li>"; }
\t \t \t
\t \t \t else { echo "<li><a href='?category=".$category."?page=".$i."'>".$i."</a></li>"; }
\t \t }
?>
</ul>
</div> \t \t
<?php break;?>
<?php endswitch;?> \t
[檢查此鏈接](http://stackoverflow.com/questions/3705318/simple-php-pagination-script)你會得到你需要的信息 – Orion