php - 如何使用 php 和 html 创建行和列

标签 php html

我需要像这样创建行列:

enter image description here

这是测试代码,我只是实现快照的逻辑。

源代码

    $data = array('early','early','comment1','comment2','20','30');


   function create_table($data)
   {
   $res = '<table width="200" cellpadding="1" cellspacing="1" border="1">';
   $max_data = sizeof($data);
   $ctr = 1;
      foreach ($data as $db_data)
      {
            if ($ctr % 2 == 0) $res .= '<td>' . $db_data. '</td></tr>';
              else
              {
              if ($ctr < $max_data) $res .= '<tr><td>' . $db_data. '</td>';
               else $res .= '<tr><td colspan="2">' . $db_data. '</td></tr>';
              }
            $ctr++;
      }
      return $res . '</table>';
   }

 echo create_table($data);

html

          <h2>Entry Fee</h2>

        <table border="0" class="tb not-mobile">
        <tr>
        <td width="30%" rowspan="3">Early (Payment received by 1/4/15) </td>
        <td width="58%">MD/DO</td>
        <td width="12%" class="aligncenter">$23</td>
        </tr>
        <tr>

        <td>CRNA/PA</td>
        <td class="aligncenter">$37</td>
        </tr>
        <tr>
        <td>RESIDENT/RN/OTHERS</td>
        <td class="aligncenter">$49</td>
        </tr>
        <tr>
        <td rowspan="3">Early (Payment received by 1/4/15) </td>
        <td>MD/DO</td>
        <td class="aligncenter">$23</td>
        </tr>
        <tr>

        <td>CRNA/PA</td>
        <td class="aligncenter">$37</td>
        </tr>
        <tr>
        <td>RESIDENT/RN/OTHERS</td>
        <td class="aligncenter">$49</td>
        </tr>
        </table>
  • 快照显示的原始 html。
  • 我正在尝试构建类似于快照的相同逻辑。

数组结构

        Array
        (
        [0] => Array(
        [type] => General Public Tickets Adult
        [metadata] => Array(
        [0] => Array(
        [amount] => 50
        [comment] => (Working Days)
        )
        [1] => Array(
        [amount] => 80
        [comment] => (Saturday/ Sunday/ Holiday)
        )
        )
        )

        [1] => Array(
        [type] => Special Tickets Children
        [metadata] => Array(
        [0] => Array(
        [amount] => 300
        [comment] => (Saturday/ Sunday/ Holiday)
        )
        [1] => Array(
        [amount] => 10000
        [comment] => (Monday afternoon)
        )
        )
        )
        )

最佳答案

不可能按照您希望的方式完成。您必须创建一个二维数组并从中获取数据。 这是一个例子:

<?php
// On the line below, create your own associative array:
$myArray = array (  'Early (Payment received by 1/4/15)' => array('MD/DO', '$23','RNA/PA', '$37','RESIDENT/RN/OTHERS', '$49'),
                    'Early (Payment received by 1/4/15) ' => array('MD/DO', '$23','RNA/PA', '$37','RESIDENT/RN/OTHERS', '$49'));

// On the line below, loop through the array and output
// *all* of the values to the page:
print '<table width="800" cellpadding="1" cellspacing="1" border="1">';
foreach ($myArray as $place => $task) 
{
    print "<tr><td rowspan='4'>".$place."</td></tr>";
    $i = 0;
    print "<tr>";
    foreach ($task as $thingToDo)
    {
        $i++;
        if ($i == 2)
        {
            print "<td>".$thingToDo."</td>";
            print "</tr>";
            $i = 0;
        }
        else
        {
            print "<td>".$thingToDo."</td>";
        }
    }
}
print " </table>";
?>

输出: enter image description here

希望对你有帮助

关于php - 如何使用 php 和 html 创建行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26339763/

相关文章:

php - 通过PDO::sqlite Create函数在sqlite中实现timestampdiff

php - Mysqli_error() 不起作用

javascript - Angularjs 根据存储在 session 存储中并存储在 $scope 中的变量动态显示/隐藏元素

javascript - 尽管使用了 jQuery,datepicker 仍然无法工作

php - 如何以编程方式获取与 WordPress 中的搜索查询匹配的帖子?

php - 根据多个条件从连接中进行选择的原则

php - Magento Quote Item 对象 - 如何加载项目的选项?

javascript - Jquery - 如何隐藏表前的 h2 和表后的文本?

php - HREF IP 地址

javascript - 如何在点击事件中获取子元素