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

Has anyone tried to replace the html markup of three select drop downs by looping through an array?

Just watched the "Grouping Options for More Detail" video on the Build a Basic PHP Website course.

Alena suggests creating an array which can be looped over to make the html for the three select dropdowns.

I am new to programming and really want to solve this challenge but finding it very difficult. I'm hoping with lots of practice I will eventually be able to grasp the login and code PHP..

So far I have put the data in an array. But stuck on looping through to get the relevant data.

Any tips to head me in the correct direction would be great :)

$categories = [];

$categories["Books"] = [
    "Format" => [
        "Audio",
        "Ebook",
        "Hardback",
        "Paperback",
    ],
   "Genre" => [
        "Action",
        "Adventure",
        "Comedy",
        "Fantasy",
        "Historical",
        "Historical Fiction",
        "Horror",
        "Magical Realism",
        "Mystery",
        "Paranoid",
        "Philosophical",
        "Political",
        "Romance",
        "Saga",
        "Satire",
        "Sci-Fi",
        "Tech",
        "Thriller",
        "Urban",
    ],
];

$categories["Movies"] = [
  "Format" => [
  "Blu-ray",
  "DVD",
  "Streaming",
  "VHS",
],

"Genre" => [
    "Action",
    "Adventure",
    "Animation",
    "Biography",
    "Comedy",
    "Crime",
    "Documentary",
    "Drama",
    "Family",
    "Fantasy",
    "Film-Noir",
    "History",
    "Horror",
    "Musical",
    "Mystery",
    "Romance",
    "Sci-Fi",
    "Sport",
    "Thriller",
    "War",
    "Western",
],
];

$categories["Music"] = [

    "Format" => [
    "Cassette",
    "CD",
    "MP3",
    "Vinyl",
 ],

 "Genre" => [
    "Alternative",
    "Blues",
    "Classical",
    "Country",
    "Dance",
    "Easy Listening",
    "Electronic",
    "Folk",
    "Hip Hop/Rap",
    "Inspirational/Gospel",
    "Jazz",
    "Latin",
    "New Age",
    "Opera",
    "Pop",
    "R&B/Soul",
    "Reggae",
    "Rock",
 ],
];

// Nested loops 
foreach($categories as $key => $val) {
    echo "<h1>" . $key . "</h1>";
    foreach($val as $type_key => $tval) {
        echo "<h2>" . $type_key . "</h2>";
            foreach($tval as $item_key => $item) {
                echo $item_key . ": " . $item . "<br />";
            }
            echo "<br/> \n";
    }
}

Ok, I've been playing around with this for a couple of days and have this as a working example. Would be good to see how anyone else has done this.

// Categories array
$categories = [];

$categories["Books"] = [
    "Format" => [
        "Audio",
        "Ebook",
        "Hardback",
        "Paperback",
    ],

    "Genre" => [
        "Action",
        "Adventure",
        "Comedy",
        "Fantasy",
        "Historical",
        "Historical Fiction",
        "Horror",
        "Magical Realism",
        "Mystery",
        "Paranoid",
        "Philosophical",
        "Political",
        "Romance",
        "Saga",
        "Satire",
        "Sci-Fi",
        "Tech",
        "Thriller",
        "Urban",
    ],
];

$categories["Movies"] = [
    "Format" => [
        "Blu-ray",
        "DVD",
        "Streaming",
        "VHS",
    ],

    "Genre" => [
        "Action",
        "Adventure",
        "Animation",
        "Biography",
        "Comedy",
        "Crime",
        "Documentary",
        "Drama",
        "Family",
        "Fantasy",
        "Film-Noir",
        "History",
        "Horror",
        "Musical",
        "Mystery",
        "Romance",
        "Sci-Fi",
        "Sport",
        "Thriller",
        "War",
        "Western",
    ],
];

$categories["Music"] = [
     "Format" => [
        "Cassette",
        "CD",
        "MP3",
        "Vinyl",
    ],

    "Genre" => [
        "Alternative",
        "Blues",
        "Classical",
        "Country",
        "Dance",
        "Easy Listening",
        "Electronic",
        "Folk",
        "Hip Hop/Rap",
        "Inspirational/Gospel",
        "Jazz",
        "Latin",
        "New Age",
        "Opera",
        "Pop",
        "R&B/Soul",
        "Reggae",
        "Rock",
    ],
];



<form method="post" action="test_suggest.php">
    <table>
      <tr>
         <th><label for="name">Name (required)</label></th>
         <td><input type="text" id="name" name="name" value="<?php if (isset($name)) echo $name; ?>"/></td>
      </tr>

      <tr>
         <th><label for="email">Email (required)</label></th>
         <td><input type="text" id="email" name="email" value="<?php if (isset($email)) echo $email; ?>" /></td>
      </tr>

      <tr>
         <th><label for="category">Category (required)</label></th>
         <td>
           <select id="category" name="category">
             <option value="">Select One</option>
              <?php 
              foreach($categories as $key => $val) { ?>
                <option value="<?php echo $key; ?>" <?php if (isset($category) && $category == "$key") echo " selected"; ?>><?php echo $key; ?></option>
                <?php 
              } ?>
           </select>
         </td>
      </tr>

      <tr>
         <th><label for="title">Title (required)</label></th>
         <td><input type="text" id="title" name="title" value="<?php if (isset($title)) echo $title; ?>"/></td>
      </tr>

      <tr>
         <th><label for="format">Format</label></th>
         <td>
           <select id="format" name="format">
             <option value="">Select One</option>
             <?php 
             foreach($categories as $key => $val) { ?>
                 <optgroup label="<?php echo $key; ?>"><?php 
                     foreach($val as $type_key => $tval) {
                         if($type_key == "Format") {
                             foreach($tval as $item_key => $item) { ?>
                                 <option value="<?php echo $item; ?>" 
                                      <?php if (isset($format) && $format == "$item") echo " selected"; ?>>
                                      <?php echo $item; ?>
                                 </option><?php 
                             }
                         }
                     } ?>
                 </optgroup><?php 
             } ?>
           </select>
         </td>
      </tr>

      <tr>
            <th>
                <label for="genre">Genre</label>
            </th>
            <td>
                <select name="genre" id="genre">
                    <option value="">Select One</option>
                    <?php 
     foreach($categories as $key => $val) { ?>
       <optgroup label="<?php echo $key; ?>">
         <?php 
         foreach($val as $type_key => $tval) {
            if($type_key == "Genre") { ?>
               <?php 
               foreach($tval as $item_key => $item) { ?>
                  <option value="<?php echo $item; ?>" 
                      <?php 
                       if (isset($format) && $format == "$item") echo " selected"; ?>>
                      <?php echo $item; ?>
                  </option><?php 
               }
            }
         } ?>
      </optgroup><?php 
     } ?>
                </select>
            </td>
        </tr>

      <tr>
         <th><label for="year">Year</label></th>
         <td><input type="text" id="year" name="year" value="<?php 
                       if (isset($year)) echo $year; ?>"/>
         </td>
      </tr>

      <tr>
         <th><label for="details">Additional Details</label></th>
         <td>
                 <textarea name="details" id="details"><?php 
                        if (isset($details)) echo $details; ?>
                 </textarea>
          </td>
      </tr>

      <tr style="display:none;">
         <th><label for="address">Address</label></th>
         <td><input type="text" name="address" id="address" /></td>
        <p>Please leave this field blank</p>
      </tr>


    </table>

    <input type="submit" value="Send" />

  </form>

1 Answer

jamesjones21
jamesjones21
9,260 Points

If I was at my laptop I would be able to help out, best way to do this is to do the html markup first then place the PHP code where you want the array to spit out the values.

For future reference I would always use the short hand PHP for a foreach loop. It allows you to embed HTML code neater than all echos all over the shop :)