如何在 sql 中的 table 的每一行之后添加一个按钮?
How to add a button after each row of a table from sql?
我有一个 table 包含三列日期、标题、消息。
我将在每个 table 中仅显示日期和标题,并希望在每一行之后添加一个单独的按钮,然后我将显示该消息。
我试过了
<?php
echo "<table style='border: solid 3px aqua;'>";
echo "<tr><th>Circular/Notice</th><th>DateGenerated</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid red;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "er";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT title ,DateGenerated FROM messages");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo '<input type ="button" value = "View"/>';
echo "</table>";
?>
我得到的结果只是 table 顶部的一个按钮。
<input>
需要在 <td>
内,<td>
需要在 <tr>
内
HTML table 中不在行和单元格中的任何内容都会在 table.
之外呈现
要为每一行设置一个按钮,您需要将代码放入 TableRows 循环中。我建议编辑 endChildren()
函数,如下所示:
function endChildren() {
echo '<td><input type="button" value = "View"/></td>';
echo "</tr>" . "\n";
}
我有一个 table 包含三列日期、标题、消息。
我将在每个 table 中仅显示日期和标题,并希望在每一行之后添加一个单独的按钮,然后我将显示该消息。
我试过了
<?php
echo "<table style='border: solid 3px aqua;'>";
echo "<tr><th>Circular/Notice</th><th>DateGenerated</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid red;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "er";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT title ,DateGenerated FROM messages");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo '<input type ="button" value = "View"/>';
echo "</table>";
?>
我得到的结果只是 table 顶部的一个按钮。
<input>
需要在 <td>
内,<td>
需要在 <tr>
HTML table 中不在行和单元格中的任何内容都会在 table.
之外呈现要为每一行设置一个按钮,您需要将代码放入 TableRows 循环中。我建议编辑 endChildren()
函数,如下所示:
function endChildren() {
echo '<td><input type="button" value = "View"/></td>';
echo "</tr>" . "\n";
}