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

Filipe Pacheco
seal-mask
.a{fill-rule:evenodd;}techdegree
Filipe Pacheco
Full Stack JavaScript Techdegree Student 21,416 Points

Playing with the code

Hey guys, I was following Alena's instruction, but then thought of displaying the item details in a different way. I don't know if it would be the best thing to do, but I wanted to practice and see if it worked. I want to share it with you and get some feedback.

In this part, Alena uses the following code to display the item details

<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["authors"]); ?></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>

What if instead of writing the keys like Category, Genre, Format, Year, etc.. I used a foreach loop to get the keys from the catalog array and display them there? Like this:

<table>
        <?php foreach($catalog[$id] as $item => $value){
                  echo "<tr>
                          <th>". ucfirst($item) . "</th>
                          <td>". $value . "</td>
                        </tr>";  


        }?>
</table>

It kind of worked, apart for some problems:

1) I got a notice because there is an array, so I gotta somehow implode the arrays inside.

2) I gotta get rid of the 'title' and 'img' key and values.

So I tried the following, and it worked.

In functions.php:

<?php
function get_item_detail($item, $value){
    if($item == 'title' | $item == 'img'){
      $output = null;
    }elseif(is_array($value)){
      $output = "<tr>
                    <th>". ucfirst($item) . "</th>
                    <td>". implode(", ",$value) . "</td>
                 </tr>";
    }else{
      $output = "<tr> 
                    <th>". ucfirst($item) . "</th>
                    <td>". $value . "</td>
                 </tr>";
    }
    return $output;                  
}
?>

In details.php

     <table>
        <?php 
              foreach($catalog[$id] as $item => $value){
                  echo get_item_detail($item, $value);
              }
       ?>
     </table>

I thinks the only thing different is the order of the elements, but if it was necessary to sort it in an specific way, I could change it directly in the catalog.php. What are your thoughts on this?

1 Answer

René Sánchez
René Sánchez
9,954 Points

This is really awesome Filipe!! That is a pretty neat way to code it! Congratulations bro, great thought process (Y)