Hello,
Having a bit of trouble trying to figure out how I can iterate through an array in PHP to update the database. I am getting a string with all the elements serialized from jquery ui using the .sortable. This is a nested list made of UL/LI tags. Here is an example: http://www.b-hind.eu/jquery/index.php
The array comes out as a serialized string... (formatted below to make it easier to read)
listItem[]=472
&listItem_472[]=478
&listItem[]=479
&listItem[]=480
&listItem[]=481
&listItem[]=469
&listItem[]=445
&listItem[]=468
&listItem[]=477
This is what I need to to translate as.
0=472
472=478
0=479
0=480
0=481
0=469
0=445
0=468
0=477
The left number indicates parentid. The right number indicates the childid. "0" is the root level.
Any help much appreciated.
Parsing array
Moderator: General Moderators
Re: Parsing array
What's the unformatted version? What do you actually have? Is it an array? A string? Something else?joeyd wrote:(formatted below to make it easier to read)
Re: Parsing array
Thanks for the reply...
This is the jquery code that sends the string to my save page.
Here is the unordered list... The string comes from the ID of each element. Root level items are just listItem[].
This is the actual string that is given via jquery...
listItem[]=472&listItem_472[]=478&listItem[]=479&listItem[]=480&listItem[]=481&listItem[]=469&listItem[]=445&listItem[]=468&listItem[]=477
This is the jquery code that sends the string to my save page.
Code: Select all
$('#navlist').sortable({
'items':'li',
'placeholder':'placeholder2',
'nested':'ul',
'maxLevels':5,
update : function () {
var order = $('#navlist').sortable('serialize');
$("#result").load("save.php?"+order);
Code: Select all
<ul class="ui-sortable" id="navlist">
<li id="listItem_472">Services
<ul>
<li class="" style="" id="listItem_478">IT/Networking</li>
</ul>
</li>
<li id="listItem_479">Products</li>
<li id="listItem_480">Case Studies</li>
<li id="listItem_481">Partners</li>
<li id="listItem_469">About Us</li>
<li id="listItem_445">Bios</li>
<li id="listItem_468">Careers</li>
<li id="listItem_477">Contact Us</li>
</ul>listItem[]=472&listItem_472[]=478&listItem[]=479&listItem[]=480&listItem[]=481&listItem[]=469&listItem[]=445&listItem[]=468&listItem[]=477
Re: Parsing array
First I'd check that the query string is valid for your purpose.
Then you can iterate over $_GET. If the key is just "listItem" then substitute it for "listItem_0".
Given that, the parent node ID is (int)substr(key, 10) and the child node ID is the value.
Code: Select all
if (preg_match('/^(listItem(_\d+)?\[\]=\d+&?)+$/', $_SERVER["QUERY_STRING"])) {Given that, the parent node ID is (int)substr(key, 10) and the child node ID is the value.