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

I don't know how to fix this code from PHP.

When I see preview of this code, each of item get error message below.

Notice: Undefined index: media_id in /home/treehouse/workspace/inc/functions.php on line 109

However, I can't understand how to fix it, so if someone can solve this problem, please let me know.

★Here is the code of "function.php".

<?php function get_catalog_count($category = nulll){ $category = strlower($category); include("connection.php");

try{ $sql = "SELECT COUNT(media_id) FROM Media"; if(!empty($category)){ $result = $db -> prepare( $sql . " WHERE LOWER(category) = ?" ); $result -> bindParam(1,$category,PDO::PARAM_STR); }else{ $result = $db -> prepare($sql); } $result -> execute(); } catch (Exceotion $e){ echo "bad query"; } $count = $result -> fetchColumn(0);

}

function full_catalog_array(){ include("connection.php");

try{
  $result = $db -> query(
    "SELECT title, category, img 
     FROM Media
     ");

}catch(Exception $e){
   echo "Unable to retrived results";
    exit;
}

$catalog = $result -> fetchAll(PDO::FETCH_ASSOC);
return $catalog;

}

function random_catalog_array() { include("connection.php");

try {
   $results = $db->query(
     "SELECT title, category,img 
     FROM Media
     ORDER BY RANDOM()
     LIMIT 4"
   );
} catch (Exception $e) {
   echo "Unable to retrieved results";
    echo $e -> getMessage();
   exit;
}

$catalog = $results->fetchAll();
return $catalog;

}

function single_item_array($id){ include("connection.php");

try{
  $results = $db -> prepare("SELECT title, category, img, format, year, genre, publisher, isbn
                          FROM Media 
                          JOIN Genres ON Media.genre_id = Genres.genre_id
                          LEFT OUTER JOIN Books ON Media.media_id = Books.media_id
                          WHERE Media.media_id = ?"
                        );
   $results -> bindParam(1,$id,PDO::PARAM_INT);
   $results -> execute();
  }catch(Exception $e){
   echo "Unable to retrived results";
   echo $e -> getMessage();
    exit;
}


$item = $results -> fetch();
if(empty($item)) return $item;
   try{
  $results = $db -> prepare("SELECT fullname, role
                            FROM Media_People
                            JOIN People ON Media.People.people_id = People.people_id
                            WHERE Media_People.media_id = ?"
                        );
    $result -> bindParam(1,$id,PDO::PARAM_INT);
    $result -> execute();
  }catch(Exception $e){
   echo "Unable to retrived results";
      echo $e -> getMessage();
    exit;
}
while($row = $results->fetch(PDO::FETCH_ASSOC)){
    $item[$row["role"]][] = $row["fullname"];
}
return $item;

}

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

}

★And here is the code of "detail.php"

<?php

include("inc/functions.php");

if (isset($_GET["id"])) { $id = filter_input(INPUT_GET, "id", FILTER_SANITIZE_NUMBER_INT); $item = single_item_array($id); // $var_dump($item); if (isset($catalog[$id])) { $item = $catalog[$id]; } }

if (empty($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="<?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["author"]); ?></td>
            </tr>
            <tr>
                <th>Publisher</th>
                <td><?php echo implode(", ",$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 implode(", ",$item["director"]); ?></td>
            </tr>
            <tr>
                <th>Airtist</th>
                <td><?php echo implode(", ",$item["airtist"]); ?></td>
            </tr>
            <tr>

            <tr>
                <th>Writers</th>
                <td><?php echo implode(", ",$item["writer"]); ?></td>
            </tr>
            <tr>
                <th>Stars</th>
                <td><?php echo implode(", ",$item["star"]); ?></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>

1 Answer

Justin Radcliffe
Justin Radcliffe
18,987 Points

Try downloading the project files zip folder and comparing side by side your functions.php file with the 'master' functions.php file. I've used this option as a last resort and most of the time this will identify the error.