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!

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 Enhancing a Simple PHP Application Paginating a List: Model and View Adding HTML Markup

Brenda Krafft
Brenda Krafft
18,036 Points

Shirt images only display when the page first opens.

When I open the Shirts page for the first time, the t-shirt images display just fine.

But if I click on any of the pagination numbers, I get that "broken image" symbol and the name of the t-shirt instead.

Even if I click back to page 1, which just displayed fine, this happens.

But if I click back to the Home page, and return to the Shirts page, again the images show just fine. Then I lose them again on pagination.

I know that I'm getting the right data back, as the t-shirt names change for every page, so this must be a display issue.

Any ideas on why this might be happening?

Well, here is the call from the Shirts page. This is the only call to render the shirts images, regardless of whether I'm just opening the page or not. The $products array changes, but I've verified that the information in there is correct:

foreach($products as $product) { 
                        echo get_list_view_html($product);

Here is the get_list_view_html() function:

* This function takes an array of one shirt's info and pulls out "sku", "img", and "name" only.
* It also encapsulates that information into formatted HTML, so it is ready to be displayed.
* @param:  $product        array()       array of t-shirt information
* return:  $output         string        HTML-formatted list of t-shirt info to display
function get_list_view_html($product) {
    $output = "";
    //build an HTML output
    //Originally had to 'echo' out the HTML to get rid of extra white space, so on separate lines
    $output = $output . "<li>";
    $output = $output . '<a href="shirt.php?id=' . $product["sku"] . '">'; //can also use \" but more readable with '' -->
    //Original code<img src="<?php echo $product["img"]; php end here>" alt="<?php echo $product["name"]; php end here>"> -->
    $output = $output . '<img src="' . $product["img"] . '" alt="' . $product["name"] . '">';
    $output = $output . "<p>View Details</p>";
    $output = $output . "</a>";
    $output = $output . "</li>";

    return $output;


So the code is exactly the same for displaying the images.

You need to post your code or a fork snapshot. You can create a snapshot using the camera icon in the upper right corner.

It sounds like there is an issue with the link to later pages and how the images are displayed. Maybe the path is wrong in the code processing the image links has the wrong base link.