其他一切都应该没问题,但我仍然有问题显示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
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
您无法将标量变量与数组进行比较.你需要我们in_array()在这里:
if(isset($_GET['cat']) && in_array($_GET['cat'], $cat_array)) {
Run Code Online (Sandbox Code Playgroud)
正如弗雷德指出的那样,在你修复mysql和mysqli函数的混合之前,它无论如何都无法工作.
| 归档时间: |
|
| 查看次数: |
56 次 |
| 最近记录: |