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

Siraj Khan
Siraj Khan
3,451 Points

The details page does not open in the browser.?

I get a blank browser page. I don't know whats wrong with my code. Please help..!

PHP

functions.php

<?php


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



function array_category($catalog, $category){

   $output = array();

  foreach ($catalog as $id => $item) {
      if ($category == null  OR strtolower($category) == strtolower($item["category"])) {
      $sort = $item["title"];
      $sort = ltrim($sort, "The ");
      $sort = ltrim($sort, "A ");
      $sort = ltrim($sort, "An ");
      $output[$id] = $sort;

    }
  }

  asort($output);

  return array_keys($output);
}




 ?>
PHP

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="media-picture">

      <span>
        <img src="<?php echo $item["img"]; ?>" alt="<?php echo $item["title"]; ?>">
     </span>

    </div>

    <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>


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



      <?php if (strtolower($item["category"]) == "books"]) { ?>

        <tr>
          <th>Authors</th>
          <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>
Anton Klyuchkovksiy
Anton Klyuchkovksiy
8,327 Points

Hi, Siraj Khan!

Can you send snapshot of your workspace? Top right hand corner of the workspace area - there's 3 little icons, one of them is a camera, for taking a snapshot.

1 Answer

There could be multiple things wrong which is hard to see without seeing all your php files but the one i can see straight away is an issue with your get_item_html function.

Problem

Although you point the link to the details page, you do not pass through the value as to which details the function should pull up. As you can see below in your anchor tag, you reference the details.php but do not point to any $id.

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

Solution

As you can see here within the anchor tag, after the details.php we pass in a value of $id to point to the item within the array

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

In my opinion php is such an ugly language, structuring the php like it is structured in html helps me debug these issues, Good Luck!