Marco Jaimes
Marco Jaimes

Reputation: 11

Need to insert values from an array into a html table

This time I'm having some issues for passing values into a html table that I've already created.

This is my code so far:

<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'])
            }
        }
    }

So, basically I do not know how to insert all the values that comes after the document.write into de fields of the table.

Could please somebody help me?

Thanks a lot dearest stackers!

Upvotes: 0

Views: 58

Answers (1)

Barmar
Barmar

Reputation: 780724

The table rows need to be before </table>. <td> needs to be inside <tr>, but you have it the other way around. And lots of your values aren't in <td> at all.

You should just do the entire thing in 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>

Upvotes: 1

Related Questions