HEREDOC 内的 PHP 函数 - 返回选项卡布局
我有一个 php 函数,可以动态查询数据库,并返回 6 个表。我想将每个表存储为 php 数组元素,通过 JSON/AJAX 将数组传回,并将每个表在页面上单独显示。我编写了测试代码来完成这一切。测试代码获取 HTML 字符串数组并将它们传回。
我遇到的问题是,在我的测试代码中,我使用了非常简单的字符串:
$d1 = <<<END
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
END;
$d2 = "<div id='#f-2'>Message 2</div>";
实际上,我拥有的是一大堆 php 代码来循环 SQL 结果并构建 HTML 表:
while($row=pg_fetch_array($result, null, PGSQL_ASSOC))
{
if(!$written_header)
{
echo "<table class=\"fancy\" id=\"ticket_list_table\"><thead><tr><th>ID</th><th>Hardware Name</th><th>Serial No.</th><th>IP Address</th><th>Username</th><th>Password</th></tr></thead><tbody>";
$written_header = true;
}
echo "<tr><td>",$row[id],"</td><td>",'$row[hardwarename]',"</td><td>",'$row[serialnumber]',"</td><td>",'$row[ipAddress]',"</td><td>",'$row[username]',"</td><td>",'$row[password]',"</td></tr>";
}
pg_free_result($result);
if($written_header)
{
// close the table body and table object
echo "</tbody></table>";
}
else
{
echo "No results.";
}
echo "</div>";
如何将这些更复杂的东西存储在 PHP 中变量以便我可以将其作为数组元素传递回来?
I have a php function that dynamically queries a database, and returns 6 tables. I want to store each of these tables as a php array element, pass the array back via JSON/AJAX, and display each table as a seperate on a page. I wrote test code to do all of this. The test code takes an array of HTML strings and passes them back.
The problem I have is that in my test code, I used really simple strings:
$d1 = <<<END
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
END;
$d2 = "<div id='#f-2'>Message 2</div>";
Actually, what I have is a whole bunch of php code to loop through SQL results and construct HTML tables:
while($row=pg_fetch_array($result, null, PGSQL_ASSOC))
{
if(!$written_header)
{
echo "<table class=\"fancy\" id=\"ticket_list_table\"><thead><tr><th>ID</th><th>Hardware Name</th><th>Serial No.</th><th>IP Address</th><th>Username</th><th>Password</th></tr></thead><tbody>";
$written_header = true;
}
echo "<tr><td>",$row[id],"</td><td>",'$row[hardwarename]',"</td><td>",'$row[serialnumber]',"</td><td>",'$row[ipAddress]',"</td><td>",'$row[username]',"</td><td>",'$row[password]',"</td></tr>";
}
pg_free_result($result);
if($written_header)
{
// close the table body and table object
echo "</tbody></table>";
}
else
{
echo "No results.";
}
echo "</div>";
How can I store this more complex stuff in a PHP variable so that I can then pass it back as an array element?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定您的理解是否正确,但是
此 .= 符号不断将新数据添加到先前值的末尾。
Not sure if understand you correctly, but
This .= notation keeps adding new data to the end of previous value.
这是我自己的特定问题的解决方案:
This is the solution to my own particular problem: