vendredi 5 juin 2015

Some reason my if statements aren't working properly

Everything else should be fine, but I still have a problem displaying the else function. In theory I believe everything should work. I looked at all of the syntax and everything looks fine, everything is properly closed. What I'm trying to do is, if the $_GET['cat'] == 1, 2, or 3 (which is why I made it an array) are valid then I want be able to pull just that specific category, if not then I want it to display all the categories together. Much like wordpress's functionality.

<?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

    }
}


?>

Am I missing something?

Aucun commentaire:

Enregistrer un commentaire