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 Sorting Array Items

Min Chin
Min Chin
401 Points

Sorting arrays wont show up

when i click on the category link, the sorted items wont show up and I just get a blank page. Below is catalog.php

        <?php 
include("inc/data.php");
include("inc.functions.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 
        $categories = array_category($catalog,$section);
        foreach ($categories as $id) {
            echo get_item_html($id, $catalog[$id]);
        }               
        ?>
        </ul>
    </div>
</div>

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

below is my functions.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) {
    if ($category == null) {
        return array_keys($catalog);
    }
    $output = array();

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

        }
    }

return $output;

}

below is my index.php

<?php 
include("inc/data.php");
include("inc/functions.php");
$pageTitle = "Personal Media Library ";

include("inc/header.php"); ?>
        <div class="section catalog random">

            <div class="wrapper">

            <h2>May we suggest something?</h2>

            <ul class="items">
                <?php 
                $random = array_rand($catalog,4);
                foreach ($random as $id) {
                    echo get_item_html($id, $catalog[$id]);

            }
            ?>              
        </ul>

        </div>

        </div>

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

Hi Min,

It is one of the best examples of how a small typo can result in a big issue. :)

It took me a while to get it. In your catalog.php, 2nd line: you have put "." instead of "/" (in include).

Hope this will solve the problem!!

Cheers!!