<?php

    include ('pages/dbconnect.php');            
    $rekdep=mysql_query("SELECT * FROM t_org ORDER BY dept, com ASC");
    echo "<table class='gridtable'>";
    echo "<tr><tr><th> Departement</th><th> Commune </th><th> ORG </th><th>Animal</th>";
    echo "</tr>";
    echo "</tr>";
    $odl_dept = null;
    $odl_com = null;
    $odl_org = null;

    while($dep=mysql_fetch_assoc($rekdep)){ 
            //List of department
            if($dep['dept']!=$odl_dept){
                echo "<td>".$dep['dept']."</td>";
            }
            //display blank td if the same department is found a second time
            else{echo "<td></td>";}   
            //List of commune
            if($dep['com']!=$odl_com){
                echo "<td>".$dep['com']."</td>";
                }
            //display blank td if the same commune is found a second time
            else{echo "<td></td>";}   
    // list of organizations
            if($dep['nom']!=$odl_org){
                echo "<td>".$dep['nom']."</td>";
                $org= $dep['nom'];
                //display the number of animal for each organization
                $qcab= mysql_query("SELECT sum(`qte_an`) FROM `t_animal` WHERE ((org= '$org') AND (nom_an='Cabrit'))") or die(mysql_error());
                $arr_cab = mysql_fetch_array($qcab);
                $qte_cab = $arr_cab[0];
                echo "<td>"; echo $qte_cab; echo "</td>";
            }
    echo "</tr>";        
    $odl_dept = $dep['dept'] ;
    $odl_com = $dep['com'] ;
    $odl_org = $dep['nom'] ;
}   
echo "</table>";
?> 

Dani AI

Generated

The thread shows an HTML table built in PHP and a per-row SUM query. ’s code creates many queries inside the loop (an N+1 pattern); correctly asked whether the goal is an HTML view or a true Excel file; suggested a temporary table. A clearer, faster pattern is to (1) produce one combined SQL result that aggregates the animal counts, and then (2) export that result as CSV (simple, Excel-friendly) or as a proper XLSX using a library.

Aggregate in SQL so the database does the work instead of running a query per row. Example pattern (add more SUM(CASE ...) columns for other animals as needed):

SELECT o.dept, o.com, o.nom AS org,
  SUM(CASE WHEN a.nom_an = 'Cabrit' THEN a.qte_an ELSE 0 END) AS qte_cab
FROM t_org o
LEFT JOIN t_animal a ON a.org = o.nom
GROUP BY o.dept, o.com, o.nom
ORDER BY o.dept, o.com, o.nom;

Export as CSV from PHP using PDO and streaming to stdout. Key points: send correct headers, emit a UTF-8 BOM if Excel compatibility is needed, and use fputcsv to handle quoting and commas:

<?php
// assume $pdo is a PDO instance and $stmt is executed from the SQL above
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="export.csv"');
echo "\xEF\xBB\xBF";
$out = fopen('php://output','w');
fputcsv($out, ['Departement','Commune','Org','Cabrit']);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  fputcsv($out, [$row['dept'],$row['com'],$row['org'],$row['qte_cab']]);
}
fclose($out);
exit;
?>

Troubleshooting and notes: avoid deprecated mysql_* functions — use PDO or mysqli and prepared statements; ensure no prior HTML or whitespace before headers; for formatted spreadsheets use PhpSpreadsheet (composer package phpoffice/phpspreadsheet); for very large exports stream results or build a server-side view/temporary table to reduce complexity. This approach ties ’s temp-table idea into a cleaner SQL/view + export workflow.

Recommended Answers

All 4 Replies

Hi everyone, i ask for your help because that's the first time i have to export it, after reading some comment and some document, i' able to export data from one table but this case is different, i combine different tables in the query. is there any easiest way to get the data in order to export them. please me...

Take a moment to read what you are asking and then read in wikipedia (or anywhere) what is html and what is excel. So the problem is producing the html table or do you really want to export your data to excel file ? .

Recently I learned that learning curve can be quite flat to people that are smart , but have no real interest to learn something new.

You could create a temporary table (using PHP) to combine the data from the other tables and then export that using PHPMyAdmin. You can also create a report putting the data into an HTML table and then write that to excel (if you want to avoid the manual intervention).

Thanks Chrishea, i'll try it like you said.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.