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

Ryann Green
Ryann Green
11,010 Points

Creating Unique List With PHP

I am currently trying to create a "list" of images using PHP.

 <div class="main-content">
    <ul>
<?php if($_GET["selector"] == $items['selector']) {
      foreach($items as $itemID => $item) { ?>
      <li><a href='item.php?selector=<?php echo $item['selector'] ?>?id=<?php echo $itemID ?>'><img src='<?php echo $item['imgunhover'];?>' alt='<?php $item['name'];?>' onmouseover="this.src='<?php echo $item['imghover'];?>';" onmouseout="this.src = '<?php echo $item['imgunhover'];?>';"/></a></li>
    <?php }} ?>
    </ul>
  </div> 

I have included my array.php at the head of the document. What I am trying to do is say that if the "selector=value" of the url is the same as the selector value of the $items array, then it should loop through only those items. However, whenever I try this, my header and footer appear, but none of the images do. Does anyone know what I'm doing wrong or what a better way to accomplish this would be?

2 Answers

Chris Malcolm
Chris Malcolm
2,909 Points

Hi ryan you are checking your main array for "selector" key..but you need to actually check within each item if it matches selector. swapping your foreach before the if statement instead of after..and check $item not $items. this will work.

<? php
foreach($items as $itemID => $item) { 
     if($_GET["selector"] == $item['selector']) {
      ?>
      <li><a href='item.php?selector=<?php echo $item['selector'] ?>?id=<?php echo $itemID ?>'><img src='<?php echo $item['imgunhover'];?>' alt='<?php $item['name'];?>' onmouseover="this.src='<?php echo $item['imghover'];?>';" onmouseout="this.src = '<?php echo $item['imgunhover'];?>';"/></a></li>
    <?php 
    }
 } 
?>
Ryann Green
Ryann Green
11,010 Points

Thank you so much! I figured I had to be close, but I should have realized that. You're a life saver. That worked perfectly. Thank youu!

Ryann Green
Ryann Green
11,010 Points

Here is my array:

<?php
$items[1] = array(
    "name" => "Red Couch",
    "imgunhover" => "img/testcolor.jpg",
    "imghover" => "img/testbw.jpg",
    "price" => 18, 
    "quantity" => 1,
    'dimensions' => '38" H x 18" D x 49" W',
    "description" => "Cras sit amet justo ac ipsum convallis porta id sed enim. Mauris blandit volutpat pharetra. Ut a massa et massa pulvinar accumsan. Integer et lobortis quam. Aenean aliquam aliquet dolor, nec fringilla urna porttitor sit amet. In hac habitasse platea dictumst. Phasellus sed ultricies justo, in dapibus ligula. Etiam facilisis elit vel auctor consequat. Pellentesque ut iaculis sem. Integer tincidunt sem ligula, sit amet mollis lorem aliquet vel.",
    "selector" => "couches"
);
$items[2] = array(
    "name" => "Another Item",
    "imgunhover" => "img/testcolor2.jpg",
    "imghover" => "img/testbw2.jpg",
    "price" => 18, 
    "quantity" => 1,
    'dimensions' => '38" H x 18" D x 49" W',
    "description" => "Cras sit amet justo ac ipsum convallis porta id sed enim. Mauris blandit volutpat pharetra. Ut a massa et massa pulvinar accumsan. Integer et lobortis quam. Aenean aliquam aliquet dolor, nec fringilla urna porttitor sit amet. In hac habitasse platea dictumst. Phasellus sed ultricies justo, in dapibus ligula. Etiam facilisis elit vel auctor consequat. Pellentesque ut iaculis sem. Integer tincidunt sem ligula, sit amet mollis lorem aliquet vel.",
    "selector" => "couches"
); ?>

I don't think it should be, since it is checking the array before going through the foreach loop, but I tried it anyway and it didn't seem to have any effect.