Help for loop/multidimensional array

Hi all,
could someone help me please to find a way to output this query grouped by [Nomeprodotto], [spess], [marca] as follow ?
After trying dozens of times with different sample script, now I’m stuck and so confused… :frowning:
Thank in advance !

Header:
[Nomeprodotto] - [spess] - [marca]

Loop:
[quant] - [IDum] - [lungo] - [corto] - [$mq]
[quant] - [IDum] - [lungo] - [corto] - [$mq]
[quant] - [IDum] - [lungo] - [corto] - [$mq]
[quant] - [IDum] - [lungo] - [corto] - [$mq]
[quant] - [IDum] - [lungo] - [corto] - [$mq]
[quant] - [IDum] - [lungo] - [corto] - [$mq]

[php]<?php

$query2 = mysql_query("
SELECT
aa_distinta.IDrecord,
aa_distinta.IDdettaglio,
prodotti.nomeprodotto,
dettagli_sugli_ordini.spess,
aa_distinta.marca,
aa_distinta.IDum,
aa_distinta.quant,
aa_distinta.lungo,
aa_distinta.corto,
dettagli_sugli_ordini.IDordine
FROM
aa_distinta
INNER JOIN dettagli_sugli_ordini On dettagli_sugli_ordini.IDdettaglio = aa_distinta.IDdettaglio
INNER JOIN prodotti On prodotti.IDprodotto = dettagli_sugli_ordini.IDprodotto
WHERE
dettagli_sugli_ordini.IDordine = ‘".$_GET[‘IDordine’]."’
ORDER BY
nomeprodotto, spess desc, corto desc, lungo desc"
, $link);

While ($record2 = mysql_fetch_array($query2)) {
$mq = $record2[‘quant’]$record2[‘lungo’]$record2[‘corto’]/10000;

echo “

” . $record2[‘IDrecord’] . “”;
echo “” . $record2[‘nomeprodotto’] . “”;
echo “” . $record2[‘spess’] . “”;
echo “” . $record2[‘marca’] . “”;
echo “” . $record2[‘IDum’] . “”;
echo “” . $record2[‘quant’] . “”;
echo “” . $record2[‘lungo’] . “”;
echo “” . $record2[‘corto’] . “”;
echo “” . $mq . “”;
echo ‘’;
}

?>[/php]

What are you currently getting for an output?

I’m getting the loop included header in the row (not yet splitted)

Paste a copy so I can see it.

[nomeprodotto] - [spess] - [marca] - [quant] - [IDum] - [lungo] - [corto] - [$mq]

[nomeprodotto] - [spess] - [marca] - [quant] - [IDum] - [lungo] - [corto] - [$mq]

[nomeprodotto] - [spess] - [marca] - [quant] - [IDum] - [lungo] - [corto] - [$mq]

[nomeprodotto] - [spess] - [marca] - [quant] - [IDum] - [lungo] - [corto] - [$mq]

[nomeprodotto] - [spess] - [marca] - [quant] - [IDum] - [lungo] - [corto] - [$mq]

Ok thats what I thought… try adding AS something after your select line in the query like this

[php]SELECT
aa_distinta.IDrecord AS IDrecord,
aa_distinta.IDdettaglio AS IDdettaglio,
prodotti.nomeprodotto AS nomeprodotto,
dettagli_sugli_ordini.spess AS spress,
aa_distinta.marca AS marca,
aa_distinta.IDum AS IDum,
aa_distinta.quant AS quant,
aa_distinta.lungo AS lungo,
aa_distinta.corto AS corto,
dettagli_sugli_ordini.IDordine AS IDordine
FROM[/php]

I may be wrong, but I believe he wants to output the results as groups. So every combination of [nomeprodotto] - [spess] - [marca] would be a different group.

If that’s the case you would need to build a grouped array from the results. Depending on how you need the output, you could possibly combine the 3 variables to build a group. e.g.

[php]
// Group results
$grouped = array();
while($record2 = mysql_fetch_array($query2)) {
$group = $record2[‘nomeprodotto’] . " - " . $record2[‘spess’] . " - " . $record2[‘marca’];
$grouped[$group][] = $record2;
}

// Output results

foreach($grouped as $header => $values) {
echo "Header: " . $header . “
”;
foreach($values as $value) {
$mq = $value[‘quant’]$value[‘lungo’]$value[‘corto’]/10000;
echo $value[‘quant’] . " - " . $value[‘IDum’] . " - " . $value[‘lungo’] . " - " . $value[‘corto’] . " - " . $mq . “
”;
}
}
[/php]

Display is not the issue he is facing passing the data from the query is the reason he is getting repeats of the same row with out the data is because issue with his query not displaying the output.

There is nothing in his code that suggests output is the problem. If it was an issue with column named he would be getting undefined variable errors with no output. Either way, he has two potential solutions :slight_smile:

@Andrew
Maybe I don’t understand how can apply your solution

@m@tt
It works exactly as I need !!
Is that I was looking for ! :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:
Now I also understand the logic of the multidimensional array. An example like this is more useful than hours and hours spent studying the manuals. The exchange of informations is life!
Thanks a lot !

Sponsor our Newsletter | Privacy Policy | Terms of Service