2013-03-18 69 views
-1

我试图重新设计一个Web应用程序,我得到的代码块难倒了HTML的PHP​​代码:重新设计页面:改变产生

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] : '&nbsp;'; 
      $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">&nbsp;</td> 
     <form action="some-action" method="post"></form> 
    </tr> 
    </tbody> 
</table> 

and so on. . . . 

,它看起来像这样:

Disregard the size of the buttons. That's intentional

我只是想让他们都在一个div,我可以风格我的口味。任何人都可以向我解释那段代码,以便我可以使用它吗? 或者更好的还是让我看看如何让它们适合于div。

+1

如果我能提供任何意见,这将是从这样的事情移开。该代码几乎不可读。 – andy 2013-03-18 09:56:59

回答

0

echo "<div id='mycustomdiv'>"; 
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>'; 

echo "</div>"; 
 

看到上面的代码有一个回声您的自定义div和该div的结束回声。添加这些回声。如果你需要解释代码,请告诉我。

+0

我想完全删除这些表。有任何想法吗? – ThisBoyPerforms 2013-03-18 10:04:18

2

你需要澄清你的问题提一提,你想删除的表结构。

这里发生的事情是应用程序的数组中返回一个数组$这个 - > cells1每个项目它使用它包含的元素创建一个单独的表。

下面是一个三元if语句,它说的是如果c [2]存在echo c [2]如果不是echo''该行中的第一件事是问题然后是问号以及如果做什么if如果问题是错误的,那么冒号是真的,冒号怎么办。

$c[2] ? $c[2] : ''; 

要改变这只是创建divs,你真正需要做的就是删除表格标记并将其替换为你需要的任何东西。

例如:

foreach ($this->cells1 as $c) { 
      $s .= $c[2] ? $c[2] : ''; 
      $s .= '<div><p>' . ($c[0] ? (' ' . $c[0]) : ''); 
      $s .= $c[1] ? $c[1] : '&nbsp;'; 
      $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]; 
} 

这会给你只是一个什么每个元素都被返回,所以你可以计算出的数据是什么样子,你知道后搞清楚什么用它做会让生活变得更加简单的列表。