Domanda

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);
}
È stato utile?

Soluzione

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.

SELECT pa.id AS product_id, pa.name , pav.id AS product_attr_id, pav.value
FROM P_Attributes pa JOIN P_AttributeValues pav ON pav.attribute_id = pa.id
ORDER BY pa.id ASC, pav.id ASC

This will generate rows like this:

product_id, name, product_attr_id, value
1, "Product 1", 1, "picture1"
1, "Product 1", 2, "picture2"
1, "Product 1", 3, "picture3"
1, "Product 1", 4, "picture4"
2, "Product 2", 5, "picture5"
2, "Product 2", 6, "picture6"
2, "Product 2", 7, "picture7"
2, "Product 2", 8, "picture8"

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:

array('product_id' => 1, 'name' => "Product 1",
      'product_attr_id' => 1, 'value' => "picture1");

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 $productRec and filled in the loop before this.

if (!array_key_exists($productRec['product_id'], $products)) {
    $products[$productRec['product_id']] = array('name' => $productRec['name'],
                                                 'picture' => array());
}

$products[$productRec['product_id']]['picture'][$productRec['product_attr_id']] =
    $productRec['value'];

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.

Altri suggerimenti

Here is a good article on doing this type of query:

http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

You might be able to adjust their examples to fit your needs.

Depending on your table structure, you may be able to pull rows that have everything you're looking for, and then code the creation of the array from that row.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top