Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,415 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,942 Points

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