hi mate,
i want to ask something else, i have done with this but i didn’t make sure any security issues. so, just forget about that for while:
(isset($_GET['id']) ? $catId = $_GET['id'] : '');
require_once 'inc/database.php';
// Get all post from database
$getPosts = "SELECT posts.*, posts_categories.*, categories.*
FROM (posts
INNER JOIN posts_categories
ON posts.post_id = posts_categories.postid)
INNER JOIN categories
ON posts_categories.catid = categories.cat_id
WHERE categories.cat_id = '$catId'";
$nowPosts = $db->query($getPosts);
if (!$nowPosts)
{
$title = 'Please try again later!';
echo "<p class=\\"dbError\\">This category could not display. Please try again later.</p>";
}
else
{
if ($nowPosts->num_rows == 0)
{
$title = '404, Page Not Found!';
// I will set $posts[] array manually in below like this
/*
$posts = array (
array (
'title' => '404, Page Not Found!',
'post_id' => '',
'post_title' => '404, Page Not Found!',
'post_createon' => '',
'post_content' => 'There is no any content in this category.
Please select another category.',
));
*/
}
else
{
while ($post = $nowPosts->fetch_assoc())
{
$title = $post['cat_label'];
// here, i sent the content from database to $posts[] array
$posts[] = $post;
}
}
}
ok, according to my comment at the above code. i have sent all content that fetched from database (mysql) to $posts array. so i do this code in html template:
<?php
foreach ($posts as $post): ?>
<h1>
<a href="post.php?id=<?php echo $post['post_id']; ?>"
title="<?php echo $post['post_title']; ?>">
<?php echo $post['post_title']; ?>
</a>
</h1>
<p><em>Created Date: <?php echo $post['post_createon']; ?></em></p>
<p><?php echo $post['post_content']; ?></p>
<?php endforeach;
?>
based on the above code, i must have some content in $posts array but when i have no any content in that category that mean no any content in $posts i will get error like this:
Notice: Undefined variable: posts in /var/www/site/cat.php on line 117
Warning: Invalid argument supplied for foreach() in /var/www/site/cat.php on line 117
so i think i can add any value in $posts array manually like first code in above that I’ve comment them. but, it’s still make me disturbed cause it will make the title clickable and will go to this url:
http://localhost/site/post.php?id=
you can see that this title will redirect us to post.php page with no any [id] value
sorry for my bad english and explanation, but you can read my full code in attachment