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

Tricia Cuninghame
Tricia Cuninghame
2,936 Points

foreach loop is skipping first row

My code is designed to pull data from a database and display it in a dropdown menu.

this is my function:

function format_array($category = null){
  $category = strtolower($category);
  include("connection.php");

  try{
    $sql = "SELECT format, category
            FROM media
            GROUP BY format";
    if (!empty($category)){
      $results = $db->prepare($sql
          . " WHERE LOWER(category) = ?"
          . " ORDER BY category");
      $results -> bindParam(1,$category,PDO::PARAM_STR);
  } else {
      $results = $db->prepare($sql . " ORDER BY category");
    }
    $results -> execute();
  } catch (Exception $e) {
    echo "bad query";
  }
  $format = array();
  while ($row = $results -> fetch(PDO::FETCH_ASSOC)){
    $formats[$row["category"]][] = $row ["format"];
  }
  return $formats;

}

and my code on the form page:

$format_array = format_array();
            foreach ($format_array as $category =>$options) {
              echo"<optgroup label=\"$category\"";
              foreach ($options as $option){
                  echo "<option value=\"$option\"";
                  if (isset($format) && $format=="$option") {
                    echo " selected";
                  }
                  echo ">$option</option>";
                }
                echo "</optgroup>";
              }
              ?>
                      </select>
                  </td>
              </tr>

It is not displaying the first option in each category.

i.e. on my database there is 5 options under the books category - but only 4 show in the drop down. The same applies to the other categories as well.

Can anyone see where I have gone wrong?

2 Answers

Tricia Cuninghame
Tricia Cuninghame
2,936 Points

Thank you for the reply - it was actually very simple - I was missing a closing > !!

I have clearly been staring at this for far too long!

echo"<optgroup label=\"$category\"";

should have been

echo"<optgroup label=\"$category\">";
Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Not sure if it is this part, but I'm wondering why it start out as $format and then changes to $formats?

<?php
$format = array(); // $format here - looks like this is not being used?
while ($row = $results -> fetch(PDO::FETCH_ASSOC)){
  $formats[$row["category"]][] = $row ["format"]; // $formats here
}
return $formats; // $formats here again