Jump to content

Adding Column to a table with values by ID


Andarvi282

Recommended Posts

<?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++;
}
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

$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. 

Link to comment
Share on other sites

$data->members is a string

No it is not.

 

$result is an associative array containing data from $data->members and a number.

:facewall:

 

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>';
}
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.