How to create rows and columns using php and html

I need to create columns of rows like this:

enter image description here

This is test code that simply implements the logic for the snapshot.

source

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

      

  • This is the raw html that shows the snapshot.
  • I am trying to build the same logic as the snapshot.
Array structure

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

      

+3


source to share


1 answer


Impossible how you want it to be done. You have to create a 2D array and get data from that. Here's an example:

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

      



Output: enter image description here

Hope it helps

+2


source







All Articles