I have a set of data that is not normalized. I do the normalization in PHP it it works just fine.
The dataset looks like this (screenshot bellow), it is a lot larger tho. I am only interested in orginizing the category and the type. With those two orginized I can make good tables and menu's.
Problem
Now the problem is, I am switching to an AJAX system, and the data no longer comes into PHP. Instead it comes directly into the page using node.js/mongodb. Is there a way I can do something like this:
<script type="text/javascript">
// Array containing all the objects from the server
// looks like this
var data = [Object, Object, Object];
var artikelen = [];
for(var i=0; i<data.length; i++){
artikelen[data[i].categorie][data[i].type][] = data[i];
}
</script>
// ----------------
OLD SITUATION
//-----------------
In PHP I did this:
$sql = "SELECT *
FROM mydb
WHERE merk = 'webecos'
ORDER BY categorie, type, code";
$result = $wpdb->get_results( $sql );
foreach($result as $row){
$artikelen[$row->categorie][$row->type][] = $row;
}
Now I can make good sorted tables / menu with nested loops. The code I used is this.
<ul id="inkoop_menu">
<?php foreach ( $artikelen as $categorie => $row ): ?>
<li>
<a class="inkoop_button" data-menu="categorie" href="#">
<?=$categorie; ?>
</a>
<ul>
<?php foreach ( $row as $type => $artikel ): ?>
<li>
<a class="inkoop_button" data-menu="type" href="#">
<?=$type; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
Edit
Maybe this is a but better to understand. The array I am after looks like this:
array['categorie-name']['type-name'][x] = whole object;
[]=
, you need to usepush
instead. – Gumbo Mar 12 '12 at 20:07