I am trying to create various, standard HTML list styles from PHP arrays for a movie database I’m building as part of my final year project at university, however I’m having some trouble accessing/understanding how to do this. I’ve got the relevant data from the various APIs I’m using, but I’d like to create the entire list dynamically, dropping in the correct bits of information where appropriate.
Details of the two list styles I’m looking to create are below.
List style 1 – Character/Cast details
For the first style of list, I’d like to create it as:
<ul>
<li>Cast name - Character name</li>
<li>Cast name - Character name</li>
<li>etc. etc.</li>
<ul>
My data set direct from the array looks like this:
Array
(
[abridged_cast] => Array
(
[0] => Array
(
[name] => Daniel Craig
[id] => 162687443
[characters] => Array
(
[0] => James Bond
)
)
[1] => Array
(
[name] => Javier Bardem
[id] => 162661456
[characters] => Array
(
[0] => Silva
)
)
[2] => Array
(
[name] => Judi Dench
[id] => 162652435
[characters] => Array
(
[0] => M
)
)
[3] => Array
(
[name] => Ralph Fiennes
[id] => 162653681
[characters] => Array
(
[0] => Gareth Mallory
[1] => Mallory
)
)
[4] => Array
(
[name] => Naomie Harris
[id] => 162705781
[characters] => Array
(
[0] => Eve
)
)
)
In the above example, there’s five cast members w/ their respective character names, however on some other examples, there’s more or less than five, so, ideally, I’d want the system to know however many cast members were given and create a list for each, up to a maximum of 10 names.
List style 2 – Videos
Similar to the last style, however this list will hopefully look like this:
<ul>
<li><a href='<http://youtube.com/<dynamically generated URL direct to the clip>'><img src='dynamically generated URL to the clip's thumbnail' /></a></li>
<li>same as above...</li>
</ul>
The source array looks like this:
Array
(
[id] => 37724
[youtube] => Array
(
[0] => Array
(
[name] => Trailer
[size] => HQ
[source] => 24mTIE4D9JM
)
[1] => Array
(
[name] => Official Trailer
[size] => HD
[source] => 6kw1UVovByw
)
[2] => Array
(
[name] => Trailer 1
[size] => HD
[source] => 1Uyjf5Pp0Ko
)
[3] => Array
(
[name] => Trailer 2
[size] => HD
[source] => 5Ejo9_3iUpw
)
)
)
Apologies for the length of this post, thanks in advance to anyone who may be able to assist!
The thing you are looking for is called a foreach loop. For your first example it would look something like this:
Now this example is purely illustratory though it most definetely be the begginers approach.
Still, I would strongly recommend investigating MVC patterns and PHP frameworks, like for example YII. If deployed and configured right YII with all its goodness should make all the repetitive work done for you here.
I’ve seen that your question contains two parts, though I am not willing to write a sample for the second one, since it is practically the same as the first and with proper attention – you will get on track and figure it out.