2017-08-08 74 views
0

這次我將一些問題傳遞給我已經創建的html表中。需要將數組中的值插入到html表中

這是我到目前爲止的代碼:

<table id="FinalTable"> 
    <tr> 
     <td align="center">FECHA</td> 
     <th colspan="12" align="center">EVENTOS</th> 
    </tr> 
    <tr> 
     <th></th> 
     <td align="center">HORA INICIO</td> 
     <td align="center">SUMMARY</td> 
     <td align="center">DESCRIPTION</td> 
     <td align="center">LOCATION</td> 
     <td align="center">CREATED</td> 
     <td align="center">CREATOR_EMAIL</td> 
     <td align="center">STARTDATE</td> 
     <td align="center">START_DATE</td> 
     <td align="center">START_DATETIME</td> 
     <td align="center">START_TIMEZONE</td> 
     <td align="center">END_DATE</td> 
     <td align="center">END_DATETIME</td> 
     <td align="center">END_TIMEZONE</td> 
    </tr> 
    <tr> 
     <td align="center">(First date and so on)</td> 
     <td align="center">(Events detalized)</td> 
    </tr> 
</table> 
<br> 

<script type="text/javascript" language="javascript"> 
    document.write("<br><br><br>It's the end of the world as we know it!</br>  </br></br>"); 


    var ar = <?php echo json_encode($newarray) ?>; 
    for (var i = 0; i < ar.length; i++) { 
     for (var j = 0; j < ar[i]['Eventos'].length; j++) { 
      if(ar[i]['Eventos'][j].isArray !== 'undefined'){ 
       document.write(
         '<td><tr> ' + "fecha_inicio" + ' ' + ar[i]['Eventos'][j]['fecha_inicio'] + '</tr></td>' + 
         ' ' + "hora_inicio" + ' ' + ar[i]['Eventos'][j]['hora_inicio'] + 
         ' ' + "summary" + ' ' + ar[i]['Eventos'][j]['summary'] + 
         ' ' + "description" + ' ' + ar[i]['Eventos'][j]['description'] + 
         ' ' + "location" + ' ' + ar[i]['Eventos'][j]['location'] + 
         ' ' + "created" + ' ' + ar[i]['Eventos'][j]['created'] + 
         ' ' + "creator_email" + ' ' + ar[i]['Eventos'][j]['creator_email'] + 
         ' ' + "startdate" + ' ' + ar[i]['Eventos'][j]['startdate'] + 
         ' ' + "start_date" + ' ' + ar[i]['Eventos'][j]['start_date'] + 
         ' ' + "start_datetime" + ' ' + ar[i]['Eventos'][j]['start_datetime'] + 
         ' ' + "start_timezone" + ' ' + ar[i]['Eventos'][j]['start_timezone'] + 
         ' ' + "end_date" + ' ' + ar[i]['Eventos'][j]['end_date'] + 
         ' ' + "end_datetime" + ' ' + ar[i]['Eventos'][j]['end_datetime'] + 
         ' ' + "end_timezone" + ' ' + ar[i]['Eventos'][j]['end_timezone']) 
      } 
     } 
    } 

所以,基本上我不知道如何插入一切document.write後進入該表的去字段中的值。

請有人幫助我嗎?

非常感謝最親愛的堆垛機!

+1

這與PHP有什麼關係? – Qirel

+0

爲什麼不用PHP填充表格而不是Javascript? – Barmar

+0

一些明智的代碼縮進將是一個好主意。 它可以幫助我們閱讀代碼,更重要的是它可以幫助您調試代碼**。 您可能會被要求在幾周/幾個月內修改此代碼,最後您會感謝我。 – GrumpyCrouton

回答

1

表格行必須在</table>之前。 <td>需要在<tr>之內,但你有相反的方向。很多你的價值都不在<td>之內。

你應該在PHP中完成整個事情。

<table id="FinalTable"> 
    <tr> 
     <td align="center">FECHA</td> 
     <th colspan="12" align="center">EVENTOS</th> 
    </tr> 
    <tr> 
     <th></th> 
     <td align="center">HORA INICIO</td> 
     <td align="center">SUMMARY</td> 
     <td align="center">DESCRIPTION</td> 
     <td align="center">LOCATION</td> 
     <td align="center">CREATED</td> 
     <td align="center">CREATOR_EMAIL</td> 
     <td align="center">STARTDATE</td> 
     <td align="center">START_DATE</td> 
     <td align="center">START_DATETIME</td> 
     <td align="center">START_TIMEZONE</td> 
     <td align="center">END_DATE</td> 
     <td align="center">END_DATETIME</td> 
     <td align="center">END_TIMEZONE</td> 
    </tr> 
    <tr> 
     <td align="center">(First date and so on)</td> 
     <td align="center">(Events detalized)</td> 
    </tr> 
<?php 
foreach ($newarray as $el) { 
    foreach ($el['Eventos'] as $event) { 
     if (is_array($event)) { 
      echo "<tr>"; 
      echo "<td>{$event['fecha_inicio']}</td>"; 
      echo "<td>{$event['hora_inicio']}</td>"; 
      // repeat for all fields in $event 
      echo "</tr>"; 
     } 
    } 
} 
?> 
</table> 
+0

哇!非常感謝朋友!你做得這麼簡單,我的代碼越來越複雜了! 最好的問候! –