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 trialPhilip Harper
10,033 PointsArrays, Links and Page organisation in PHP
Hi all, so I'm working on a self-initiated project—mainly to help understand and learn about our mutual web friend, PHP.
My question is about Arrays, and web page organisation. I have a list of 200 museum pages, which fall into "Subject Category" AND "Admission Type" AND "London Area" — each museum page will be accessible from each of those navigational menu items, as they are attributes of the museum, for example, The British Museum is "Subject Category = Archaeology, Architecture, Art, Child Friendly, Decorative Arts + Design" AND "Admission Type = Free" AND "London Area = Central" — so you can see how one museum spans across these three main sections, if you like.
So,
I'm a bit confused about how to organise the arrays to organise them, I was thinking something like...
$categories = array ( 'Archaeology' => '<a href="../categories/archaeology-museums-london.php">Archaeology</a>', 'Architecture' => '<a href="../categories/architecture-museums-london.php">Architecture</a>');
// etc
Then drilling down into subject array, museum name equals museum link, like thus...
$archaeology = array ( 'Whatever Museum' => 'a href="../categories/archaeology-museums-london.php');
// etc
But then, I'm going to get alot of code repetition o of museum name as the same process would be repeated with "London Area" and "Admission Type" — I'm confused... whats about the best, practical and maintainable way of organising these museum pages into categories.
I hope that's clear and not confusing in the question?
2 Answers
Rebecca Vonada
5,274 PointsIf I'm reading your concerns correctly, you might be able to get away with this by making the museum your index and using multidimensional arrays, so:
$museums = array(
[Museum_1] => array(
'Category' => 'Art',
'Link' => 'http://linkstuff.com/',
'AdmissionType' => 'Free'
);
[Museum_2] => array(
'Category' => 'Not Art',
'Link' => 'http://linkotherstuff.com/',
'AdmissionType' => 'Not Free'
);
);
Now to access those, you could then iterate through the list, so:
foreach ($museums as $museum) {
if ($museum['Category'] == 'Art') {
// Do something with the museums that are art
}
}
If you have that much data, though, you might be better off storing the museum information in a database (SQL's an easy go-to and if you use phpMyAdmin you can import from CSV for easy database creation). It would save you iterating through a giant array multiple times when you needed to get collections.
Philip Harper
10,033 PointsRebecca!
Thanks so much for your reply / answer! You've explained it really well and to be honest, inspired me. I need to hurry up and get on to the course "Enhancing a Simple PHP Application" — I think MySQL databases are covered there.
Thanks again, really appreciate it.