其他一切都应该没问题,但是我仍然在显示else功能时遇到问题。理论上我相信一切都应该有效。我查看了所有语法,一切看起来都很好,一切都已正确关闭。我想要做的是,如果$_GET['cat'] == 1, 2, or 3
(这就是为什么我把它作为一个数组)是有效的,那么我希望能够拉出那个特定的类别,如果没有那么我希望它显示所有的类别在一起。很像WordPress的功能。
<?php
include('config.inc.php');
// security check to make sure it's not grabbing anything extra that's not there
$get_cat_id = mysqli_query($mysql_conn, "SELECT `id` FROM `categories`");
$cat_array = array();
while($row = mysql_fetch_assoc($get_cat_id)) {
$cat_array[] = $row['id'];
}
echo $cat_array[];
// checks to see if it's calling for a specific category
if(isset($_GET['cat']) && ($_GET['cat'] == $cat_array)) {
$fetch_post_content = mysqli_query($mysql_conn, "SELECT * FROM `posts` WHERE `category_id` = '".$_GET['cat']."' ORDER BY `id` DESC");
$fetch_category_content = mysqli_query($mysql_conn, "SELECT * FROM `categories` WHERE `id` = '".$_GET['cat']."'");
?>
<h1 class="category-title"><?= $fetch_category_content->name; ?></h1>
<?php
while($post_content = mysqli_fetch_object($fetch_post_content)) { ?>
<article>
<?php
// this checks for and grabs the featured image from the database
if(isset($post_content->img_id)) {
$fetch_post_image = mysqli_query($mysql_conn, "SELECT * FROM `images` WHERE `id` = '".$post_content->img_id."'");
?>
<img src="<?= $fetch_post_image->url; ?>">
<?php } ?>
<h3 class="post-title"><?= $post_content->title; ?></h3>
<h5 class="post-datetime">Posted on <?= $post_content->date; ?> at <?= $post_content->time; ?></h5>
<p class="post-content"><?= $post_content->content; ?></p>
<?php
// grabs authors name from user database
$fetch_post_author = mysqli_query($mysql_conn, "SELECT * FROM `users` WHERE `id` = '".$post_content->author_id."'");
?>
<h6 class="post-author">Posted by <?= $fetch_post_author->name; ?></h6>
</article>
<?php
// ends while function
}
// displays all conent for the front page if no specific category is called
} else {
$fetch_post_content = mysqli_query("SELECT * FROM `posts` ORDER BY `id` DESC");
while($post_content = mysqli_fetch_object($fetch_post_content)) {
?>
<article>
<?php
// this checks for and grabs the featured image from the database
if(isset($post_content->img_id)) {
$fetch_post_image = mysqli_query($mysql_conn, "SELECT * FROM `images` WHERE `id` = '".$post_content->img_id."'");
?>
<img src="<?= $fetch_post_image->url; ?>">
<?php } ?>
<h3 class="post-title"><?= $post_content->title; ?></h3>
<h5 class="post-datetime">Posted on <?= $post_content->date; ?> at <?= $post_content->time; ?></h5>
<p class="post-content"><?= $post_content->content; ?></p>
<?php
// grabs authors name from user database
$fetch_post_author = mysqli_query($mysql_conn, "SELECT * FROM `users` WHERE `id` = '".$post_content->author_id."'");
?>
<h6 class="post-author">Posted by <?= $fetch_post_author->name; ?></h6>
</article>
<?php
}
}
?>
我错过了什么吗?
答案 0 :(得分:5)
您无法将标量变量与数组进行比较。您需要我们in_array()
:
if(isset($_GET['cat']) && in_array($_GET['cat'], $cat_array)) {
正如Fred指出的那样,在你修复mysql和mysqli函数的混合之前,它无论如何都不会起作用。