2016-06-04 10 views
0

hallo,Populate PHP Array von mysql

Ich möchte ein Array von mysql füllen, aber es funktioniert nicht gut ..

Also bitte jemand kann mir helfen !!

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

Webseite

enter image description here

es wie dieses

Requred output

so, wie Sie es sehen können nicht das gleiche .. jede Hilfe sein sollte !?

+0

Wie sollte es auf der Webseite aussehen? –

+0

Ich bearbeite meinen Beitrag, damit du wissen kannst, wie es sein sollte – mamoud

Antwort

0

Ich denke, Sie machen dies komplexer als es sein muss. Überprüfen Sie die w3c für eine einfache Möglichkeit, Abfrageergebnisse in ein Array zu verwandeln.

+0

ich arbeite im hongroise Algorithmus, ich war im Begnen, also sollte ich das Array 2D zuerst vorbereiten, deshalb sieht mein Code so aus: / – mamoud