-2
我有一個SQL查詢,該查詢返回結果如下:自定義陣列的顯示,PHP
,但我想這樣的
顯示它是如何,我可以在PHP中這樣做,我嘗試了,但我發現價值'Semaine X',將成爲表列的名稱和不能重複的'libelleCategorieClient'的值的一些困難 謝謝,
我有一個SQL查詢,該查詢返回結果如下:自定義陣列的顯示,PHP
,但我想這樣的
顯示它是如何,我可以在PHP中這樣做,我嘗試了,但我發現價值'Semaine X',將成爲表列的名稱和不能重複的'libelleCategorieClient'的值的一些困難 謝謝,
爲了將行成假設libelleCategorieClient是唯一列(旋轉),請嘗試以下操作:
<?php
$db = new PDO('mysql:host=localhost;dbname=test', 'root', 'pass');
$q = $db->query("SELECT DISTINCT cat.libelleCategorieClient,
sum(ligne.quantiteLigneCommande * ligne.prixLigneCommande) AS montant,
sem.libelleSemaine
FROM CategorieClient cat,Client c, commande cmd,
lignecommande ligne,semaine sem
WHERE cat.idCategorieClient=c.idCategorieClient
AND c.idClient=cmd.idClient AND cmd.idCommande=ligne.idCommande
AND sem.idSemaine=cmd.idSemaine
GROUP BY cat.libelleCategorieClient, sem.libelleSemaine");
$sem=array();
$client=array();
while($row=$q->fetch(PDO::FETCH_ASSOC)){
$sem[$row['cat.libelleCategorieClient']][$row['libelleSemaine']] = $row['montant'];
$client[$row['cat.libelleCategorieClient']]= $row['libelleCategorieClient'];
}
$semkey=key($sem);
echo '<table>';
echo '<tr>';
echo '<th>Cetegories Clients</th>';
foreach($sem[$semkey] as $keys=>$vals){
echo '<th>'.$keys.'</th>';
}
echo '</tr>';
foreach($sem as $key=>$val){
echo '<tr>';
echo '<td>'.$client[$key].'</td>';
foreach($val as $v){
echo '<td>'.$v.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
非常感謝你Mawia HL工作正常 –
什麼是查詢結果的結構?陣列?對象?數組中的對象? –
它是一個對象,它太長了,因爲我在我的DB中有很多表:'SELECT DISTINCT cat.libelleCategorieClient,sum(ligne.quantiteLigneCommande * ligne.prixLigneCommande)AS montant,sem.libelleSemaine FROM CategorieClient cat,Client c,commande cmd, lignecommande ligne,semaine sem WHERE cat.idCategorieClient = c.idCategorieClient AND c.idClient = cmd.idClient AND cmd.idCommande = ligne.idCommande AND sem.idSemaine = cmd.idSemaine group by cat.libelleCategorieClient,sem.libelleSemaine' –
此查詢返回上面的表 –