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 trialPaul Adams
11,105 PointsHas 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";
}
}
1 Answer
jamesjones21
9,260 PointsIf 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 :)
Paul Adams
11,105 PointsPaul Adams
11,105 PointsOk, 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.