【发布时间】:2016-06-05 13:25:38
【问题描述】:
我正在使用 PHP RecursiveIteratorIterator 函数基于我的 DATABASE 生成一个动态 HTML 表,如下所示:
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td>".parent::current()."</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
下面的foreach 循环生成每个新行并将数据传递给上面的迭代器。
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
我执行 PDO 准备 $stmt,其中我从数据库中提取的第一个值始终是行的 ID。
这一切都很好。现在,我遇到的问题是我想在迭代器的current() 函数中的每个<td> 元素上生成一个HTML name 参数。我需要这个 name 参数始终是当前 RecursiveIteratorIterator 循环的第一个 key(在我的情况下,这将是每一行的 ID 列值)。
类似这样的东西,所以我可以在其每个 <td> 元素中包含 row 的 ID:
function current() {
return "<td name='".***FIRST ITERATOR KEY VALUE HERE***."'>".parent::current()."</td>";
}
编辑: 整个代码
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
/* Here I need to add the value from the ID column to the <td name="... parameter */
return "<td name='".***FIRST ITERATOR KEY VALUE HERE***."'>".parent::current()."</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
try
{
$conn = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT
id,
ddate,
day,
month,
year,
arrival,
departure,
serial_number,
time_mark,
observations
FROM ROUTS WHERE ddate = '".$datecode."'");
$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;
编辑: 我想生成的示例表:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="table-styles.css">
<title>Register</title>
</head>
<body>
<form method='post' onsubmit="return confirm('Are you sure you want to submit?');">
<table id='table-left'>
<caption>11/22/33</caption>
<caption>ROUTS</caption>
<tr>
<th>ID</th>
<th>Date</th>
<th>Day</th>
<th>Month</th>
<th>Year</th>
<th>Arrival</th>
<th>Departure</th>
<th>S/N</th>
<th>Time Mark</th>
<th>Observation</th>
</tr>
<tr>
<td name="id-1">1</td> <!-- The value of this <td> tag needs to populate in the name of each next <td> 'name' parameter for this row!-->
<td name="ddate-1">03062016</td> <!-- Here I want to add this number "1" to the name of the <td> element.-->
<td name="day-1">Day</td> <!-- Here I want to add this number "1" to the name of the <td> element.-->
<td name="month-1">Month</td> <!-- Here I want to add this number "1" to the name of the <td> element.-->
<td name="year-1">Year</td> <!-- Here I want to add this number "1" to the name of the <td> element.-->
<td name="arrival-1">Arrival</td> <!-- Here I want to add this number "1" to the name of the <td> element.-->
<td name="departure-1">Departure</td> <!-- Here I want to add this number "1" to the name of the <td> element.-->
<td name="sn-1">S/N</td> <!-- Here I want to add this number "1" to the name of the <td> element.-->
<td name="time-mark-1">Time Mark</td> <!-- Here I want to add this number "1" to the name of the <td> element.-->
<td name="observation-1">Observation</td> <!-- Here I want to add this number "1" to the name of the <td> element.-->
</tr>
<tr>
<td name="id-2">2</td> <!-- The value of this <td> tag needs to populate in the name of each next <td> 'name' parameter for this row!-->
<td name="ddate-2">03062016</td> <!-- Here I want to add this number "2" to the name of the <td> element.-->
<td name="day-2">Day</td> <!-- Here I want to add this number "2" to the name of the <td> element.-->
<td name="month-2">Month</td> <!-- Here I want to add this number "2" to the name of the <td> element.-->
<td name="year-2">Year</td> <!-- Here I want to add this number "2" to the name of the <td> element.-->
<td name="arrival-2">Arrival</td> <!-- Here I want to add this number "2" to the name of the <td> element.-->
<td name="departure-2">Departure</td> <!-- Here I want to add this number "2" to the name of the <td> element.-->
<td name="sn-2">S/N</td> <!-- Here I want to add this number "2" to the name of the <td> element.-->
<td name="time-mark-2">Time Mark</td> <!-- Here I want to add this number "2" to the name of the <td> element.-->
<td name="observation-2">Observation</td> <!-- Here I want to add this number "2" to the name of the <td> element.-->
</tr>
<tr>
<td name="id-3">3</td> <!-- The value of this <td> tag needs to populate in the name of each next <td> 'name' parameter for this row!-->
<td name="ddate-3">03062016</td> <!-- Here I want to add this number "3" to the name of the <td> element.-->
<td name="day-3">Day</td> <!-- Here I want to add this number "3" to the name of the <td> element.-->
<td name="month-3">Month</td> <!-- Here I want to add this number "3" to the name of the <td> element.-->
<td name="year-3">Year</td> <!-- Here I want to add this number "3" to the name of the <td> element.-->
<td name="arrival-3">Arrival</td> <!-- Here I want to add this number "3" to the name of the <td> element.-->
<td name="departure-3">Departure</td> <!-- Here I want to add this number "3" to the name of the <td> element.-->
<td name="sn-3">S/N</td> <!-- Here I want to add this number "3" to the name of the <td> element.-->
<td name="time-mark-3">Time Mark</td> <!-- Here I want to add this number "3" to the name of the <td> element.-->
<td name="observation-3">Observation</td> <!-- Here I want to add this number "3" to the name of the <td> element.-->
</tr>
<tr>
<td><input class='hdl' type='text' name='id' value=''></input></td>
<td><input class='hdl' type='text' name='date' value=''></input></td>
<td><input class='hds' type='text' name='day' value=''></input></td>
<td><input class='rm' type='text' name='month' value=''></input></td>
<td><input class='hm' type='text' name='year' value=''></input></td>
<td><input class='ob' type='text' name='arr' value=''></input></td>
<td><input class='pre' type='text' name='dep' value=''></input></td>
<td><input class='prs' type='text' name='sn' value=''></input></td>
<td><input class='vol' type='text' name='tm' value=''></input></td>
<td><input class='obs' type='text' name='ob' value=''></input></td>
</tr>
</table>
<input id='submit' type='submit' name='submit' value='Submit' />
</form>
</body>
</html>
我希望有人能帮我解释一下这件事。
【问题讨论】:
-
而
RecursiveIteratorIterator::key()不起作用? -
它可以工作,但它返回每个值的列名 :/ 。我需要它始终是当前循环的第一个键。
-
我做了这样的测试:
function current() { return "<td name='".RecursiveIteratorIterator::key()."'>".parent::current()."</td>"; } -
好的,您能否用一个查询结果示例来补充您的问题,我认为这是一个关联数组。第二个生成的 HTML 示例将在
TableRows类生成其<tr><td>...</td></tr>输出时显示结果?了解您的预期结果会很棒.. -
@dbf 请您说出您的意思/需要什么作为查询结果。我的意思是,您需要数组的
var_dump();还是print_r();?是的,据我所知,它是关联的。但是,您可以在我更新问题中的代码时进行检查!