Okay, so I got this site im working on to better myself with php, And I got a category system working in a switch and also a pagination, but i can't really get it to work together.
What I got so far graps all 4 posts from my DB and displays it on the "blog.php" page with the pagination making it only show 1 result at a time, blog.php?page=2 showing the next and so on, and it all works great.
Now clicking on the category "test" will display 2 results on the page "blog.php?category=2", still showing 1 by the pagination, but clicking on the next page "blog.php?category=2?page=2" will cause it to give the warnings
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
The code i got is this
<?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;?>