Andarvi282 Posted November 6, 2017 Share Posted November 6, 2017 (edited) <?php echo "<center><table id=\"myTbl\"><tr><th><th><center><b>Name</b></center></th><th><center><b>Role</b></center></th><th><center><b>Tag</b></center></th><th><center><b>Average Score</b></center></th>"; $i = 0; foreach ($data ->members as $person) { echo '<tr id="rows">'; echo '<td>' . $person->name . '</td>'; echo '<td>' . $person->roleName. '</td>'; echo "<td><center><a href=\"./player.php?player=" . $person->tag . " \">" . $person->tag . "</a></td>"; echo '<td id="scoreAverage' . $i . '"></td>'; $i++; echo '</tr>'; } $a = 0; foreach ($result as $key => $value) { echo '<script>document.getElementById("scoreAverage[' . $a . ']").innerHTML = "' . $value . '";</script>'; $a++; } echo '</table>'; ?> I'm trying to add an array of values to a new column within an existing table with an existing <TH> and with existing <TD>s with dynamically added IDs on the TDs. For some reason the values are not getting added to the table cells. Perhaps a 2nd set of eyes can tell me what I did wrong or guidance on a better way to do this?The main code for adding the content to the cells is shown below, this is the problem code. I posted the rest above for context. $a = 0; foreach ($result as $key => $value) { echo '<script>document.getElementById("scoreAverage[' . $a . ']").innerHTML = "' . $value . '";</script>'; $a++; } Edited November 6, 2017 by Andarvi282 Quote Link to comment Share on other sites More sharing options...
requinix Posted November 6, 2017 Share Posted November 6, 2017 Surely you recognize that using Javascript for this is not the right way to do it? One question: what are the array keys from $data->members and $result? Are they both using straight numbers? Or does one of them (or both) have special keys? Quote Link to comment Share on other sites More sharing options...
Andarvi282 Posted November 6, 2017 Author Share Posted November 6, 2017 $data->members is a string, and $result is an associative array containing data from $data->members and a number. Yeah I know javascript much better so was just going with what I thought would work. If there is a better way I'd love to know how to do it. I think my problem is with one of my foreach statments, the closest I've gotten so far, posts the first number on the last rown then adds many more rows that contain the rest, I'm not getting it placed right after an hour of playing with it I thought maybe somebody could help me understand a better way. Quote Link to comment Share on other sites More sharing options...
requinix Posted November 6, 2017 Share Posted November 6, 2017 $data->members is a stringNo it is not. $result is an associative array containing data from $data->members and a number. What I was trying to ask was... you know, forget it. Try this. foreach ($data ->members as $key => $person) { echo '<tr id="rows">'; echo '<td>' . $person->name . '</td>'; echo '<td>' . $person->roleName. '</td>'; echo "<td><center><a href=\"./player.php?player=" . $person->tag . " \">" . $person->tag . "</a></td>"; echo '<td>' . $result[$key] . '</td>'; echo '</tr>'; } Quote Link to comment Share on other sites More sharing options...
Andarvi282 Posted November 7, 2017 Author Share Posted November 7, 2017 I tried that and it still puts it on the bottom in a new row like some other things I did. I am working on redoing my coding and will see what else I can try. Quote Link to comment Share on other sites More sharing options...
requinix Posted November 7, 2017 Share Posted November 7, 2017 Well I see your header has 5 columns but your body rows have 4. But I don't know how to managed to get some rows with 10 columns. 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.