php - 从 mysql 填充 PHP 数组

标签 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

所以你可以看到它不一样..有什么帮助吗!?

最佳答案

我认为你让事情变得比需要的更加复杂。检查w3c一种将查询结果转换为数组的简单方法。

关于php - 从 mysql 填充 PHP 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37632239/

相关文章:

javascript - 从数组中删除虚假值

PHP/MySQL 在比较网格或表格中显示产品和类别

php - 在 ColdFusion 中验证来自 Microsoft Teams 自定义机器人的 HMAC

php - 使用 PHP 将 450x450 的正方形图像裁剪成 9 个 150x150 的图像

javascript - 如何使用 AJAX 告诉 PHP 在文章下方显示哪些评论?

java - 由 : java. time.DateTimeException 引起:发现冲突:字段 DayOfWeek 6 不同于从 2016-01-30 派生的 DayOfWeek 2

mysql - 在 django 中用 group by 列的总和

Java:无法使用代码计算中位数

mysql - SQL 中的标签组

c - 短路评估是否保证评估顺序?