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 Enhancing a Simple PHP Application Paginating a List: Model and View Using Partials

Wayne Wilkinson
Wayne Wilkinson
14,112 Points

Using Partials causes error

I moved the code generated by function get_list_view_html(), and put it in a partial as recomended (partial_product_list_view.html.php). The main page and shirts.php use to display 4 shirts per line. Now only 3 are displayed per line. If I shitch back to using the function, 4 per line are displaed again. I downloaded the file for the project and took a look at the partial, and I could see no difference from that file and the one I created. So, I am not sure what the differences are in the methods of display. Is there anything that you can think of that would cause this behavior?

I did this track about a month ago, and I vaguely remember having a similar problem. Make sure that you don't have extra spaces around the include call and that you're classes are specified.

Mine looked like this:

<div class="section shirts page">
  <div class="wrapper">
    <h1>Mike&rsquo;s Full Catalog of Shirts</h1>
    <?php include(ROOT_PATH . "inc/partial-list-navigation.html.php"); ?>
    <ul class="products">
      <?php
        foreach($products as $product) {
            include(ROOT_PATH . "inc/partial-product-list-view.html.php");
        }
      ?>
    </ul>
    <?php include(ROOT_PATH . "inc/partial-list-navigation.html.php"); ?>
  </div>
</div>

And the include file looked like this:

<li>
  <a href="<?php echo BASE_URL; ?>shirts/<?php echo $product["sku"]; ?>/">
    <img src="<?php echo BASE_URL . $product["img"]; ?>" alt="<?php echo $product["name"]; ?>">
    <p>View Details</p>
  </a>
</li>

(Edited for clarity)

5 Answers

Wayne Wilkinson
Wayne Wilkinson
14,112 Points

Thanks to everyone for you quick responses. I finally found the culprit. It happen to be an extra cariage return at the very end of the partial (just after the </li>). All other whitespace in the partial (partial_product_list_view.html.php) was dealt with by the browser as expected, and 4 items per line were displayed.

Nicole Anderson
Nicole Anderson
8,558 Points

I was having the same problem, and I think it's related to how php handles whitespace, mentioned in one of the sections of Build a Simple PHP Application. I added an opening php tag to the end of the html partial file, and it solved the issue. I left out the closing tag because it can cause issues with php - see this Stackoverflow thread and the php manual.

I'm wondering if this is an issue for some people and not others because of differences in text editors. I'm using Atom, and it keeps adding a newline to the end of the document when I save.

I've only ever found white space an issue when dealing with Header redirects, partials are a little more robust.

But you shouldn't have to add an opening php tag at the end of any file to make it work. If this is the case, something is wrong!

If your text editor is doing something by default that's breaking your code.. it's not a very good text editor!

I think Wayne Wilkinson needs to post some code

Nicole Anderson
Nicole Anderson
8,558 Points

The whitespace issue was brought up in Build a Simple PHP Application - Understanding Whitespace. It's related to the styling used for the list items.

I wouldn't say that my text editor is "breaking" any code, but I think it may be the source of the problem, and the reason why not everyone is experiencing it. I found an interesting Stackoverflow thread on end-of-file newlines, and it seems to be a Unix convention.

I'm open to solutions other than adding an opening php tag, but I believe whitespace is the issue, at least in my case and possibly for Wayne as well.

Have you tried dumping out the returned array to make sure you're always getting back 4 results?

That's the first thing I would check!

Just had the same problem. The answers helped me too :-). Thanks

Daniel Jenkins
Daniel Jenkins
17,714 Points

Bit late to the party but I encountered this problem recently too. I resolved it by setting out my 'foreach' loop like so

foreach($products as $product) {include(ROOT_PATH . "inc/partial-product-list-view.html.php");}

An issue with whitespace perhaps, but seemingly inside the 'foreach' loop rather than in the partial