php代码里面的echo ...怎么管理呢?

时间:2012-12-13 22:20:51

标签: php html mysql

我正在尝试执行以下操作。这不起作用。但是我怎样才能让它发挥作用?我想用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>";

5 个答案:

答案 0 :(得分:3)

突破php并像往常一样编写HTML。当您需要在PHP中输出内容时,打开标签并执行您需要执行的操作

<table>
    <tr><?php echo $something ?></tr>
</table>

答案 1 :(得分: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>

答案 2 :(得分:0)

如果您想构建一个HTML字符串以便稍后回显,可以使用string concatenation这样的内容 -

$html = '<table>';

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

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

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

stringOne.stringTwo

会给你

stringOnestringTwo

答案 3 :(得分:-1)

您可以使用连接来执行此操作:

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

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

答案 4 :(得分:-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";
    }