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
nicholas maddren
12,793 PointsCreating a product page
Hey guys, I have a MySQL database which has a table that contains product information, each row is equivalent to one product.
I have developed a listing page using PDO with the following code:
<?php while($row = $results->fetch(PDO::FETCH_ASSOC))
{
echo '
<div class="listing-container">
<h3 class="model-listing-title clearfix">'.$row["Make"].' '.$row["Model"].' '.$row["Variant"].'</h3>
<h3 class="price-listing">Ā£'.number_format($row['Price']).'</h3>
</div>
<div class="listing-container-spec">
<img src="'.(explode(',', $row["PictureRefs"])[0]).'" class="stock-img-finder"/>
<div class="ul-listing-container">
<ul class="overwrite-btstrp-ul">
<li class="diesel-svg list-svg">'.$row["FuelType"].'</li>
<li class="saloon-svg list-svg">'.$row["Bodytype"].'</li>
<li class="gear-svg list-svg">'.$row["Transmission"].'</li>
<li class="color-svg list-svg">'.$row["Colour"].'</li>
</ul>
</div>
<ul class="overwrite-btstrp-ul other-specs-ul h4-style">
<li>Mileage: '.number_format($row["Mileage"]).'</li>
<li>Engine size: '.$row["EngineSize"].'cc</li>
</ul>
<button href="#" class="btn h4-style checked-btn hover-listing-btn"><span class="glyphicon glyphicon-ok"></span> History checked
</button>
<button href="#" class="btn h4-style more-details-btn hover-listing-btn tst-mre-btn"><span class="glyphicon glyphicon-list"></span> More details
</button>
<button href="#" class="btn h4-style test-drive-btn hover-listing-btn tst-mre-btn"><span class="test-drive-glyph"></span> Test drive
</button>
<h4 class="h4-style listing-photos-count"><span class="glyphicon glyphicon-camera"></span> 5 More photos</h4>
</div>
';
} ?>
So basically that code loops each row of the SQL table into a list on the page.
What I need to do now is give each one of these SQL rows a page that is dedicated to each row in the SQL table.
I also need to add a href to the code listed above that will link each loop to the right page for that product.
If your still confused to what I am trying to accomplish then the best example is this:
Here is the listing page on eBay, my layout is very similar and basically does the same thing however I have already developed this part. http://www.ebay.co.uk/sch/i.html?_odkw=anything&_from=R40&_osacat=11233&_from=R40&_trksid=p2045573.m570.l1313.TR12.TRC2.A0.H0.Xdeadmau5&_nkw=deadmau5&_sacat=11233
Here is what I am trying to develop. http://www.ebay.co.uk/itm/deadmau5-4x4-12-NEW-CD-ALBUM-/351119594618?pt=UK_CDsDVDs_CDs_CDs_GL&hash=item51c05bd47a
As you can see it is a product page, I am trying to do exactly the same thing. I am trying to give each row a page dedicated to it and be able to link the right product pages to the right loop on the listing page
Any idea how I can do this?
1 Answer
Aaron Graham
18,033 PointsI'm not sure how your site is laid out so far, but this is how this would usually be done. First, you need a url that maps to your product listings page (essentially the code you have above). This would be something like example.com/products. Next, each item in your database needs a unique ID. I'm not sure what database you are using, but with MySQL, this can be accomplished by defining your table with a 'NOT NULL AUTO_INCREMENT' attribute on an 'id' key. Next, you need to have a way to query your database through the url. If you are using some type of routing or url rewriting, you could set up the url like this: example.com/products/id/1234567, where 1234567 is the unique id of the item in your database. If you aren't using routing, you could query it like this: example.com/products?id=1234567. This url would execute something like the following on your database: SELECT * FROM products WHERE id=1234567;. Take the result of the query and display it on your page. All that would be left would be adding links in your product listing page. That should be trivial with the unique id key in your database entries. Something like '<a href="/products/id/' . $row[id]. '">link name</a>'. Hope that gives you some ideas.