HEREDOC 内的 PHP 函数 - 返回选项卡布局

发布于 2024-09-19 02:07:46 字数 1557 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2024-09-26 02:07:46

不确定您的理解是否正确,但是

$result = "<table>";
$result .= "<tr>";
$result .= "<trd>";

此 .= 符号不断将新数据添加到先前值的末尾。

Not sure if understand you correctly, but

$result = "<table>";
$result .= "<tr>";
$result .= "<trd>";

This .= notation keeps adding new data to the end of previous value.

抠脚大汉 2024-09-26 02:07:46

这是我自己的特定问题的解决方案:

  while($row=pg_fetch_array($result, null, PGSQL_ASSOC))
  {

    if(!$written_header)
    {
      $d1 = "<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;
    }

    $d1 = $d1."<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
    $d1 = $d1."</tbody></table>";
  }
  else
  {
    $d1 = $d1."No results.";
  }

This is the solution to my own particular problem:

  while($row=pg_fetch_array($result, null, PGSQL_ASSOC))
  {

    if(!$written_header)
    {
      $d1 = "<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;
    }

    $d1 = $d1."<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
    $d1 = $d1."</tbody></table>";
  }
  else
  {
    $d1 = $d1."No results.";
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文