1

I'm trying to make a page that once someone logs in, they can view the contents of the database that is associated with them in a clear, concise and organized way. Currently, I just have PHP echoing HTML and variables to make the page. i.e. I did what I knew would work.

I am new to everything programming and have done Javascript DOM manipulation so I can't see this echo thing can't being the right.

while($row = mysql_fetch_array($result))
{

echo "<tr>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['Last_Name'] . "</td>";
echo "<td>" . $row['Employee_No'] . "</td>";
echo "<td>" . $row['Job_No'] . "</td>";
echo "<td>" . $row['Job_Name'] . "</td>";
echo "</tr>";
 }
echo "</table>";

I know that technically, this will work, but I am just looking for basic information on how to do this properly, preferably in an easy to understand format.

http://php.net/manual/en/book.dom.php

I found this, it looks like it is potentially what I need, but I am not experienced enough to know and it's not the most beginner friendly documentation.

4

5 回答 5

1

如果你愿意,你也可以通过这种方式将你的 php 代码和 html 代码分开:

phpcode.php

while($row = mysql_fetch_array($result))
{
    global $firstName, $lastName;
    $firstName=$row['First_Name'];
    $lastName=$row['Last_Name'];
    include('htmlcode.php');
}

htmlcode.php

<?php global $firstName, $lastName; ?>
<tr>
<td><?php echo $firstName; ?></td>
<td><?php echo $lastName; ?></td>
<tr>

大多数情况下这不是必需的,但如果您想要更好的可读代码,在某些情况下可能会有所帮助。

于 2012-04-13T20:16:55.713 回答
1

在 PHP 中,当您创建 HTML 输出时,您不会被绑定到 DOM。因为与 javascript 不同,没有 DOM 可以与浏览器共享。相反,您可以像echo在您的问题中一样创建文本输出。这完全没问题。

于 2012-04-13T20:04:15.737 回答
0

只需echo "<table>";在 while 循环之前添加,表格应该可以正确呈现。您可能需要使用 CSS 样式来进一步美化表格

于 2012-04-13T20:08:11.543 回答
0

我总是喜欢以下,使用 pinouchon 的代码。

<table>
<?php foreach ($conn->query($query) as $row): ?>
<tr>
 <td> <?= $row['First_Name'];?></td>
 <td> <?= $row['Last_Name'];?></td>
 <td> <?= $row['Employee_No'];?></td>
 <td> <?= $row['Job_No'];?></td>
 <td> <?= $row['Job_Name'];?></td>
</tr>
<?php endforeach;?>
</table>
于 2012-04-13T20:18:45.710 回答
0

看看PHP PDO。与数据库交互时会更容易。您的代码将如下所示:

echo "<table>";
foreach ($conn->query($query) as $row) {
  echo "<tr>";
  echo "<td>" . $row['First_Name'] . "</td>";
  echo "<td>" . $row['Last_Name'] . "</td>";
  echo "<td>" . $row['Employee_No'] . "</td>";
  echo "<td>" . $row['Job_No'] . "</td>";
  echo "<td>" . $row['Job_Name'] . "</td>";
  echo "</tr>";
}
echo "</table>";
于 2012-04-13T20:08:41.597 回答