【问题标题】:Populate PHP Array from mysql从 mysql 填充 PHP 数组
【发布时间】:2016-10-04 13:47:07
【问题描述】:

你好,

我想从 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

网页

应该是这样的

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

【问题讨论】:

  • 网页应该是什么样子的?
  • 我编辑了我的帖子,这样你就可以知道它应该是什么样子了

标签: php mysql arrays pdo


【解决方案1】:

我认为你让这变得比它需要的更复杂。查看w3c 了解将查询结果转换为数组的简单方法。

【讨论】:

  • 我在 hongroise 算法中工作,我刚开始,所以我应该首先准备二维数组,这就是我的代码看起来像这样的原因:/
猜你喜欢
  • 2012-09-06
  • 2012-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多