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 Item Details and Redirection

James Mockridge
James Mockridge
4,043 Points

Blank page on details.php

Not sure where I went wrong. I would be grateful for any pointers.

<?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;
  }

$page_title = $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>
    </div>
  </div>

Hi James, your code seems to be good. What happen in your browser when type in the URL : http://port-80-txcdzassgc.treehouse-app.com/details.php?id=102

Does it shows you the img and the title of the $item correctly ?

Also, if you type in the URL : http://port-80-txcdzassgc.treehouse-app.com/details.php

As there is no "id" found thanks to the variable $_GET in the URL, your code should automatically redirect to the catalog.php page. If it doesn't, then you have a problem of redirection.

Here is my code that works fine for details.php :

<?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="breadcrumbs">
      <a href="catalog.php">Full Catalog</a>
      &gt; <a href="catalog.php?cat=<?php echo strtolower($item["category"]); ?>"><?php echo $item["category"];?></a>
      &gt; <?php echo $item["title"]; ?>

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

    <div class="media-details">
      <h1> <?= $item["title"]; ?></h1>
      <table>
        <tr>
          <th>Category</th>
          <td><?= $item["category"];?></td>
        </tr>
        <tr>
          <th>Genre</th>
          <td><?= $item["genre"];?></td>
        </tr>
        <tr>
          <th>Format</th>
          <td><?= $item["format"];?></td>
        </tr>
        <tr>
          <th>Year</th>
          <td><?= $item["year"];?></td>
        </tr>

        <?php
        if(strtolower($item["category"]) == "books") { ?>
        <tr>
          <th>Authors</th>
          <!-- implode rassemble les รฉlรฉments d'un tableau en une chaรฎne de caractรจre. -->
          <td><?php echo implode(", ",$item["authors"]); ?></td>
        </tr>
        <tr>
          <th>Publisher</th>
          <td><?php echo $item["publisher"]; ?></td>
        </tr>
        <tr>
          <th>ISBN</th>
          <td><?php echo $item["isbn"]; ?></td>
        </tr>
        <?php } else if(strtolower($item["category"]) == "movies") { ?> 
        <tr>
          <th>Director</th>
          <td><?php echo $item["director"]; ?></td>
        </tr>
        <tr>
          <th>Writers</th>
          <td><?php echo implode(", ",$item["writers"]); ?></td>
        </tr>
        <tr>
          <th>Stars</th>
          <td><?php echo implode(", ",$item["stars"]); ?></td>
        </tr>
        <?php } else if(strtolower($item["category"]) == "music") { ?>
        <tr>
          <th>Artist</th>
          <td><?php echo $item["artist"]; ?></td>
        </tr>
        <?php } ?>
      </table>
    </div>
  </div>
</div>        

Hope it will help you ! Have a great day !

James Mockridge
James Mockridge
4,043 Points

Hello Romuald,

I did enter the category id (id =101) into the address bar and it came up as blank white page. Without the id I get a redirect to catalog.php.

I will try out your code and see if it is a problem on details.php. If I can rule that out I can be sure the problem is coming from one of the include files.

Thank you for responding and for your code.

Edit: Your code worked which means that the problem is on my version of details.php rather than one of the include file.

3 Answers

In this part of your code:

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

Try putting the $id = $_GET("id"); into square brackets - $id = $_GET["id"];

Should work!

That's it Matthew ! That was the mistake between his code and mine. :)

James Mockridge
James Mockridge
4,043 Points

Oh bugger you beat me to it

Ha!!!

I guarantee you 90% of any issues will be forgetting the $ before a variable, forgetting a semi-colon or mixing up brackets. Fun stuff, right? :)

Thank you!

Cool ! I'm glad my answer helped you man :) Yeah maybe you missed a semi colon or a curly brace... Anyway great news if it works now ;)

Make sure to up-vote the answer for people who might have in the future the same problem as yours ;)

James Mockridge
James Mockridge
4,043 Points

after a side by side comparison I found the issue. I used brackets () rather than square brackets [].

this
$id = $_GET("id");

should be
$id = $_GET["id"];

The issue is resolved. Check your brackets everyone.

By the way if you copied my code in yours James, be careful with your variable $page_title as mine is called $pageTitle. So you might want to change it to your variable for your global code environment to work properly :)