<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<?php 

    function nombreAbonne($nomcentrale,$typeabonne){
        $servername="mysql:host=94.23.252.168;dbname=ersendb";
        $username="root";
        $password="prose@111";
        $connexion=new PDO($servername,$username,$password);
        
        if($connexion){
            $query="SELECT count(*) as nbreTypeAbonne FROM ersen_abonne,ersen_centrale,ersen_typecategorie,ersen_categorie
            WHERE ersen_abonne.centrale=ersen_centrale.id
            AND ersen_abonne.typecategorie=ersen_typecategorie.id
            AND ersen_typecategorie.categorie=ersen_categorie.id
            AND ersen_centrale.nom = '$nomcentrale'
            AND ersen_categorie.libelle = '$typeabonne'";

            $result=$connexion->query($query); 
                     
            $pdo=$result->fetch();
            if($pdo){
                $resultat=$pdo['nbreTypeAbonne'];
            }else{
                $resultat=null;
            }
        }
        return $resultat;
    }
?>
<?php 
    function nombreAbonneDynamic(){
        $servername="mysql:host=94.23.252.168;dbname=ersendb";
        $username="root";
        $password="prose@111";
        $connexion=new PDO($servername,$username,$password);
        if($connexion){
            $query="SELECT DISTINCT ersen_centrale.nom AS nom,ersen_categorie.libelle AS libelle 
                        FROM ersen_abonne,ersen_centrale,ersen_typecategorie,ersen_categorie
                        WHERE ersen_abonne.centrale=ersen_centrale.id
                        AND ersen_abonne.typecategorie=ersen_typecategorie.id
                        AND ersen_typecategorie.categorie=ersen_categorie.id";
            $pdo=$connexion->query($query);
            ?>
            <table class="table table-hover">
                <thead style="background-color: #1E88E5; color: white;">
                    <tr>
                        <th style="border: 2px solid blue">Nom</th>
                        <th style="border: 2px solid blue">Nombre</th>
                        <th style="border: 2px solid blue">Type</th>
                    </tr>
                </thead>
                
                <tbody style="background-color: #8C9EFF;color:black;">
                    <?php while($row=$pdo->fetch()){?>
                    <tr>
                        <?php $appel=nombreAbonne($row['nom'],$row['libelle']); ?>
                        <td style="border: 2px solid blue"><?php echo $row['nom'];?></td>
                        <td style="border: 2px solid blue"><?php echo $appel.' abonnes';?></td>
                        <td style="border: 2px solid blue"><?php echo $row['libelle'];?></td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>

            <?php
        }
    }
?>
<h4 style="margin-left: 450px;">Les abonnes pour chaque centrale </h4>
<?php echo nombreAbonneDynamic(); ?>
</body>
</html>