2015-05-02 13 views
5

Questo è un problema semplice che ho cercato di risolvere per diverse ore. Ho una matrice contenente informazioni di diversi studenti e segna hanno segnato nei test. Ci sono 8 soggetti in totale, ogni soggetto ha 5 parametri.Visualizzazione di un array nidificato in una tabella HTML

La matrice si presenta come di seguito:

Array 
(
    [Ron] => Array 
     (
      [subject1] => Array 
       (
        [test1] => 47 
        [test2] => 86 
        [total] => 133 
        [percentage] => 88.67 
        [status] => Pass 
       ) 
      [pass_count] => 8 
      [fail_count] => 0 
      [gross_total] => 963 
      [gross_percentage] => 80.25 

      [...] 

      [subject8] => Array 
       (
        [test1] => 48 
        [test2] => 86 
        [total] => 134 
        [percentage] => 89.33 
        [status] => Pass 
       ) 

      [pass_count] => 8 
      [fail_count] => 0 
      [gross_total] => 900 
      [gross_percentage] => 75.50 
     ) 

    [John] => Array 
     (
      [subject1] => Array 
       (
        [test1] => 39 
        [test2] => 72 
        [total] => 111 
        [percentage] => 74 
        [status] => Pass 
       ) 
      [pass_count] => 8 
      [fail_count] => 0 
      [gross_total] => 963 
      [gross_percentage] => 80.25 

      [...]     

      [subject8] => Array 
       (
        [test1] => 39 
        [test2] => 75 
        [total] => 114 
        [percentage] => 76 
        [status] => Pass 
       ) 

      [pass_count] => 8 
      [fail_count] => 0 
      [gross_total] => 846 
      [gross_percentage] => 70.5 
     ) 

) 

ho bisogno di visualizzare quanto segue in una tabella HTML ben formattata (sto pensando di utilizzare bootstrap), ma non posso per la vita di me figura come nidificare correttamente la tabella utilizzando PHP.

Questo è il markup HTML che ho attualmente: http://jsfiddle.net/9y910uvp/

Questo dà il seguente risultato:

Il che va bene.

il problema reale

Sto utilizzando PHP per visualizzare il contenuto dalla matrice. Sono confuso su come visualizzare i valori per le sezioni nidificate <tr> e <td>.

Come si esegue il ciclo della matrice e si visualizza il contenuto correttamente? (Pubblicheremo i miei tentativi finora, ma sono tutti stupidi e non funzionano quindi non sto postando).

Un esempio sarebbe molto apprezzato perché ho passato innumerevoli ore a cercare di farlo funzionare, ma ho fallito terribilmente.

+0

... si dovrebbe davvero inviare qualunque sia funzionato al meglio. È incredibilmente utile. Se nessuno di loro ha funzionato a tutti, la prego di precisare, perché anche questa è un'informazione utile –

+0

@QPaysTaxes: nessuno di questi ha funzionato affatto. Come ho affermato nella domanda. Mi dispiace se sembra un problema da fare a casa, ma onestamente ho provato così tanto a farlo da solo ... e ho fallito. –

+0

@AlliterativeAlice: È davvero un tavolo annidato?Non ne sono più sicuro. –

risposta

1

A giudicare dal vostro array, questo potrebbe essere qualcosa di simile a quello che stai cercando:

<table border="1"> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Subject</th> 
      <th>Test1 Marks</th> 
      <th>Test2 Marks</th> 
      <th>Total Marks</th> 
      <th>Status</th> 
      <th>Percentage</th> 
      <th>Pass Count</th>  
      <th>Total Percentage</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php foreach($arr as $name => $subjects): ?> 
      <?php $i = 0; ?> 
      <?php foreach($subjects as $subjectName => $subject): ?> 
       <?php if (is_array($subject)): ?> 
        <tr> 
         <?php if ($i === 0): ?> 
          <td rowspan="8"><?php echo $name; ?></td> 
         <?php endif; ?> 
         <td><?php echo $subjectName; ?></td> 
         <td><?php echo $subject['test1']; ?></td> 
         <td><?php echo $subject['test2']; ?></td> 
         <td><?php echo $subject['total']; ?></td> 
         <td><?php echo $subject['status']; ?></td> 
         <td><?php echo $subject['percentage']; ?></td>  
         <?php if ($i === 0): ?> 
          <td rowspan="8"><?php echo $subjects['pass_count']; ?></td> 
          <td rowspan="8"><?php echo $subjects['gross_percentage']; ?></td> 
         <?php endif; ?> 
        </tr> 
       <?php endif; ?> 
       <?php $i++; ?> 
      <?php endforeach; ?> 
     <?php endforeach; ?> 
    </tbody> 
</table> 
2

Prova questo fuori

<table border="1"> 
    <thead> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Subject</th> 
      <th>Test1 Marks</th> 
      <th>Test2 Marks</th> 
      <th>Total Marks</th> 
      <th>Status</th> 
      <th>Percentage</th> 
      <th>Pass Count</th>  
      <th>Total Percentage</th> 
     </tr> 
    </thead> 
    <tbody> 

     </tr> 
     <?php 
     $numberOfSubjects = 3; //I used 3 subjects. You should change this to 8 for your data. 
     foreach ($data as $student => $info) { 
      echo "<tr><td rowspan=$numberOfSubjects />$student</td>"; 

      //flag to let the inner loop the tr has been drawn for the first row 
      $firstRow = true; 
      foreach ($info as $key => $value) { 

       //we only want subject info 
       if (strpos($key, "subject") === 0) { 
        if (!$firstRow) { 
         echo "<tr>"; 
        } //else we already drew it 

        //its a subject so 
        echo "<td>$key</td>"; 
        echo "<td>{$value['test1']}</td>"; 
        echo "<td>{$value['test2']}</td>"; 
        echo "<td>{$value['total']}</td>"; 
        echo "<td>{$value['status']}</td>"; 
        echo "<td>{$value['percentage']}</td>"; 

        //only draw total for the first row 
        if ($firstRow) { 
         echo "<td rowspan=$numberOfSubjects>{$info['pass_count']}</td>"; 
         echo "<td rowspan=$numberOfSubjects>{$info['gross_percentage']}</td>"; 
        } 
        //close the row 
        echo "</tr>"; 
        $firstRow = false; 
       } 
      } 
     } 
     ?> 
    </tbody> 
</table> 

Ecco l'output:

enter image description here

Si basa su un set di dati campione che ho costruito dalla descrizione, incluso bel ow per completezza:

<?php 
$data = array(
    "Ron" => Array 
     (
     "subject1" => Array 
      (
      "test1" => 47 
      , "test2" => 86 
      , "total" => 133 
      , "percentage" => 88.67 
      , "status" => Pass 
     ) 
     , "pass_count" => 8 
     , "fail_count" => 0 
     , "gross_total" => 963 
     , "gross_percentage" => 80.25, 
     "subject2" => Array 
      (
      "test1" => 47 
      , "test2" => 86 
      , "total" => 133 
      , "percentage" => 88.67 
      , "status" => Pass 
     ) 
     , "subject3" => Array 
      (
      "test1" => 48 
      , "test2" => 86 
      , "total" => 134 
      , "percentage" => 89.33 
      , "status" => Pass 
     ) 
     , "pass_count" => 8 
     , "fail_count" => 0 
     , "gross_total" => 900 
     , "gross_percentage" => 75.50 
    ), 
    "John" => Array 
     (
     "subject1" => Array 
      (
      "test1" => 47 
      , "test2" => 86 
      , "total" => 133 
      , "percentage" => 88.67 
      , "status" => Pass 
     ) 
     , "pass_count" => 8 
     , "fail_count" => 0 
     , "gross_total" => 963 
     , "gross_percentage" => 80.25, 
     "subject2" => Array 
      (
      "test1" => 47 
      , "test2" => 86 
      , "total" => 133 
      , "percentage" => 88.67 
      , "status" => Pass 
     ) 
     , "subject3" => Array 
      (
      "test1" => 48 
      , "test2" => 86 
      , "total" => 134 
      , "percentage" => 89.33 
      , "status" => Pass 
     ) 
     , "pass_count" => 8 
     , "fail_count" => 0 
     , "gross_total" => 963 
     , "gross_percentage" => 80.25 
    ) 
); 
3

Non proprio la stessa uscita, ma qui è un approccio ricorsivo che dovrebbe gestire qualsiasi profondità di matrici nidificate ...

<?php 

$data = Array (
    "Ron" => Array (
      "subject1" => Array (
       "tests" => Array (
        "test1" => 47, 
        "test2" => 86, 
        "total" => 133, 
        "percentage" => 88.67, 
        "status" => "Pass", 
       ), 
       "pass_count" => 8, 
       "fail_count" => 0, 
       "gross_total" => 963, 
       "gross_percentage" => 80.25, 
      ), 


      "subject8" => Array (
       "tests" => Array (
        "test1" => 48, 
        "test2" => 86, 
        "total" => 134, 
        "percentage" => 89.33, 
        "status" => "Pass", 
       ), 
       "pass_count" => 8, 
       "fail_count" => 0, 
       "gross_total" => 900, 
       "gross_percentage" => 75.50, 

      ), 
    ), 

    "John" => Array (
      "subject1" => Array ( 
       "tests" => Array (
        "test1" => 39, 
        "test2" => 72, 
        "total" => 111, 
        "percentage" => 74, 
        "status" => "Pass", 
       ), 
       "pass_count" => 8, 
       "fail_count" => 0, 
       "gross_total" => 963, 
       "gross_percentage" => 80.25, 

      ), 

      "subject8" => Array (
       "tests" => Array (
        "test1" => 39, 
        "test2" => 75, 
        "total" => 114, 
        "percentage" => 76, 
        "status" => "Pass", 

       ), 
       "pass_count" => 8, 
       "fail_count" => 0, 
       "gross_total" => 846, 
       "gross_percentage" => 70.5, 
      ), 
     ), 
); 


print_r($data); 
$table = table_cell($data); 
echo $table; 


function table_cell($data) { 

    $return = "<table border='1'>"; 
    foreach ($data as $key => $value) { 
    $return .= "<tr><td>$key</td><td>"; 
    if (is_array($value)) { 
     $return .= table_cell($value); 
    } else { 
     $return .= $value; 
    } 
    $return .= "</td><tr>"; 
    } 
    $return .= "</tr></table>"; 
    return($return); 

} 

e il tavolo sembra niente come la richiesta, ma. .. era un excersize interessante ...

table output

+0

A chi ha risposto questa risposta, GRAZIE! Non riuscivo a ricordare quale domanda ho risposto con questo codice, e ne ho bisogno :-) – dbinns66

Problemi correlati