<分区>
<分区>
我正在尝试重新设计一个 Web 应用程序,但我被这段代码难住了:
foreach ($this->cells1 as $c) {
$s .= $c[2] ? $c[2] : '';
$s .= '<div style="clear:both"><table><tr><td nowrap="nowrap"' . ($c[0] ? (' ' . $c[0]) : '') . '>';
$s .= $c[1] ? $c[1] : ' ';
$s .= '</td>';
$s .= $c[3] ? $c[3] : '';
}
$s .= '</tr></table></div>';
它正在生成 HTML 代码,例如:
<div style="clear:both"></div>
<table>
<tbody>
<tr>
<td nowrap="nowrap">Search:</td>
<form action="some-action" method="post"></form>
</tr>
</tbody>
</table>
<div style="clear:both"></div>
<table>
<tbody>
<tr class="alt-row">
<td nowrap="nowrap">
<input type="text">
</td>
</tr>
</tbody>
</table>
<div style="clear:both"></div>
<table>
<tbody>
<tr>
<td nowrap="nowrap"> </td>
<form action="some-action" method="post"></form>
</tr>
</tbody>
</table>
and so on. . . .
它看起来像这样:
我只想将它们全部放在一个 div 中,我可以根据自己的喜好设计样式。任何人都可以向我解释那部分代码,以便我可以使用它吗? 或者更好的办法是告诉我另一种方法让它们适合 div。
最佳答案
您需要澄清您的问题以提及您希望删除表结构。
这里发生的事情是应用程序为该数组中的每个元素返回一个数组 $this->cells1 它正在使用它包含的元素创建一个单独的表。
下面是一个三元 if 语句,它说的是 if c[2] exists echo c[2] if not echo '' 该行的第一件事是问题,然后是问号,如果问题是正确的,然后是冒号,如果问题是错误的,该怎么办。
$c[2] ? $c[2] : '';
要将其更改为只创建 div,您真正需要做的就是删除表格标记并将其替换为您需要的任何内容。
例如:
foreach ($this->cells1 as $c) {
$s .= $c[2] ? $c[2] : '';
$s .= '<div><p>' . ($c[0] ? (' ' . $c[0]) : '');
$s .= $c[1] ? $c[1] : ' ';
$s .= '</p>';
$s .= $c[3] ? $c[3] : '';
}
$s .= '</div>';
将生成一个第一个元素包含在 p 标签中的 div。
就我个人而言,我会做的是分别回显每个元素以查看它返回的内容,然后相应地构造 foreach,因为老实说它看起来也有一些从数据库返回的 HTML。
foreach($this->cells1 as $c) {
echo 'c0:' . $c[0];
echo 'c1:' . $c[1];
echo 'c2:' . $c[2];
echo 'c3:' . $c[3];
}
这将为您提供每个元素返回的内容的列表,这样您就可以弄清楚数据的样子,在您知道弄清楚如何处理它会让生活变得更轻松之后。
关于php - 重新设计页面 : changing the PHP code that generates the HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15473793/