0

I would like to be pointed in the right direction on how I would go about editing data (not headings) of a table using PHP DOM Document.

I have been looking into PHP DomDocument to replace the content of "Name 1" and "Age 1" etc, with real data from a database, however I am having a few issues...

<?php

$doc = new DOMDocument();
$doc->loadHTMLFile('template.html');

$sql = 'SELECT name,
               age
        FROM db.people';

$sql = mysql_query($sql);

for($i=0; $person = mysql_fetch_assoc($sql); $i++)
{
    $doc->getElementsByTagName('td')->item($i)->nodeValue = $person['name'];
}

$doc->formatOutput = TRUE;
echo $doc->saveHTML();

?>

I would like to continue editing the above PHP code to replace place holder data with data from a database.

<table>
    <tr>
        <th>Name</th>
        <th>Age</th>
    </tr>
    <tr>
        <td>Stephanie</td>
        <td>22</td>
    </tr>
    <tr>
        <td>Martin</td>
        <td>45</td>
    </tr>
    <tr>
        <td>Sarah</td>
        <td>61</td>
    </tr>
    <tr>
        <td>Kevin</td>
        <td>12</td>
    </tr>
</table>

Can anyone point me in the right direction, and if i'm on the right track?

4

1 回答 1

0

循环内的两个分配都分配给完全相同的元素,除了顶部分配给元素“名称”,底部分配给元素“年龄”。所以年龄总是赢。

于 2013-06-01T15:32:58.250 回答