如何创建行&使用php和html的列

时间:2014-10-13 12:13:28

标签: 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)
        )
        )
        )
        )

1 个答案:

答案 0 :(得分:2)

你想要的方式是不可能的。您必须创建一个2D数组并从中获取数据。 这是一个例子:

<?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

希望这有帮助