以 HTML 形式显示 SQL 数据
Displaying SQL data in HTML form
我在 PHP 中编写了以下代码以从 mysql table 中获取详细信息,但我不知道如何以 HTML 形式显示它们。
<?php
$servername = "localhost";
$username = "root";
$password = "icsk";
$dbname = "yusuf";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT dsl, Fname, Lname, Cid, pack FROM homereg";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['dsl']. " " . $row['Fname']. " " . $row['Lname']. " " . $row['cid']. " " . $row['pack'] ."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
上面的代码借助echo在网页上显示数据,但我希望它以表格形式显示。
HTML 表单有以下代码:
<form method="POST" name = "frm" action="homerenew.php">
<div align="center">
<table border="1" width="425">
<tr>
<td width="223"><font face="Georgia"><b>Subscription Number</b></font></td>
<td width="186"><input type="text" name="T1" size="20"></td>
</tr>
<tr>
<td width="223"><font face="Georgia"><b>Your Current Pack</b></font></td>
<td width="186"><input type="text" name="T2" size="20"></td>
</tr>
<tr>
<td width="223"><font face="Georgia"><b>Renewal Options</b></font></td>
<td width="186"><select size="1" name="D1">
<option value = "1 Month">1 Month</option>
<option value = "2 Months">6 Months</option>
<option value = "1 Year>1 Year</option>
</select></td>
</tr>
<tr>
<td width="223"><font face="Georgia"><b>Balance Payable</b></font></td>
<td width="186"><input type="text" name="T3" size="20"></td>
</tr>
</table>
</div>
<p align="center"><input type="submit" value="Renew" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
我是 PHP 连接的新手,所以有点困惑。帮助将不胜感激。谢谢
首先,您需要包含 HTML 表单的页面的 .php 扩展名。
从数据库中获取数据后,您可以将其设置到表单元素中,例如
<input type="text" name="T1" size="20" value="<?php echo $row['dsl'];?>">
我建议使用类似于 PHPTal 的模板引擎。您将 html 页面编写为 .xhtml 文件,其中包含 tal:模板引擎用来插入 php 值的标签。 Install instructions, example of use.
使用模板引擎的好处是您可以从脚本中删除所有 html 内容,并将显示逻辑保留在 xhtml 文件中。 php 代码只是收集数据并将其分配给您的模板知道的标签。
您可以将所有行作为数组获取,并将其分配给php中模板对象中的变量名。然后在 xhtml 文件中使用该变量名来重复 table(或任何其他元素)中的行。
Php:
<?php
require_once 'PHPTAL.php';
$rows = getMyDBRows();
// create a new template object
$template = new PHPTAL('my_template_file.xhtml');
$template->rows = $rows;
// execute the template
try {
echo $template->execute();
}
catch (Exception $e){
echo $e;
}
Xhtml行部分以重复每一行、插入元素内容、设置属性和使用条件语句仅显示索引为1的行的输入字段为例。重复方法可用于选择中的选项和条件检查以设置所选属性。 Phptal 要求严格 xhtml 构造 xhtml 中的任何错误,页面将 return 一个错误,指出哪里有问题。
<tr tal:repeat="row rows">
<td tal:content="row/dsl"><\td>
<td tal:content="row/fname"><\td>
<td><input name="lname" tal:attributes="value row/lname, style php: repeat.row.id EQ 1?'display:inline-block':'display:none" tal:content="row/lname"></input>
......
<\tr>
我在 PHP 中编写了以下代码以从 mysql table 中获取详细信息,但我不知道如何以 HTML 形式显示它们。
<?php
$servername = "localhost";
$username = "root";
$password = "icsk";
$dbname = "yusuf";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT dsl, Fname, Lname, Cid, pack FROM homereg";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['dsl']. " " . $row['Fname']. " " . $row['Lname']. " " . $row['cid']. " " . $row['pack'] ."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
上面的代码借助echo在网页上显示数据,但我希望它以表格形式显示。 HTML 表单有以下代码:
<form method="POST" name = "frm" action="homerenew.php">
<div align="center">
<table border="1" width="425">
<tr>
<td width="223"><font face="Georgia"><b>Subscription Number</b></font></td>
<td width="186"><input type="text" name="T1" size="20"></td>
</tr>
<tr>
<td width="223"><font face="Georgia"><b>Your Current Pack</b></font></td>
<td width="186"><input type="text" name="T2" size="20"></td>
</tr>
<tr>
<td width="223"><font face="Georgia"><b>Renewal Options</b></font></td>
<td width="186"><select size="1" name="D1">
<option value = "1 Month">1 Month</option>
<option value = "2 Months">6 Months</option>
<option value = "1 Year>1 Year</option>
</select></td>
</tr>
<tr>
<td width="223"><font face="Georgia"><b>Balance Payable</b></font></td>
<td width="186"><input type="text" name="T3" size="20"></td>
</tr>
</table>
</div>
<p align="center"><input type="submit" value="Renew" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
我是 PHP 连接的新手,所以有点困惑。帮助将不胜感激。谢谢
首先,您需要包含 HTML 表单的页面的 .php 扩展名。
从数据库中获取数据后,您可以将其设置到表单元素中,例如
<input type="text" name="T1" size="20" value="<?php echo $row['dsl'];?>">
我建议使用类似于 PHPTal 的模板引擎。您将 html 页面编写为 .xhtml 文件,其中包含 tal:模板引擎用来插入 php 值的标签。 Install instructions, example of use.
使用模板引擎的好处是您可以从脚本中删除所有 html 内容,并将显示逻辑保留在 xhtml 文件中。 php 代码只是收集数据并将其分配给您的模板知道的标签。
您可以将所有行作为数组获取,并将其分配给php中模板对象中的变量名。然后在 xhtml 文件中使用该变量名来重复 table(或任何其他元素)中的行。
Php:
<?php
require_once 'PHPTAL.php';
$rows = getMyDBRows();
// create a new template object
$template = new PHPTAL('my_template_file.xhtml');
$template->rows = $rows;
// execute the template
try {
echo $template->execute();
}
catch (Exception $e){
echo $e;
}
Xhtml行部分以重复每一行、插入元素内容、设置属性和使用条件语句仅显示索引为1的行的输入字段为例。重复方法可用于选择中的选项和条件检查以设置所选属性。 Phptal 要求严格 xhtml 构造 xhtml 中的任何错误,页面将 return 一个错误,指出哪里有问题。
<tr tal:repeat="row rows">
<td tal:content="row/dsl"><\td>
<td tal:content="row/fname"><\td>
<td><input name="lname" tal:attributes="value row/lname, style php: repeat.row.id EQ 1?'display:inline-block':'display:none" tal:content="row/lname"></input>
......
<\tr>