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 Creating the Display Function

Nothing is displaying for full catalog?

On https://teamtreehouse.com/library/build-a-basic-php-website/listing-and-sorting-inventory-items/creating-the-display-function where the functions.php is separated.

//functions.php in inc folder

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

Then on catalog.php shows a foreach loop calling the function with parameters.

//top section of the HTML document
include("inc/data.php");
include("inc/functions.php");
.
.
.
<div class="section catalog page">
  <div class="wrapper">
    <h1><?php echo $pageTitle; ?></h1>
    <ul class="items">
      <?php 
      foreach ($catalog as $id => $item) {
        echo get_item_html($id,$item);
      }
      ?>
    </ul>
  </div>
</div>

Then replacing the index.php unordered list elements using the above loop

// the same as in catalog.php
      foreach ($catalog as $id => $item) {
        echo get_item_html($id,$item);
      }
      ?>

The output shows "blank".

If I "echo" on catalog.com, I see the full catalog. But when I created a function just like what is shown on the video instruction, I'm seeing blank on the browser.

//original catalog.php unodered list items

      <?php 
      foreach ($catalog as $item) {
        echo "<li><a href='#'><img src='"
          . $item["img"] ."' alt='"
          . $item["title"] ."'>"
          . "<p>View Details</p>" 
          ."</a></li>";
      }
      ?>

Any ideas?

2 Answers

Hazem mosaad
Hazem mosaad
15,384 Points

HI John where is the return of the function Without the return the function not do anything !

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

Thanks! I felt dumb.

Hazem mosaad
Hazem mosaad
15,384 Points

No No You Must write it in a paper and do more Exercise Never Give Up Continue Learning ,,You Are Welcome

Thanks Hazem!