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

mike cee
mike cee
7,302 Points

Displaying Item Details

i can't access the item details page after i type details.php?id=101. i get redirected to the full catalog. please help

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

Julio Andrade
PLUS
Julio Andrade
Courses Plus Student 10,896 Points

In the span img tag where you have $item["img"] and $item["title"] you have double quotes inside double quotes. Try this instead $item['img'] and $item['title']

John Tampi
John Tampi
1,025 Points

Whats the difference between double quote and single one? I believe she mentioned about it earlier but I forgot

I am having this same issue, I've watched and re-watched this video and can not spot the error.

When clicking on an item from the catalogue I am just re-directed to the catalogue. (.../catalog.php)

<?php

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

// Determine page title and catalog category using $_GET.
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;

// Import page header template.
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['categories']) == "books") {
          ?>
         <tr>
            <th>Authors</th>
            <td><?php echo $item['authors']; ?></td>
         </tr>
         <tr>
            <th>Publishers</th>
            <td><?php echo $item['publishers']; ?></td>
         </tr>
         <tr>
            <th>ISBN</th>
            <td><?php echo $item['isbn']; ?></td>
         </tr>
        <?php } ?>
      </table>
    </div>
  </div>
?>

It's also worth noting that I have updated my functions.php to reflect the changes listed in the Teachers Notes section, like so:

<?php 
function get_item_html($id,$item) {
    $output = "<li><a href='details.php?id="
        . $id . "'><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); 
}