Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Displaying Categories

Colin Sharkey
Colin Sharkey
14,631 Points

Images not displaying

I have been following allow with the php media library website tutorial and everything was straight forward until I added the categories function and all the images disappeared from the books/movies/music sections.

I've looked through the code and compared to the video but cant find whats wrong.

If anyone can help I'd really appreciate it!

header.php:

<html> <head> <title><?php echo $pageTitle; ?></title> <link rel="stylesheet" href="css/style.css" type="text/css"> </head> <body>

<div class="header">

    <div class="wrapper">

        <h1 class="branding-title"><a href="./">Personal Media Library</a></h1>

        <ul class="nav">
            <li class="books <?php if ($section == "books") { echo " on"; }  ?>"><a href="catalog.php?cat=books">Books</a></li>
            <li class="movies <?php if ($section == "movies") {echo " on";} ?>"><a href="catalog.php?cat=movies">Movies</a></li>
            <li class="music <?php if ($section == "music") {echo " on";} ?>"><a href="catalog.php?cat=music">Music</a></li>
            <li class="suggest <?php if ($section == "suggest") {echo " on";} ?>"><a href="suggest.php">Suggest</a></li>
        </ul>

    </div>

</div>

<div id="content"> <!-- Start of content -->

function.php:

<?php function get_item_html($id, $item){ $output = "<li><a href='#'><img src='" . $item["img"] . "' alt='" . $item["title"] . "' />" . "<p>View Details</p>" . "</a></li>"; return $output;

}

function array_category($catalog, $category) { $output = array(); return $output; }

foreach ($catalog as $id => $item) {
    if (strtolower($category) == strtolower($item["category"])) {
        $output[] = $id;            
    }
}
if ($category === null) {
    return array_keys($catalog);
}

?>

catalog.php

<?php

include("inc/data.php"); include("inc/function.php");

$pageTitle = "Full Catalog"; $section = null;

if (isset ($_GET["cat"])) {
    if($_GET["cat"] == "books"){
        $pageTitle = "Books";
        $section = "books";
    } else if ($_GET["cat"] == "movies") {
        $pageTitle = "Movies";
        $section = "movies";
    } else if ($_GET["cat"] == "music") {
        $pageTitle = "Music";
        $section = "music";
    }
}

include("inc/header.php"); ?>

<div class="section catalog page">
    <div class="wrapper">

        <h1><?php echo $pageTitle;  ?></h1>

        <ul class="items">
            <?php 
            // Generates object array based on category using array_catagory and foreach.
            $categories = array_category($catalog,$section);
            foreach ($categories as $id) {
                echo get_item_html($id,$catalog[$id]);
            }
            ?>
        </ul>
    </div>
</div>

<?php include("inc/footer.php"); ?>

Your functions.php file is incorrect. It should be:

<?php

function get_item_html($id, $item){
            $output = "<li><a href='#'><img src='" 
            . $item["img"]. 
            "' alt='"
            . $item["title"] . "
            ' />"
            . "<p>View Details</p>"
            . "</a></li>";
            return $output;
        }




function array_categories($catalog,$category){
    if ($category == null){
       return array_keys($catalog);
    }
    $output = array();

    foreach ($catalog as $id => $item){
    if (strtolower($category) == strtolower($item["category"])){
        $output[] = $id;
    }
    }

    return $output;
}