I am creating a project where I have created a system that allows a user to create and publish blog posts. I want to be able to have a drop down function that will filter and display posts by a specific category they are tagged with. How would I go about doing that?
This is not a WordPress site, and any research I've been doing on my own has mainly turned up results for WordPress sites.
If anyone could help me or at least point me in a good direction, that would be great. I'm fairly new to working with PHP so I will take any help I can get.
Below is the code I have so far.
Here is the PHP I have at the top of the file:
<?php
include('includes/db_connect.php');
$queryString = "SELECT post_id, title, price, image, LEFT(description, 300) AS description, category FROM post INNER JOIN categories ON categories.category_id=post.category_id WHERE category = '" . $_GET["category"] . "' ORDER BY post_id DESC LIMIT $start, $per_page";
$_GET["category"] . "' ORDER BY post_id DESC LIMIT $start, $per_page";
$query = $db->prepare($queryString);
$query->execute();
$query->bind_result($post_id, $title, $price, $image, $description, $category);
?>
I am also unable to make the dropdown work correctly. I am trying to get it to grab the categories from the database since I have it so the poster can create categories for the posts. I created a drop down similar to this on the post creation page, but I am now trying to get it to work for this filtering dropdown. Right now my biggest problem is not being able to get the to show categories or even show anything else on the page that goes below it. If I remove it, then my posts all show. I tried ending the while loop for it, but I cannot find a way for it to actually let me do that.
Here is the part of the index.php where the posts are being displayed:
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" id="centertext">
<h1>Posts</h1>
<!-- THIS IS THE DROPDOWN /-->
<article>
<form>
<select name="category" class="form-control">
<?php
$query = $db->query("SELECT * FROM categories");
while($row = $query->fetch_object()){
echo "<option value='index.php?category=".$row->category_id."'>".$row->category."
</option>";
exit();
}
?>
</select>
<button type="submit" name="submit" value="Submit" class="btn btn-default">SEARCH</button>
</form>
</article>
<?php
while ($query->fetch()):
$lastspace = strrpos($description, '');
?>
<!-- THIS IS THE STRUCTURE FOR EACH POST /-->
<article>
<div class="preview">
<div class="ptop">
<?php echo "<img src='admin/images/".$image."' width='100%' height='100%' >";?>
<div class="basicinfo">
<h2><?php echo $title?></h2>
<?php echo $category?>
</div>
</div>
<div class="pbottom">
<p><?php echo substr($description, $lastspace).'...<br><br><a href="post.php?id='.$post_id.'">VIEW POST</a>'?></p>
</div>
</div>
</article>
<?php endwhile?>
</div>
</div>
</div>
The following are the post and categories tables from my database:
POST
CREATE TABLE `post` (
`post_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`category_id` int(11) NOT NULL,
`description` text NOT NULL,
`posted` datetime NOT NULL,
`photo_id` int(11) DEFAULT NULL,
`image` varchar(300) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CATEGORIES
CREATE TABLE `categories` (
`category_id` int(11) NOT NULL,
`category` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;