2017-12-22 3 views
-3

Ich habe eine assoziative Array wie folgt, ich möchte eine Tabelle mit diesen Daten, wie Frühstück, Snacks, Mittagessen, Abendessen, Abendessen in Zeile das Folgende ist der Code, den ich versucht habe, aber ich wie folgt ist stecken, wo die Tabellenzeile zu brechen, weil, wenn das Array mehr als ein enthält will Tabelle ist Einzelteil assoziatives Array druckenDrucken assoziative Array zu Tabelle

*--------------------------------------------------* 
| **Breakfast Snacks Lunch Supper Dinner** | 
| test   test   test  testfrom 
|testfrom 
*--------------------------------------------------* 

Array Ausgang

Array 
(
    [meal_plan_id] => 17 
    [calorie_limit] => 1 
    [total_calorie] => 0 
    [date] => 2017-12-29 
    [meal_plan] => Array 
     (
      [0] => Array 
       (
        [meal_type] => bf 
        [label] => Breakfast 
        [calorie_limit] => 30 
        [total_calorie] => 0 
        [data] => Array 
         (
          [0] => Array 
           (
            [id] => 107 
            [label] => test 
            [quantity] => 10 
            [unit] => g 
            [status] => bf 
           ) 

          [1] => Array 
           (
            [id] => 109 
            [label] => testfrom 
            [quantity] => 12 
            [unit] => g 
           ) 

         ) 

       ) 

      [1] => Array 
       (
        [meal_type] => sn 
        [label] => Snacks 
        [calorie_limit] => 10 
        [total_calorie] => 0 
        [data] => Array 
         (
          [0] => Array 
           (
            [id] => 108 
            [label] => test 
            [quantity] => 121 
            [unit] => g 
           ) 

         ) 

       ) 

      [2] => Array 
       (
        [meal_type] => lu 
        [label] => Lunch 
        [calorie_limit] => 20 
        [total_calorie] => 0 
        [data] => Array 
         (
          [0] => Array 
           (
            [status] => su 
           ) 

         ) 

       ) 

      [3] => Array 
       (
        [meal_type] => su 
        [label] => Supper 
        [calorie_limit] => 30 
        [total_calorie] => 0 
        [data] => Array 
         (
          [0] => Array 
           (
            [status] => sn 
           ) 

          [1] => Array 
           (
            [id] => 116 
            [label] => test 
            [quantity] => 200 
            [unit] => oz 
           ) 

         ) 

       ) 

      [4] => Array 
       (
        [meal_type] => dn 
        [label] => Dinner 
        [calorie_limit] => 20 
        [total_calorie] => 0 
        [data] => Array 
         (
          [0] => Array 
           (
            [id] => 113 
            [label] => test500 
            [quantity] => 20 
            [unit] => oz 
            [status] => dn 
           ) 

         ) 

       ) 

     ) 

) 

unten ist der Code, den ich habe versucht

// $daily_meals = show_daily_meals() // contains the array 
    if(!empty($daily_meals['meal_plan'])){ 

     echo '<tr>'; 
     foreach($daily_meals['meal_plan'] as $meal_plan){  

      foreach ($meal_plan['data'] as $data){ 

       if(!empty($data['id'])) 
        echo '<td class="'.$meal_type.'" data-meals-id="'.$data['id'].'"><span class="left">'.$data['label'].'</span> <span class="right">'.$data['quantity'].' <a class="delete_row"><i class="fa fa-trash" aria-hidden="true"></i></a></span><div class="row_loader"></div></td>'; 

       else echo '<td></td>'; 
      } 

      $i++; 
      if($i%5 == 0) echo '</tr>'; 
     } 

    } 
+1

Wo ist der Code? Zeigen Sie auch die gewünschte Ausgabe an. – Nic3500

+1

Sie wollen nur Etikett ..? – GYaN

+1

@ Nic3500, ich habe die Frage bearbeitet, bitte überprüfen Sie –

Antwort

0

Versuchen in einem Array alle Spaltennamen zu speichern und eine Tabelle mit ihm erstellen:

foreach($array as $row) { 
    $column[$i]['label'] = $row['label']; 
    $column[$i]['data'] = $this->getdata($row['data']); 
    $i++; 
} 

function getdata($array) { 
    $data = ''; 

    foreach($array as $row) { 
     $data. = $row['label'].','; 
    } 

    return $data; 
} 

eine Tabelle mit den Spaltennamen erstellen und versuchen, Einfügen von Daten eine Schleife in Bezug auf

1
einfügen mit

Hier ist Ihre Lösung

Eingang

<?php 
    $array = array(
     array(
      'meal_type' => 'bf', 
      'label' => 'Breakfast', 
      'calorie_limit' => 30, 
      'total_calorie' => 0, 
      'data' => array(
       array(
        'id' => 107, 
        'label' => 'test', 
        'quantity' => 10, 
        'unit' => 'g', 
        'status' => 'bf' 
       ), 
       array(
        'id' => 109, 
        'label' => 'testfrom', 
        'quantity' => 12, 
        'unit' => 'g' 
       ) 
      ) 
     ), 
     array(
      'meal_type' => 'sn', 
      'label' => 'Snacks', 
      'calorie_limit' => 10, 
      'total_calorie' => 0, 
      'data' => array(
       array(
        'id' => 108, 
        'label' => 'test', 
        'quantity' => 121, 
        'unit' => 'g' 
       ) 
      ) 
     ), 
     array(
      'meal_type' => 'lu', 
      'label' => 'Lunch', 
      'calorie_limit' => 20, 
      'total_calorie' => 0, 
      'data' => array(array('status' => 'su')) 
     ), 
     array(
      'meal_type' => 'su', 
      'label' => 'Supper', 
      'calorie_limit' => 30, 
      'total_calorie' => 0, 
      'data' => array(
       array('status' => 'sn'), 
       array(
        'id' => 116, 
        'label' => 'test', 
        'quantity' => 200, 
        'unit' => 'oz' 
       ), 
      ) 
     ), 
     array(
      'meal_type' => 'dn', 
      'label' => 'Dinner', 
      'calorie_limit' => 20, 
      'total_calorie' => 0, 
      'data' => array(
       array(
        'id' => 113, 
        'label' => 'test500', 
        'quantity' => 20, 
        'unit' => 'oz', 
        'status' => 'dn' 
       ) 
      ) 
     ) 
    ); 

Lösung

1. foreach für Eltern-Array. Dann zweite für sein Kind ..

foreach($array as $r){ 
     $$r['label'] = '<table width="100%">'; 
     foreach($r['data'] as $s){ 
      if(isset($s['label']))$$r['label'] .= '<tr><td align="center">'.$s['label'].'</td></tr>'; // The if condition check weather the label is exist or not if yes then add that in particular label's table 
     } 
     $$r['label'] .= '</table>'; 
    } 
    echo ' 
    <table width="100%" border="1"> 
     <thead> 
      <tr> 
       <th>Breakfast</th> 
       <th>Snacks</th> 
       <th>Lunch</th> 
       <th>Supper</th> 
       <th>Dinner</th> 
      </tr> 
     </thead> 

Hier werden alle Etiketten canverted in variable durch $$r['label']. Und alle haben ihre eigene Tabelle Nun fügen wir alle diese Tabellen in die Haupttabelle ein, um die Ausgabe zu erhalten.

 <tbody> 
      <tr> 
       <td>'.$Breakfast.'</td> 
       <td>'.$Snacks.'</td> 
       <td>'.$Lunch.'</td> 
       <td>'.$Supper.'</td> 
       <td>'.$Dinner.'</td> 
      </tr> 
     </tbody> 

    </table>'; 
    //?// echo "<pre>";print_r($array); 

    ?> 

Ausgabe

Output

+0

Gute Arbeit, aber Sie sollten einige Kommentare hinzufügen. – Andreas

+0

aber die Tabellendaten sind leer –

+0

Tabellendaten sind leer ...? was meinst du damit ... @ VidyaL – GYaN