2017-07-21 2 views
1

Ich habe Abfrage-Ergebnis wie dieseein mehrdimensionales Array in PHP von Mysql Ergebnis

Abfrageergebnis Tabelle:

enter image description here

Ich brauche diese Daten in HTML Tabellenansicht angezeigt werden wie

HTMLTabelle:

enter image description here

So Wie soll ich dieses Ergebnis Array-Variable umwandeln in solchen PHP-Array-Variable, so kann ich Schleife, dass Array-Variablen und zeigt das Ergebnis nach angebracht html Tabellenansicht

Bitte helfen. Vielen Dank.

+0

Für den Anfang: http://php.net/manual/en/pdostatement.fetchall.php, http://php.net/manual/en/mysqli-result.fetch-assoc .php –

Antwort

1

Eine Möglichkeit ist, dass Sie mysql_fetch_assoc() Funktion in einer Schleife wie diese verwenden:

// The array which will store all questions 
$questions = array(); 

while ($row = mysqli_fetch_assoc($result)) { 

    // Put in the array questions an array per question using mysql fieldsnames 

    // if a question with main_question_id=2 exists 
    if (!isset($questions[$row['main_question_id'])) { 

     //build your question and put it in your array 
     $questions[$row['main_question_id']] = array(
     'question_num' => $row['main_question_number'], 
     'description' => $row['descritption'], 
     'obtained_mark' => $row['obtained_mark'], 
     // etc ... 
    ); 
    } 
} 

// Displays description for question 2 for example : 
echo $questions[2]['description']; 

Dann können Sie Ihre HTML bauen (waren auch machbar während erste Schleife)

// Builds head of html table 
$html = '<table><tr><th>Question Number</th><th>Desc.</th><th>Mark</th></tr>'; 

// Builds html content table with another loop 
foreach ($questions as $question){ 
    $html .= '<tr>'; 
    $html .= '<td>'.$question["main_number_question"].'</td>'; 
    $html .= '<td>'.$question["description"].'</td>'; 
    $html .= '<td>'.$question["obtained_mark"].'</td>'; 
    $html .= '</tr>'; 
} 

// build the bottom of table 
$html .= '</table>; 


// Displays all table 
echo $html; 

Vergessen Sie nicht, Um Doc und Kommentare hinter ihr hier zu überprüfen: http://php.net/mysqli_fetch_assoc

Sie können auch verwenden

Cheers,

+0

Danke für die Antwort bro, aber ich habe bereits Ergebnis in eine PHP-Array-Variable abgerufen, aber ich muss diese Zeilen in multidimensionalen Array wie ** main_question> sub_question> child_question ** speichern –