首页 > 解决方案 > PHP 函数的 HTML

问题描述

我使用准备好的语句来收集数据。我正在尝试使用 fetch_array 函数以有组织的方式显示该数据。是否可以将 html 表插入到依赖于准备好的语句的 php 函数中?

我读过使用 HEREDOC,但我不知道用什么来代替变量。我也尝试为表格创建另一个文档,但有同样的问题。

这是我正在使用的功能。

function showProfile($user) {
    global $connection;

    $query = "SELECT * FROM profiles WHERE user='$user'";

    $result = $connection->query($query);
    /* associative array */
    $row = $result->fetch_array(MYSQLI_ASSOC);
    printf(
        "%s <br> %s\n",
        $row["forename"],
        $row["surname"]
    ) . "<br style='clear:left;'><br>";
}

这是我要使用的表

<table width="398" border="0" align="center" cellpadding="0">
  <tr>
    <td height="26" colspan="2">Your Profile Information </td>
  </tr>
  <tr>
    <td width="82" valign="top"><div align="left">FirstName:</div></td>
    <td width="165" valign="top"><?php echo $forename ?></td>
  </tr>
  <tr>
    <td valign="top"><div align="left">LastName:</div></td>
    <td valign="top"><?php echo $surname ?></td>
  </tr>
</table>

我可以使用该功能显示我的数据,但是,我想以更有条理的方式显示它。

标签: phphtmlsql

解决方案


而不是 printf(),你可以回显

echo "<table width='398'> <-- check here the quote marks so the string doesnt break
  <tr>
    <td>".$row['forename']."</td> <- then concat all the variables you want like this
  </tr>
</table>";

推荐阅读