2016-06-04 2 views
0

привет,Populate PHP массив из MySQL

я хочу, чтобы заполнить массив из 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

так, как вы можете видеть, что это не то же самое .. любая помощь !?

+0

Как должно выглядеть оно на вашем сайте? –

+0

Я редактирую свой пост, чтобы вы могли знать, как это должно быть – mamoud

ответ

0

Я думаю, вы делаете это более сложным, чем это должно быть. Проверьте w3c на простой способ преобразования результатов запроса в массив.

+0

Я работаю в алгоритме hongroise, я был в начале, поэтому сначала должен был подготовить массив 2D, поэтому мой код выглядит так: / – mamoud

Смежные вопросы