I need some help with the category part on a old project .
The problem what I have at this moment that, when I click on the category all categories are showing up on the category.php page itself.
What whI wanne have when I click Uncategorised that in the category.php page only post are showing that have been made in Uncategorised category
function get_posts() {
$sql = "SELECT
`blog`.`post_id` AS `id`,
`blog`.`post_title` AS `title`,
LEFT(`blog`.`post_body`, 512) AS `preview`,
`blog`.`post_user` AS `user`,
`blog_categories`.`id` AS `category_id`,
`blog_categories`.`name` AS `category_name`,
DATE_FORMAT(`blog`.`post_date`, '%d-%m-%Y %H:%i') AS `date`,
`blog_comments`.`total_comments`,
DATE_FORMAT(`blog_comments`.`last_comment`, '%d-%m-%Y %H:%i') AS `last_comment`
FROM `blog`
INNER JOIN `blog_categories` ON `blog_categories`.`id` = `blog`.`cat_id`
LEFT JOIN (
SELECT
`post_id`,
COUNT(`comment_id`) AS `total_comments`,
MAX(`comment_date`) AS `last_comment`
FROM `blog_comments`
GROUP BY `post_id`
) AS `blog_comments`
ON `blog`.`post_id` = `blog_comments`.`post_id`
ORDER BY `blog`.`post_date` DESC";
$posts = mysql_query($sql);
$rows = array();
while (($row = mysql_fetch_assoc($posts)) !== false) {
$rows[]= array(
'id' => $row['id'],
'title' => $row['title'],
'preview' => $row['preview'],
'user' => $row['user'],
'date' => $row['date'],
'category_id' => $row['category_id'],
'category_name' => $row['category_name'],
'total_comments' => ($row['total_comments'] === null) ? 0 : $row['total_comments'],
'last_comments' => ($row['last_comment'] === null) ? 'never' : $row['last_comment']
);
}
return $rows;
}
function get_post($pid) {
$pid = (int)$pid;
$sql = "SELECT
`post_title` AS `title`,
`post_body` AS `body`,
`post_user` AS `user`,
`blog_categories`.`id` AS `category_id`,
`blog_categories`.`name` AS `category_name`,
DATE_FORMAT(`post_date`, '%d-%m-%Y %H:%i') AS `date`
FROM `blog`
INNER JOIN `blog_categories` ON `blog_categories`.`id` = `blog`.`cat_id`
WHERE `post_id` = '$pid'";
$post = mysql_query($sql);
$post = mysql_fetch_assoc($post);
$post['blog_comments'] = get_comments($pid);
return $post;
}
The old way was like this
if ( isset($cat_id)) {
$cat_id = (int) $cat_id;
$query .= " WHERE `blog_categories`.`id` = $cat_id";
}
Database
blog_categories
id | name |
-----------
4 Uncategorised
1 Youtube
blog
post_id | cat_id | post_title | post_body | post_user | post_date
-----------------------------------------------------------------------------------
4 | 1 | Youtube | Youtube link | Admin | 2020-09-13 16:10:53
Web server
Apache/2.2.21 (Win64) PHP/5.3.10
MySQL-client versie: mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $
PHP uitbreiding: mysqli Documentatie
phpMyAdmin
Versie informatie: 3.4.10.1, meest recente versie: 5.0.2
I hope someone can help me with this problem