分页和类别不一起工作

时间:2016-02-29 19:42:41

标签: php pagination switch-statement

好的,所以我得到了这个网站我正在努力改善自己的PHP,我有一个类别系统工作在一个开关和分页,但我不能真正让它一起工作。

到目前为止,我从我的数据库中抓取了所有4个帖子并将其显示在" blog.php"分页的页面一次只显示1个结果,blog.php?page = 2显示下一个等等,这一切都很有效。

现在点击类别"测试"将在页面上显示2个结果" blog.php?category = 2",仍然按分页显示1,但点击下一页" blog.php?category = 2?page = 2& #34;会使它发出警告

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
		
//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))
{
	

echo "
	  <!-- 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']."'>
			  ".$result['title']."
			  </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'>
		  <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 --> 
";
	

}

//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)
{
	//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
	echo "<li><a href='?page=".($page-1)."' class='button'>PREVIOUS</a></li>";
}
if($page!=$total)
{
	////Go to previous page to show next 10 items.
	echo "<li><a href='?page=".($page+1)."' class='button'>NEXT</a></li>";
}
?>


<br><br><br><br>

<div class="page-navi">
<ul>		
<?php
//show all the page link with page number. When click on these numbers go to particular page. 
		for($i=1;$i<=$total;$i++)
		{
			if($i==$page) { echo "<li><a class='current'>".$i."</a></li>"; }
			
			else { echo "<li><a href='?page=".$i."'>".$i."</a></li>"; }
		}
?>
 </ul>
</div>		
		

<?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))
{
	
echo "
	  <!-- 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']."'>
			  ".$result['title']."
			  </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'>
		  <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 --> 
";
	

	
}

//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)
{
	//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
	echo "<li><a href='?category=".$category."?page=".($page-1)."' class='button'>PREVIOUS</a></li>";
}
if($page!=$total)
{
	////Go to previous page to show next 10 items.
	echo "<li><a href='?category=".$category."?page=".($page+1)."' class='button'>NEXT</a></li>";
}
?>



<br><br><br><br>

<div class="page-navi">
<ul>		
<?php
//show all the page link with page number. When click on these numbers go to particular page. 
		for($i=1;$i<=$total;$i++)
		{
			if($i==$page) { echo "<li><a class='current'>".$i."</a></li>"; }
			
			else { echo "<li><a href='?category=".$category."?page=".$i."'>".$i."</a></li>"; }
		}
?>
 </ul>
</div>		

<?php break;?>
<?php endswitch;?>	
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

好的,这很简单,我无法相信我之前没有意识到这一点。我只需要更改为其生成的链接。

我试图通过“blog.php?category = 2?page = 2”访问类别2的第2页 为了这一切工作我只需要将其更改为“blog.php?category = 2&amp; page = 2”