CSS-Regex Posted January 6 Share Posted January 6 I am trying to display data from the php file to be in the correct headers, yet its going downwards instead of going across The php file <?php $a = array(11,24,3,44,58,16); // var_dump($a); // print_r($a) echo json_encode($a); ?> The Javascript snippet if (ajaxReturn(xhr) == true) { const response = JSON.parse(xhr.responseText); // console.log(xhr.responseText) // document.getElementById('read').innerHTML = response let table = '' console.log(table) for(let i = 0; i < response.length; i++){ let num = response[i] console.log(num) // concantenate with table elements table += '<tr>' // table += '<td>' + num + '</td><td>Some random text</td>' table += '<td>' + num + '</td>' table += '</tr>' } // add to the table html elements and displays data document.getElementById('data').innerHTML = table } The html table <div id="read"> <table> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> </tr> <tbody id="data"></tbody> </table> </div> Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted January 6 Solution Share Posted January 6 the code is doing exactly what it was written to do, build a <tr><td>num</td></tr> for each number. if you want to produce a single <tr></tr>, you would need to add the opening <tr> before the start of the looping and add the closing </tr> after the end of the looping. 2 Quote Link to comment Share on other sites More sharing options...
requinix Posted January 6 Share Posted January 6 A <tr> is a table row. A <td> is a table cell. If you want one row with multiple cells then you need one <tr> and multiple <td>s. 1 Quote Link to comment Share on other sites More sharing options...
CSS-Regex Posted January 6 Author Share Posted January 6 (edited) Ah cheers, its now been corrected. You've both solved it, but I'm assuming I can only select one for the solution. Thanks again Edited January 6 by CSS-Regex Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.