2013-03-18 40 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]; 
} 

這會給你只是一個什麼每個元素都被返回,所以你可以計算出的數據是什麼樣子,你知道後搞清楚什麼用它做會讓生活變得更加簡單的列表。