0

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!

5
  • 1
    How is this related to PHP? Commented Aug 8, 2017 at 16:11
  • Why don't you fill in the table in PHP rather than Javascript? Commented Aug 8, 2017 at 16:13
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help you debug your code. You may be asked to amend this code in a few weeks/months and you will thank me in the end. Commented Aug 8, 2017 at 16:13
  • Because I organize a json file first, then I have to put all that data into the table. Commented Aug 8, 2017 at 16:13
  • @GrumpyCrouton Thanks for that, I will take care of it from now to on!! Commented Aug 8, 2017 at 16:16

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Wow!!!! Thanks a lot pal!! You made it so simple, I was getting complicated on my code!! Best regards!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.