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 Integrating PHP with Databases Using Relational Tables Adding a Secondary Query

Amit Kumar
Amit Kumar
1,063 Points

My genre dropdown is not showing .

<tr>
                <th>
                    <label for="genre">Genre</label>
                </th>
                <td>
                   <select name="genre" id="genre">
    <option value="">Select One</option>
        <?php
        $genre_arrayy = genre_array();
        foreach ($genre_arrayy as $category=>$options) {
          echo "<optgroup label=\"$category\">";
          foreach ($options as $option) {
            echo "<option value=\"$option\"";
            if (isset($genre) && $genre==$option) {
              echo " selected";
            }
            echo ">$option</option>";
          }
          echo "</optgroup>";
        }
        ?>
</select> 

                </td>
            </tr>
function genre_array($category = null) {
  $category = strtolower($category);
  include("connection.php");

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

Moderator edited: Markdown added so that the code will render properly in the forums.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Amit! I added some markdown to your question so that the code will be easier to read for other students and hopefully assist you in getting help faster. Please see the Markdown Cheatsheet link at the bottom of the "Add an Answer" section for tips on how to format your text/code in your posts! :sparkles:

I only glimpsed at your code without reading the entire code, but right away I noticed you spelled 'array' where you have genre_array. There should be only 1 'y'.

There are multiple areas where you spelled array with 2 y's, but you have it spelled correct in your function.