2015-05-02 19 views
5

Jest to prosty problem, który próbuję rozwiązać przez kilka godzin. Mam tablicę zawierającą informacje o kilku studentach i ocenach, które zdobyli w testach. W sumie jest 8 przedmiotów, z których każdy ma 5 parametrów.Wyświetlanie zagnieżdżonej tablicy w tabeli HTML

Tablica wygląda jak poniżej:

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 
     ) 

) 

muszę wyświetlać następujące ładnie sformatowany w tabeli HTML (mam zamiar użyć Bootstrap), ale nie mogę dla życia mnie rysunek jak poprawnie zagnieździć tabelę przy użyciu PHP.

To znaczników HTML, który obecnie mam: http://jsfiddle.net/9y910uvp/

To daje następujący wynik:

Który jest w porządku.

rzeczywisty problem

używam PHP, aby wyświetlić zawartość z tablicy. Nie wiem, jak wyświetlić wartości dla zagnieżdżonych sekcji <tr> i <td>.

Jak przejść przez tablicę i wyświetlić zawartość poprawnie? (Podsumowałem dotychczasowe próby, ale wszystkie są głupie i nie działają, więc nie publikuję).

Przykład byłby bardzo doceniany, ponieważ spędziłem niezliczone godziny próbując sprawić, żeby to zadziałało, ale zawiodło strasznie.

+0

... Powinieneś pisać cokolwiek pracował najlepiej. Jest niezmiernie pomocny. Jeśli żaden z nich nie działa _at all_, proszę o tym poinformować, ponieważ nawet to jest przydatne informacje –

+0

@QPaysTaxes: Żaden z nich w ogóle nie działał. Jak już wspomniałem w pytaniu. Przykro mi, jeśli to wygląda na problem z pracą domową, ale szczerze, próbowałem zrobić to sam ... i nie udało mi się. –

+0

@AlliterativeAlice: Czy to naprawdę jest tabela zagnieżdżona?Nie jestem już pewien. –

Odpowiedz

1

Sądząc po twojej tablicy, to może być coś, czego szukasz:

<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

Spróbuj tego

<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> 

Oto wynik:

enter image description here

Jest oparty na przykładowym zbiorze danych, który zbudowałem na podstawie Twojego opisu, w zestawie bel ow dla kompletności:

<?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

nie to samo wyjście, ale tutaj jest rekurencyjny podejście, które powinno obsługiwać dowolną głębokość zagnieżdżonych tablic ...

<?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); 

} 

i tabela nie przypomina wnioskowana ale. .. to był ciekawy excersize ...

table output

+0

Dla każdego, kto napotkał tę odpowiedź, DZIĘKUJĘ! Nie mogłem sobie przypomnieć, na jakie pytanie odpowiedziałem tym kodem i potrzebuję go :-) – dbinns66

Powiązane problemy