Create HTML lists from PHP arrays - php

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:
foreach($dataSet['abridged_cast'] as $castMember){
echo '<li>'.$castMember['name'].'-'.implode(', ', $castMember['characters']). '</li>';
}
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.

Related

Convert form to json, submit as single form element, then convert back as multidimensional array

I am attempting to get around having to raise the max_input_vars parameter when submitting a large form. My thought was to jsonify the form, create a second form with a single field in it, put the jsonified string into that single field, submit, decode as an array, and then replace the POST array with the decoded data. I am plugging this into an existing system so for this to work I would need the new array to have the same structure as the raw post would. However, I am running into an issue where regardless of how I encode the data I am losing info and structure beyond the first depth of the array. I have tried both using this library to encode the form:
https://github.com/macek/jquery-serialize-object
As well as jQuery(form).serializeArray(), each of which delivers results that decode in their own unintended ways. This is what the raw post data looks like when I don't intercept the form and dump it with print_r($_POST):
Array
(
[closedpostboxesnonce] => 9d9dc8fa74
[meta-box-order-nonce] => 520ef5d263
[update-nav-menu-nonce] => cfea78920d
[_wp_http_referer] => /wp-admin/nav-menus.php
[action] => update
[menu] => 2
[menu-name] => Left Sidebar Menu
[save_menu] => Save Menu
[qtranslate-fields] => Array
(
[menu-item-title] => Array
(
[3871] => Array
(
[en] => Knowledge Center
[ja] => Knowledge Center
[ko] => Knowledge Center
[zh] => Knowledge Center
[qtranslate-separator] => [
)
The data when I use the jquery-serialize-object library above to encode the form data, then decode it with json_decode($_POST["allinputs"],true):
Array
(
[closedpostboxesnonce] => 9d9dc8fa74
[meta-box-order-nonce] => 520ef5d263
[update-nav-menu-nonce] => cfea78920d
[_wp_http_referer] => /wp-admin/nav-menus.php
[action] => update
[menu] => 2
[menu-name] => Left Sidebar Menu
[qtranslate-fields[menu-item-title][3871][en]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][ja]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][ko]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][zh]] => Knowledge Center
Using jQuery(form).serializeArray() to encode, and $allinputs = (array)(json_decode($_POST["allinputs"])); to decode:
Array
(
[0] => stdClass Object
(
[name] => closedpostboxesnonce
[value] => 9d9dc8fa74
)
[1] => stdClass Object
(
[name] => meta-box-order-nonce
[value] => 520ef5d263
)
[2] => stdClass Object
(
[name] => update-nav-menu-nonce
[value] => cfea78920d
)
Is there a way to get the data encoded and then decoded while retaining the original structure?
Edit: using #think-win-win 's suggestion of passing true as the second parameter of json_decode yields:
Array
(
[0] => Array
(
[name] => closedpostboxesnonce
[value] => 9d9dc8fa74
)
[1] => Array
(
[name] => meta-box-order-nonce
[value] => 520ef5d263
)
[2] => Array
(
[name] => update-nav-menu-nonce
[value] => cfea78920d
)
It is one of the ones that I tried, along with a couple of others, but I felt the post was getting too long to include them all. The above one loses the associativeness of the top level of the array, in addition to converting the deeper levels to:
[7] => Array
(
[name] => qtranslate-fields[menu-item-title][3871][en]
[value] => Knowledge Center
)
[8] => Array
(
[name] => qtranslate-fields[menu-item-title][3871][ja]
[value] => Knowledge Center
)

Multisorting a nested array in php

I've got a bunch of content pulled out of several databases and compiled into a nested array like this:
Array
(
[0] => Array
(
[id] => 3
[published] => 1433940002
[content] => This is some content
)
[1] => Array
(
[id] => 52
[published] => 1433940001
[content] => This is some more content
)
[2] => Array
(
[id] => 16
[published] => 1433940003
[content] => This is some more content
)
)
Since I cannot sort the content whilst it is retrieved (because it is done using several queries from as many databases) I want to dig down a level into the array and sort by the "published" date whilst maintaining the depth of the array so I end up with...
Array
(
[0] => Array
(
[id] => 52
[published] => 1433940001
[content] => This is some more content
)
[1] => Array
(
[id] => 3
[published] => 1433940002
[content] => This is some content
)
[2] => Array
(
[id] => 16
[published] => 1433940003
[content] => This is some more content
)
)
I'm pretty sure array_multisort() is the way to go but I can't think where to start! Could somebody be so kind as to give me a pointer?
Thanks to #Rizier123's policing of my question I figured it out myself. For anyone else finding this thread here's my solution using the example array I supplied above.
//-- get disorganised content from databases here
usort($array, "publishcmp");
//-- output organised content here
function publishcmp($a , $b){
if ($a['published'] == $b['published']) {
return 0;
}
return ($a['published'] < $b['published']) ? -1 : 1;
}
Pretty much taken verbatim from php.net

How to show specific value from array with mustache?

I have an array like:
Array
(
[0] => Array
(
[title] => Title 1
[value] => Value 1
[id] => 1428735262
)
[1] => Array
(
[title] => Title 2
[value] => Value 2
[id] => 2428735262
)
[2] => Array
(
[title] => Title 3
[value] => Value 3
[id] => 3428735262
)
)
With mustache I am able to iterate overall of them using:
{{#values}}
<h1>{{title}}</h1>
{{{value}}}
{{/values}}
But what if I only want to show specific value, for example only second element of the array (one with Title 2)?
I tried like:
{{#values}}
{{#2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/2428735262}}
{{/values}}
and:
{{#values.2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/values.2428735262}}
But none worked. Does anyone know how to get specific item from array ?
PS. I am using PHP version of mustache. Thanks
You can use this:
{{values.1.value}}
Read more at:
https://github.com/bobthecow/mustache.php/wiki/Variable-Resolution

Sort array according to the search string occurence php

I want to sort two dimensional array according to the occurrence of the search string in php. i tried to fetch data according to the relevance of search string . i come across match and against but it will work on the MYISAM but my table is in innodb. so plan to sort the array using any sort function but i cant find out anything.
my search pressure tes string array
Array
(
[0] => pressure
[1] => tes
)
My output array which matches the above string with title is
Array
(
[0] => Array
(
[title] => tests.doc
[link] => http://localhost/test.doc
[snippet] =>
)
[1] => Array
(
[title] => Pressure Testing Your Company
[link] => http://localhost/Pressure_Testing_Your_Companys.pdf
[snippet] => Questions used by the CFO against dimensions critical to success
)
[2] => Array
(
[title] => pressure.doc
[link] => http://localhost/pressure.doc
[snippet] => Templates for services
)
)
In the above array the most relevant array[1] then array[2] and then array[0] should be in this order. i want to sort this array accordingly. my output should be like below:
Array
(
[0] => Array
(
[title] => Pressure Testing Your Company
[link] => http://localhost/Pressure_Testing_Your_Companys.pdf
[snippet] => Questions used by the CFO against dimensions critical to success
)
[1] => Array
(
[title] => pressure.doc
[link] => http://localhost/pressure.doc
[snippet] => Templates for services
)
[2] => Array
(
[title] => tests.doc
[link] => http://localhost/test.doc
[snippet] =>
)
)
please help me!!!!
Look at the example #3, I think that's what you want.
http://php.net/manual/en/function.array-multisort.php
Just tested this, it seems to be working :
$titles = array();
foreach ( $array as $key => $value )
{
$titles[ $key ] = $value['title'];
}
array_multisort( $titles, SORT_ASC , $array );
I would highly recommend to sort your results in your MySQL query, that would be much better performance wise.

Parsing XML into a HTML list

I' ve got a XML, which I parsed into a PHP array. It looks like this:
Array
(
[Album] => Array
(
[id] => 12
[name] => My album
[id_parent] => 0
[row] => Array
(
[Album] => Array
(
[row] => Array
(
[id] => 14
[name] => Another album
[id_parent] => 12
[Photos] => Array
(
[row] => Array
(
[0] => Array
(
[id] => 1078
[name] => My first photo
[Album] => Array
(
[row] => Array
(
[name] =>Another album
[id] => 15
[Photos] => Array
(
[row] => Array
(
[0] => Array
(
[id] => 1069
[name] => Summer photo
[checked] => [checked]
)
and so on.
How can I parse it into a HTML list (in nested ul's), to get something like
My album
-Another album
--My first photo
---Another album
----Summer photo
---[end Another album]
--[end My first photo]
-[end Another album]
[end My album]
Albums and photos are just samples. i need to find a way how to parse it to HTML.
EDIT:
Okay, I add something what I really meant - "checked" node. I simply need to put the parent node into tags.
I would use a quick XSLT transform to do it.
If you post a sample of the base XML I can point you in the right direction, but otherwise, check out the examples at these two links. With a little modification they can do what you want.
http://www.w3schools.com/xsl/xsl_for_each.asp
http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

Categories