Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

mike cee
mike cee
7,302 Points

undefined variable $item lil stuck please help

getting many errors on details.php "undefined variable $item"

<?php include("inc/data.php"); include("inc/functions.php");

if (isset($_GET["id"])) { $id = $_GET["id"]; if (isset($catalog[$id])) { $item = $catalog[$id]; }

} if (isset($item)) { header("location: catalog.php"); exit; }

   $pageTitle = $item['title'];
    $section = null;

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

<div class = "section page">

    <div class = "wrapper" >

        <div class = "media-picture">
                <span>
                    <img src="<?php echo $item["img"]; ?>" alt="<?php echo $item["title"]; ?>" />
                 </span>
        </div> <!-- media-picture-->

        <div class = "media-details" >
            <h1><?php echo $item["title"]; ?></h1>

            <table>     
                    <tr>
                        <th>Category</th>
                        <td><?php echo $item["category"]; ?></td>
                    </tr>
                    <tr>
                        <th>Genre</th>
                        <td><?php echo $item["genre"]; ?></td>
                    </tr>

                    <tr>
                        <th>Format</th>
                        <td><?php echo $item["format"]; ?></td>
                    </tr>
                        <th>Year</th>
                        <td><?php echo $item["year"]; ?></td>

                    </tr>
                                <?php 
                                        if (strtolower($item["category"]) == "books") {
                                            ?>
                                            <tr>
                        <th>Authors</th>
                        <td><?php echo $item["authors"]; ?></td>
                    </tr>
                    <tr>
                        <th>Publishers</th>
                        <td><?php echo $item["publisher"]; ?></td>
                    </tr>
                            <tr>
                        <th>ISBN</th>
                        <td><?php echo $item["isbn"]; ?></td>
                    </tr>            
                    <?php  }  ?>

            </table>


        </div> <!--/ media-details -->


    </div> <!--/ wrapper -->

</div> <!--/ section page -->

2 Answers

mike cee
mike cee
7,302 Points
<?php 
include("inc/data.php");
include("inc/functions.php");

if (isset($_GET["id"])) {
    $id = $_GET["id"];
    if (isset($catalog[$id])) {
        $item = $catalog[$id];
    }

}
    if (isset($item)) {
        header("location: catalog.php");
        exit; 
    }

       $pageTitle = $item['title'];
        $section = null;

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

    <div class = "section page">

        <div class = "wrapper" >

            <div class = "media-picture">
                    <span>
                        <img src="<?php echo $item["img"]; ?>" alt="<?php echo $item["title"]; ?>" />
                     </span>
            </div> <!-- media-picture-->

            <div class = "media-details" >
                <h1><?php echo $item["title"]; ?></h1>

                <table>     
                        <tr>
                            <th>Category</th>
                            <td><?php echo $item["category"]; ?></td>
                        </tr>
                        <tr>
                            <th>Genre</th>
                            <td><?php echo $item["genre"]; ?></td>
                        </tr>

                        <tr>
                            <th>Format</th>
                            <td><?php echo $item["format"]; ?></td>
                        </tr>
                            <th>Year</th>
                            <td><?php echo $item["year"]; ?></td>

                        </tr>
                                    <?php 
                                            if (strtolower($item["category"]) == "books") {
                                                ?>
                                                <tr>
                            <th>Authors</th>
                            <td><?php echo $item["authors"]; ?></td>
                        </tr>
                        <tr>
                            <th>Publishers</th>
                            <td><?php echo $item["publisher"]; ?></td>
                        </tr>
                                <tr>
                            <th>ISBN</th>
                            <td><?php echo $item["isbn"]; ?></td>
                        </tr>            
                        <?php  }  ?>

                </table>


            </div> <!--/ media-details -->


        </div> <!--/ wrapper -->



</div> <!--/ section page -->
Aurelien Roux
Aurelien Roux
25,567 Points

You have an error on line 12 where you are missing an exclamation mark

    if (isset($item)) {
        header("location: catalog.php");
        exit; 
    }

should be

    if (!isset($item)) {
        header("location: catalog.php");
        exit; 
    }

your code is actually saying "if an item is specified (like id=101 for example), redirect to catalog.php" when in fact, you want the opposite as in "if an item is NOT specified, redirect to catalog.php". so if an item is specified, the redirect order will be skipped and the following code will apply.

hope that helps