Is it possible to make a multilevel array using a mysql query? E.g. if I want to get 4 pictures for each product?
[1] => Array( 'name' => 'Product 1', 'picture' => array('picture1','picture2','picture3','picture4') )
[2] => Array( 'name' => 'Product 2', 'picture' => array('picture5','picture6','picture7','picture8') )
Or do I need to make a foreach to loop through the products and then in the foreach make a mysql query to get each products pictures?
EDIT:
My structure is:
P_Attributes
--------
id, int(15)
name, varchar(256)
P_AttributeValues
--------
id, int(15)
value, varchar(256)
attribute_id, int(15) [NOTE: This is connected to P_Attributes.id]
Then I want to get ALL P_AttributeValues to a P_Attribute row – and get it in ONE query. Is that possible?
EDIT 2:
With the query made by the accepted answers author I made it work with this PHP-code:
$attributevalues = $auctionClass->get_rows($id);
$attr_val = array();
foreach($attributevalues as $k => $v){
$attr_val[$v->AID]['attr_name'] = $v->AName;
$attr_val[$v->AID]['parameters'][] = array('attr_value_name' => $v->name, 'id' => $v->id);
}
If you just use a normal JOIN query and order it, you can get rows coming out of the records that can be formed in to the necessary hierarchical structure.
This will generate rows like this:
No idea what MySQL extension you are using, presumably PHP as I mentioned it and you didn’t correct me. If you fetch the associative array per record returned, which will be, per record, in this form:
If you have a main data array called
$products, you can produce it by just putting this code in the loop, assuming the record is called$productRecand filled in the loop before this.Using the IDs for the keys should be alright, presuming that they are primary keys with no duplicates. Will aid look up that way, rather than losing that data.