dqayok7935 2012-10-09 13:39
浏览 29
已采纳

PHP函数与Echo

I find myself lately working with a ton of tabular data, I am more than comfortable writing the raw html but i'm wondering if there's an easier way (or library worth implementing) that helps reduce time writing html tags such as

<tr><td></td></tr>

I have created my own custom function, but I think ultimately it's not necessarily helping and potentially could be slowing down my script, now my project is small so maybe it could cope with that, examples:

echo '<tr class="test_class">
    <td>' . $content . '</td>
    <td>' . $second_content . '</td>
<tr/>';

here is an example with my current function:

tr("test_class");
   td(); echo $content; escape(td);
   td(); echo $second_content; escape(td);
escape(tr);

Looking forward to hearing peoples thoughts.

  • 写回答

4条回答 默认 最新

  • dsh1956 2012-10-09 13:46
    关注

    There are multiple ways of doing this...

    1. write your own html helper library, that will contain classes, that can generate html elements based on their data source. For instance you could call them like:

      <?php
         HtmlHelper::Table("someArrayOfValues", "idOfTable", "styleOfTable");
      ?>
      

      This is a good reusable solution, if you implement this idea properly. I was playing with this myself few days ago, really it's simple.

    2. if you find 1. difficult, you can split the idea down... But not so deep like you've shown, but generate whole rows instead.

      <?php
          foreach ($myArray as $key => $value)
          {
               echo HtmlHelper::Row(...);
          }
      ?>
      
    3. Find some library, that provides this functionality. Can't help you on this one I'm afraid. I like to have control over the generated markup.

    Hope you get the idea.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗