PHP Build a Basic PHP Website Listing and Sorting Inventory Items Displaying All Items

Malcolm Stretten
Malcolm Stretten
3,814 Points

This tutorial is a nightmare. Can ANYONE see what is wrong with my catalog.php

Here is my catalog.php...I've checked and checked and it seems identical to the trainer's.

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

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

if (isset($_GET["cat"])){ if ($_GET["cat"] == "books"){ $pageTitle = "Books"; $section = "books"; } elseif ($_GET["cat"] == "movies") { $pageTitle = "Movies"; $section = "movies"; } elseif ($_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  
  foreach($catalog as $item) {
      echo "<li><a href='#'><img src= '"
          .$item["img"] . "' alt='"
          .$item["title"] . "' />"
          . "<p>View Details</p>"
          . "</a></li>";
  }

  ?>

</ul>

</div>

</div>

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

10 Answers

Steven Parker
Steven Parker
170,451 Points

:point_right: Two things stand out:

  • $GET instead of $_GET (in 2 places)
  • "elseif" instead of "else if" (also in 2 places)

:information_source: Instructions for formatting code can be found in the Markdown Cheatsheet below. :arrow_heading_down:

Malcolm Stretten
Malcolm Stretten
3,814 Points

Steven

Thank you for your suggestions but my code in fact has $_GET in all places. For some mysterious reason after I pasted it in it does ideed show in two places as $GET - I don't know why! Also, I am pretty sure that 'elseif' and 'else if' are synonymous.

Let me just paste my code again here below;

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

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

if (isset($_GET["cat"])){ if ($_GET["cat"] == "books"){ $pageTitle = "Books"; $section = "books"; } elseif ($_GET["cat"] == "movies") { $pageTitle = "Movies"; $section = "movies"; } elseif ($_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  
  foreach ($catalog as $item) {
      echo "<li><a href='#'><img src= '"
          . $item["img"] . "' alt='"
          . $item["title"] . "' />"
          . "<p>View Details</p>"
          . "</a></li>";
  }

  ?>

</ul>

</div>

</div>

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

Malcolm Stretten
Malcolm Stretten
3,814 Points

Steven

I just checked and it's just done it again! Must be some bug on Treehouse - I don't know. Believe me, all of them are $_GET - unless if changes this one!

Malcolm Stretten
Malcolm Stretten
3,814 Points

I've TRIED to contact Treehouse Support but all they seem to do is thank me for reaching out. I don't think they even know what's gone wrong here. I don't know if other people have the problem that if you actually email them direct from your email client they never seem to respond - only if you mail them from the web.

Malcolm Stretten
Malcolm Stretten
3,814 Points

Hello

This is a reply to Alex Watts. Thanks so much for the response but that code did not work either and caused the same error report. And I notice it's very different from Ann Hooligan's code. This suggests there may be a serious fault in the video. I STILL have had no reply from Treehouse apart from thanking me for reaching out.

To confirm, I am pasting the code you sent me below:

<ul class="items"> <?php foreach($catalog as $item) { echo "<li><a href='#'><img src='" . $item["img"] . "' alt='" . $item["img"] . "'>" . $item . "</a></li>"; } ?> </ul>

I followed along with this video and didn't run into any problems.

Can you tell us what is happening when you view your site? Are there any errors appearing?

Malcolm Stretten
Malcolm Stretten
3,814 Points

Instead of the images appearing, I think only one appeared and then there was a lot of error messages.

The point is that I am SURE that my code is identical to the trainer's in every way. Can you see anything at all wrong in my code?

caroline
caroline
6,311 Points

Hi Malcom

I'm a PHP newbie too so not sure if I can help, but I just wanted to let you know that I copied and pasted your code in its entirety from your first post, and then again from your second post, into my catalog.php page and it works fine on my site. Could the error be in your data.php file (or one of the other include files)?

Maybe if you post the error messages you're getting that might help someone to help you also.

Caroline

Malcolm Stretten
Malcolm Stretten
3,814 Points

Thank you, Caroline. I will check.

peter anderson
peter anderson
2,775 Points

I had the same error ( only some images displaying ). Check your data.php file and make sure that the content matches the data from the teachers data.txt file. In my case I had the following old code: $catalog[201] = "Forrest Gump"; $catalog[301] = "Beethoven"; $catalog[102] = "Clean Code";

Removing the old code made it work.