0

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?

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 4
    you're mixing MySQL functions for one thing. `mysql_fetch_assoc` so that will fail you. – Funk Forty Niner Jun 05 '15 at 16:21
  • 1
    Your if statements are not working as you expect them to, but they are working properly. ;) Have a look at [in_array()](http://php.net/manual/en/function.in-array.php). –  Jun 05 '15 at 16:21
  • 2
    and make sure that short open tags are enabled; *an insight*. `=` if not, do ` – Funk Forty Niner Jun 05 '15 at 16:25

1 Answers1

5

You can't compare a scalar variable to an array. You need to us in_array() here:

if(isset($_GET['cat']) && in_array($_GET['cat'], $cat_array)) {

And as Fred pointed out, it won't work anyway until you fix your mixing of mysql and mysqli functions.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • funny, those `l_` usually just stand out and grab me by the collar. ;-) – Funk Forty Niner Jun 05 '15 at 16:23
  • I changed that code and still nothing, I also fixed the mysql function, didn't even notice that for some reason. Not even the source code is showing. – DOLPHINS Jun 05 '15 at 16:38
  • [Turn on error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) to see if PHP is reporting an error. – John Conde Jun 05 '15 at 16:42