0

我正在尝试执行以下操作。这不起作用。但是我怎样才能让它工作呢?我想用一个 php 循环填充一个表。

echo "<table>";
echo    "<thead>";
echo        "<tr>";
echo            "<th scope=\"col\"> Column1 </th>";
echo            "<th scope=\"col\"> Column2 </th>";
echo            "<th scope=\"col\"> Column3 </th>";
echo        "</tr>";
echo    "</thead>";
echo    <tbody>
echo        <tr>
echo            <?php while($row = MySQL_fetch_array($result)) { ?>
echo                <?php $link = $row['mirlyn'];?>
echo                <td><?php echo(htmlentities($row['data1'])); ?></td>
echo                <td><?php echo(htmlentities($row['data2'])); ?></td>
echo                <td><?php echo(htmlentities($row['data3'])); ?></td>
echo            <?php}?>
echo        </tr>               
echo    </tbody>
echo "</table>";
4

5 回答 5

3

拆分 php 并像往常一样编写 HTML。当你需要在 PHP 中输​​出一些东西时,打开一个标签并做你需要做的事情

IE

<table>
    <tr><?php echo $something ?></tr>
</table>
于 2012-12-13T22:22:41.460 回答
1

使用下面的代码它应该工作!

      <table>
        <thead>
            <tr>
                <th scope="col"> Column1 </th>
                <th scope="col"> Column2 </th>
                <th scope="col"> Column3 </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <?php while ($row = MySQL_fetch_array($result)): ?> 
                    <?php $link = $row['mirlyn']; ?>
                    <td><?php echo(htmlentities($row['data1'])); ?></td>
                    <td><?php echo(htmlentities($row['data2'])); ?></td>
                    <td><?php echo(htmlentities($row['data3'])); ?></td>
                <?php endwhile; ?>
            </tr>               
        </tbody>
    </table>
于 2012-12-13T22:24:29.457 回答
0

如果您想构建一个稍后回显的 HTML 字符串,您可以使用这样的字符串连接-

$html = '<table>';

while($row = MySQL_fetch_array($result)){
  $html .= '<tr>';
  $html .= '<td>' . $someData . '</td>';
  $html .= '</tr>';
}

$html .= '</table>';
echo $html;

您只需要将最终字符串回显一次,但每次迭代另一个结果时,您只需将其附加到字符串的末尾即可。在 PHP 中,字符串连接是用点字符完成的。

stringOne.stringTwo

会给你

stringOnestringTwo
于 2012-12-13T22:24:36.457 回答
-1

您可以使用串联来执行此操作:

echo "               <td>" . htmlentities($row['data1']) . "</td>";

或者,您主要输出 HTML 并仅在必要时放入 PHP。

于 2012-12-13T22:22:36.007 回答
-2

这是一个示例数据库查询表

$accounts = $currentMember->retrieve_all_accounts(); //Same error in instructor source file as I had in last assignment. 

    /* Loop through Accounts */
    while($account = mysqli_fetch_assoc($accounts)) {
        // Retrieve Balance
        $bankaccount = new Bankaccount($account['BankAccountID']);
        $bankaccount->connection = $conn;
        $balance = mysqli_fetch_assoc($bankaccount->retrieve_current_balance());

        echo '<tr>' . "\n";
        echo "\t" . '<td class="account_number">' . $account['BankAccountID'] . '</td>' . "\n";
        echo "\t" . '<td class="account_balance">$' . number_format($balance['CurrentBalance'], 2) . '</td>' . "\n";
        echo '</tr>' . "\n";
    }
于 2012-12-13T22:23:24.570 回答