从mysql填充PHP数组

时间:2016-06-04 15:56:07

标签: php mysql arrays pdo

你好,

我想从mysql填充一个数组,但它不能很好地工作..

所以请任何人都可以帮助我!!

Function.php

 class Affectation{
    public function get_all_member() {

        $matricule = array();
        $resultats = Connexion_bd::getInstance()->query("select distinct  d.matricule_oe from demande as d,officier_eleve as o"
            . " where d.matricule_oe=o.matricule GROUP BY o.matricule ORDER BY AVG(o.moy_s1 + o.moy_s2)/2 DESC ");
        $resultats->setFetchMode(PDO::FETCH_OBJ);
        while ($rslt = $resultats->fetch()) {
            $matricule[] = $rslt;
            // array_push($rows, $rslt);
        }
        return $matricule;
    }


    public function liste_sujets(){

        $sujets = array();
        $resultats = Connexion_bd::getInstance()->query("select distinct id_sujet from demande ");
        $resultats->setFetchMode(PDO::FETCH_OBJ);
        while ($rslt = $resultats->fetch()) {
            $sujets[] = $rslt;
        }

        return $sujets;
    }


    public function liste_choix() {
        $choix = array();
        $resultats = Connexion_bd::getInstance()->query("select choix from demande  ");
        $resultats->setFetchMode(PDO::FETCH_OBJ);
        while ($rslt = $resultats->fetch()) {
            $choix[] = $rslt;
        }
        return $choix;
    }

}
}

array.php

<?php
include_once(dirname(__FILE__) . '/../class/connexion_bd.php');
include_once(dirname(__FILE__) . '/../class/Function.php');
$aff = new Affectation();
$matricule = $aff->get_all_member();
$sujets = $aff->liste_sujets();
$choix = $aff->liste_choix();
$nombre_etudiant = 0;
foreach ($matricule as $etudiant) {
    $nombre_etudiant++;
}
$nombre_sujet = 0;
foreach ($sujets as $sujet) {
    $nombre_sujet++;
}
$length = $nombre_etudiant;
$mat = array($length);
for ($i = 0; $i <= $length; $i++)
    $mat[$i] = array($length);

if ($nombre_etudiant == $nombre_sujet) {
    $i = 1;
    foreach ($matricule as $cle => $valeur) {
        $mat[0][$i] = $valeur->matricule_oe;
        echo $mat[0][$i] . " / ";
        $i++;
    }

    $i = 1;
    foreach ($sujets as $cle => $valeur) {
        $mat[$i][0] = $valeur->id_sujet;
        echo $mat[$i][0] . " / ";
        $i++;
    }

    $i = 1;
    foreach ($choix as $cle => $valeur) {
        for ($j = 1; $j <= $nombre_etudiant; $j++) {
            $mat[$j][$i] = $valeur->choix;
        }
        $i++;
    }
    $mat_copy = array($length);
    for ($i = 1; $i <= $length; $i++)
        $mat_copy[$i] = array($length);

    $mat_copy = $mat;

?>
<table border="1">
<?php
for ($i = 0; $i <= $length; $i++) {
    echo "<tr>";
    for ($j = 0; $j <= $length; $j++) {
        echo "<td>" . $mat[$i][$j] . "</td>";
    }
    echo "</tr>";
}
?>

的MySQL

enter image description here

网页

enter image description here

应该是这样的

Requred output

所以你可以看到它不一样..任何帮助!?

1 个答案:

答案 0 :(得分:0)

我认为你让它变得比它需要的更复杂。检查w3c以便将查询结果转换为数组。