以HTML格式显示SQL数据

时间:2016-11-19 13:10:06

标签: php html sql connectivity

我在PHP中编写了以下代码以从mysql表中获取详细信息,但我不知道如何以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">&nbsp;&nbsp;&nbsp;
    <input type="reset" value="Reset" name="B2"></p>
</form>

我是PHP连接的新手,这就是为什么有点困惑。将非常感谢帮助。谢谢

2 个答案:

答案 0 :(得分:0)

首先,您需要包含HTML表单的页面的.php扩展名。

从数据库中获取数据后,您可以将其设置为表单元素,如

<input type="text" name="T1" size="20" value="<?php echo $row['dsl'];?>">

答案 1 :(得分:0)

我建议使用类似于PHPTal的模板工具。您将html页面编写为.xhtml文件,其中包含tal:标签,模板引擎使用这些标签插入您的php值。 Install instructionsexample of use

使用模板引擎的好处是你可以从脚本中删除所有html内容,并将显示逻辑保留在xhtml文件中。 php代码只是收集数据并将其分配给模板知道的标签。

您可以将所有行作为数组获取,并将其分配给php中模板对象中的变量名称。然后在xhtml文件中使用该变量名称来重复表(或任何其他元素)中的行。

腓:

<?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 rows部分,其中包含重复每一行,插入元素内容,设置属性以及使用条件语句仅显示索引为1的行的输入字段的示例。重复方法可用于选择和条件检查中的选项设置所选属性。 Phptal需要严格的xhtml结构xhtml中的任何错误,页面将返回错误标识出现问题。

<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>