如何使用 PHP 中的循环动态创建 html table?

How to create html table dynamically using loop in PHP?

我想使用 php foreachfor 循环在数组数据的 html table 中显示结果。我在下面放置了数组。在数组中有字段 'bs_column',它的值可以是 12、6 或 4。在循环中,我想在每个 tr 中显示一个 td(每行一列)如果bs_column 等于 12 并且每个 tr 中有两个 td(每行两列)如果 bs_column==6 并且每个 [=18 中有三个 td =](每行三列)如果bs_column==4。 如果不成对 bs_column==6,这是循环序列中的两个字段 (<tr><td>field one</td><td>field two</td></tr>),则需要一行中的一列 (<tr><td>single field</td></tr>)。 如果不成对 bs_column==4 是循环序列中的三个字段 (<tr><td>field one</td><td>field two</td><td>field three</td></tr>) 则将两个字段 (<tr><td>field one</td><td>field two</td></tr>) 配对 如果不成对则将单个字段 (<tr><td>single field</td></tr>) 在行中的列中。

数组是

$fields= Array ( [0] => Array ( [id] => 1 [form_id] => 3 [name] => THE APPLICANT [slug] => customers_the-applicant_heading [required] => 0 [type] => heading [options] => [field_order] => 0 [bs_column] => 12 ) [1] => Array ( [id] => 2 [form_id] => 3 [name] => Applicant(s) Name [slug] => customers_applicants-name_input [required] => 0 [type] => input [options] => [field_order] => 1 [bs_column] => 12 ) [2] => Array ( [id] => 3 [form_id] => 3 [name] => Email [slug] => customers_email_input [required] => 0 [type] => input [options] => [field_order] => 2 [bs_column] => 6 ) [3] => Array ( [id] => 4 [form_id] => 3 [name] => Telephone [slug] => customers_telephone_input [required] => 0 [type] => input [options] => [field_order] => 3 [bs_column] => 6 ) [4] => Array ( [id] => 5 [form_id] => 3 [name] => s this Property currently insured? [slug] => customers_s-this-property-currently-insured_checkbox [required] => 0 [type] => select [options] => Yes,No [field_order] => 4 [bs_column] => 12 ) [5] => Array ( [id] => 6 [form_id] => 3 [name] => Name of insurer(s) [slug] => customers_name-of-insurers_input [required] => 0 [type] => input [options] => [field_order] => 5 [bs_column] => 6 ) [6] => Array ( [id] => 7 [form_id] => 3 [name] => Current insurer expiry date [slug] => customers_current-insurer-expiry-date_date_picker [required] => 0 [type] => date_picker [options] => [field_order] => 6 [bs_column] => 6 ) [7] => Array ( [id] => 8 [form_id] => 3 [name] => D.O.B. of eldest insured [slug] => customers_dob-of-eldest-insured_date_picker [required] => 0 [type] => date_picker [options] => [field_order] => 7 [bs_column] => 6 ) [8] => Array ( [id] => 9 [form_id] => 3 [name] => Applicant postal address [slug] => customers_applicant-postal-address_input [required] => 0 [type] => textarea [options] => [field_order] => 8 [bs_column] => 12 ) [9] => Array ( [id] => 10 [form_id] => 3 [name] => Suburb [slug] => customers_suburb_input [required] => 0 [type] => input [options] => [field_order] => 9 [bs_column] => 4 ) [10] => Array ( [id] => 11 [form_id] => 3 [name] => State [slug] => customers_state_input [required] => 0 [type] => input [options] => [field_order] => 10 [bs_column] => 4 ) [11] => Array ( [id] => 12 [form_id] => 3 [name] => Postcode [slug] => customers_postcode_input [required] => 0 [type] => input [options] => [field_order] => 11 [bs_column] => 4 ) [12] => Array ( [id] => 13 [form_id] => 3 [name] => PERIOD OF INSURANCE [slug] => customers_period-of-insurance_heading [required] => 0 [type] => heading [options] => [field_order] => 12 [bs_column] => 12 ) [13] => Array ( [id] => 14 [form_id] => 3 [name] => Cover required From [slug] => customers_cover-required-from_input [required] => 0 [type] => input [options] => [field_order] => 13 [bs_column] => 6 ) [14] => Array ( [id] => 15 [form_id] => 3 [name] => Cover required to [slug] => customers_cover-required-to_input [required] => 0 [type] => input [options] => [field_order] => 14 [bs_column] => 6 ) [15] => Array ( [id] => 16 [form_id] => 3 [name] => INSURED DETAILS [slug] => customers_insured-details_heading [required] => 0 [type] => heading [options] => [field_order] => 15 [bs_column] => 12 ) [16] => Array ( [id] => 17 [form_id] => 3 [name] => What are the premises you wish to insure? [slug] => customers_what-are-the-premises-you-wish-to-insure-_note [required] => 0 [type] => note [options] => [field_order] => 16 [bs_column] => 12 ) [17] => Array ( [id] => 18 [form_id] => 3 [name] => Address of property [slug] => customers_address-of-property_input [required] => 0 [type] => input [options] => [field_order] => 17 [bs_column] => 12 ) [18] => Array ( [id] => 19 [form_id] => 3 [name] => CONSTRUCTION DETAILS [slug] => customers_construction-details_heading [required] => 0 [type] => heading [options] => [field_order] => 21 [bs_column] => 12 ) [19] => Array ( [id] => 20 [form_id] => 3 [name] => Cover type required [slug] => customers_cover-type-required_checkbox [required] => 0 [type] => checkbox [options] => Specified events,Accidental damage (specified events + accidental damage) [field_order] => 22 [bs_column] => 12 ) [20] => Array ( [id] => 23 [form_id] => 3 [name] => Year built [slug] => customers_year-built_date_picker [required] => 0 [type] => date_picker [options] => [field_order] => 25 [bs_column] => 6 ) [21] => Array ( [id] => 24 [form_id] => 3 [name] => Number of storeys [slug] => customers_number-of-storeys_input [required] => 0 [type] => input [options] => [field_order] => 26 [bs_column] => 6 ) [22] => Array ( [id] => 25 [form_id] => 3 [name] => Is the building elevated or multi storey? [slug] => customers_is-the-building-elevated-or-multi-storey_checkbox [required] => 0 [type] => checkbox [options] => Single storey,Single storey elevated,Double storey,Three or more storey [field_order] => 27 [bs_column] => 12 ) [23] => Array ( [id] => 26 [form_id] => 3 [name] => Is the property well maintained structurally sound and secured against wind and rain? [slug] => customers_is-the-property-well-maintained-structurally-sound-and-secured-against-wind-and-rain_checkbox [required] => 0 [type] => checkbox [options] => Yes,No [field_order] => 28 [bs_column] => 12 ) [24] => Array ( [id] => 27 [form_id] => 3 [name] => Is the property undergoing renovations over ,000, OR, under construction, OR, to be demolished? [slug] => customers_is-the-property-undergoing-renovations-over-75000-or-under-construction-or-to-be-demolished_checkbox [required] => 0 [type] => checkbox [options] => Yes,No [field_order] => 29 [bs_column] => 12 ) [25] => Array ( [id] => 28 [form_id] => 3 [name] => Is your home location on a site exceeding 20,000 square metres? [slug] => customers_is-your-home-location-on-a-site-exceeding-20000-square-metres_checkbox [required] => 0 [type] => checkbox [options] => Yes,No [field_order] => 30 [bs_column] => 12 ) [26] => Array ( [id] => 29 [form_id] => 3 [name] => Has the land where the building or contents are been flooded or inundated by water more than once in the last 10 years? [slug] => customers_has-the-land-where-the-building-or-contents-are-been-flooded-or-inundated-by-water-more-than-once-in-the-last-10-years_checkbox [required] => 0 [type] => checkbox [options] => Yes,No [field_order] => 31 [bs_column] => 12 ) [27] => Array ( [id] => 30 [form_id] => 3 [name] => Is there a pool on your property? [slug] => customers_is-there-a-pool-on-your-property_checkbox [required] => 0 [type] => checkbox [options] => Yes,No [field_order] => 32 [bs_column] => 6 ) [28] => Array ( [id] => 31 [form_id] => 3 [name] => Building type: [slug] => customers_building-type_checkbox [required] => 0 [type] => checkbox [options] => House on slab/foundation,House on poles,Villa/Townhouse,Unit/Flat,Terrace/semi,Course of construction,Granny flat,Heritage property,Holiday home,Mobile home,Display home,Nursing home Caravan,Retirement village [field_order] => 33 [bs_column] => 12 ) [29] => Array ( [id] => 32 [form_id] => 3 [name] => Floor Construction: [slug] => customers_floor-construction_checkbox [required] => 0 [type] => checkbox [options] => Concrete,Wood [field_order] => 31 [bs_column] => 6 ) [30] => Array ( [id] => 34 [form_id] => 3 [name] => Wall construction:If mixed construction, please detail % of each [slug] => customers_wall-constructionif-mixed-construction-please-detail-of-each_checkbox [required] => 0 [type] => checkbox [options] => Double Brick Timber/,Brick Veneer Steel,Weatherboard,Fibro/Asbestos,Concrete/Hebel,Stone/ Sandstone,Cement,Polystyrene/EP,Mud brick [field_order] => 33 [bs_column] => 12 ) [31] => Array ( [id] => 35 [form_id] => 3 [name] => other [slug] => customers_other_input [required] => 0 [type] => input [options] => [field_order] => 34 [bs_column] => 12 ) [32] => Array ( [id] => 36 [form_id] => 3 [name] => Roof: [slug] => customers_roof_checkbox [required] => 0 [type] => checkbox [options] => Cement Tiles,Iron,Slate,Terracotta,Colour bond,Steel,Wood [field_order] => 35 [bs_column] => 12 ) [33] => Array ( [id] => 37 [form_id] => 3 [name] => other [slug] => customers_other_input [required] => 0 [type] => input [options] => [field_order] => 36 [bs_column] => 12 ) [34] => Array ( [id] => 38 [form_id] => 3 [name] => Sandwich Foam Panel construction (EPS) [slug] => customers_sandwich-foam-panel-construction-eps_checkbox [required] => 0 [type] => checkbox [options] => Yes,No [field_order] => 37 [bs_column] => 6 ) [35] => Array ( [id] => 39 [form_id] => 3 [name] => If yes, percentage % [slug] => customers_if-yes-percentage-_input [required] => 0 [type] => input [options] => [field_order] => 38 [bs_column] => 6 ) )`

php代码

 $max_six_per_row = 2;
   $max_four_per_row = 3;
   $item_count_two = 0;
   $item_count_three = 0;
 $fields_html .= '<table><tbody>';
foreach ($fields as $field) {
            $field['name'] = $field['name'];
        if ($field['bs_column'] == 12) {
             $fields_html .="<tr><td width='100%'>".$field['name'] ."</td></tr>";   
            } 
        if ($field['bs_column'] == 6) {
        if ($item_count_two == 0)
        {
            $fields_html .="<tr>";
        }
             $fields_html .="<td width='50%'>".$field['name'] ."</td>";
        if ($item_count_two == $max_six_per_row)
        {
            $fields_html .="</tr>";
            $item_count_two = 0;
        }
            $item_count_two++;
         } 
        if ($field['bs_column'] == 4) {
        if ($item_count_three == 0)
        {
            $fields_html .="<tr>";
        }
             $fields_html .="<td>".$field['name'] ."</td>";
        if ($item_count_three == $max_four_per_row)
        {
            $fields_html .="</tr>";
            $item_count_three = 0;
        }
            $item_count_three++;
       }
      
      }
      $fields_html .= '</tbody></table>';
     echo $fields_html;

好的,这是第一稿。我需要更好地了解 field2 和 field3。它们是否来自不同行的相同字段名称?或者是来自同一行的字段?根据您的回答,此代码逻辑可能会更改。

<!DOCTYPE HTML>
<html lang="en">
<head>
 <title>Loop Test</title>
</head>
<body>
<?php
$rows = json_decode('[{"id":"21","name":"THE APPLICANT","req":"0","type":"heading","opt":"","f_ord":"0","b_c":"12","val":""},{"id":"22","name":"Applicant(s) Name","req":"0","type":"input","opt":"","f_ord":"1","b_c":"12","val":"Ali Hasan"},{"id":"23","name":"Email","req":"0","type":"input","opt":"","f_ord":"2","b_c":"6","val":"ghulamseratalikhan@gmail.com"},{"id":"24","name":"Telephone","req":"0","type":"input","opt":"","f_ord":"3","b_c":"6","val":"03100865443"},{"id":"25","name":"s this Property currently insured?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"4","b_c":"12","val":"No"},{"id":"26","name":"Name of insurer(s)","req":"0","type":"input","opt":"","f_ord":"5","b_c":"6","val":"Insurer Name"},{"id":"27","name":"Current insurer expiry date","req":"0","type":"date_picker","opt":"","f_ord":"6","b_c":"6","val":"2021-08-20 00:00:00"},{"id":"28","name":"D.O.B. of eldest insured","req":"0","type":"date_picker","opt":"","f_ord":"7","b_c":"6","val":"2006-08-24 00:00:00"},{"id":"29","name":"Applicant postal address","req":"0","type":"input","opt":"","f_ord":"8","b_c":"12","val":"1535 service road g-11\/2 Islamabad first floor"},{"id":"30","name":"Suburb","req":"0","type":"input","opt":"","f_ord":"9","b_c":"12","val":"Capital Terriority"},{"id":"31","name":"State","req":"0","type":"input","opt":"","f_ord":"10","b_c":"6","val":"Capital"},{"id":"32","name":"Postcode","req":"0","type":"input","opt":"","f_ord":"11","b_c":"6","val":"58000"},{"id":"33","name":"PERIOD OF INSURANCE","req":"0","type":"heading","opt":"","f_ord":"12","b_c":"12","val":""},{"id":"34","name":"Cover required From","req":"0","type":"input","opt":"","f_ord":"13","b_c":"6","val":"Rashid Ali"},{"id":"35","name":"Cover required to","req":"0","type":"input","opt":"","f_ord":"14","b_c":"6","val":"Hasan Ali"},{"id":"36","name":"INSURED DETAILS","req":"0","type":"heading","opt":"","f_ord":"15","b_c":"12","val":""},{"id":"37","name":"What are the premises you wish to insure? ","req":"0","type":"note","opt":"","f_ord":"16","b_c":"12","val":""},{"id":"38","name":"Address of property","req":"0","type":"input","opt":"","f_ord":"17","b_c":"12","val":"F-10\/4 house 45 street 345 near F-10 marakaz"},{"id":"39","name":"CONSTRUCTION DETAILS","req":"0","type":"heading","opt":"","f_ord":"21","b_c":"12","val":""},{"id":"40","name":"Cover type required","req":"0","type":"checkbox","opt":"Specified events","f_ord":"22","b_c":"4","val":""},{"id":"41","name":"OR ","req":"0","type":"note","opt":"","f_ord":"23","b_c":"4","val":""},{"id":"42","name":"","req":"0","type":"checkbox","opt":"Accidental damage (specified events + accidental damage)","f_ord":"24","b_c":"4","val":"Accidental damage (specified events + accidental damage)"},{"id":"43","name":"Year built","req":"0","type":"date_picker","opt":"","f_ord":"25","b_c":"6","val":"2014-08-22 00:00:00"},{"id":"44","name":"Number of storeys","req":"0","type":"input","opt":"","f_ord":"26","b_c":"6","val":"3"},{"id":"45","name":"Is the building elevated or multi storey?","req":"0","type":"checkbox","opt":"Single storey,Single storey elevated,Double storey,Three or more storey","f_ord":"27","b_c":"12","val":"Double storey, Three or more storey"},{"id":"46","name":"Is the property well maintained structurally sound and secured against wind and rain?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"28","b_c":"12","val":"Yes"},{"id":"47","name":"Is the property undergoing renovations over ,000, OR, under construction, OR, to be demolished?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"29","b_c":"12","val":"Yes"},{"id":"48","name":"Is your home location on a site exceeding 20,000 square metres?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"30","b_c":"12","val":"No"},{"id":"49","name":"Has the land where the building or contents are been flooded or inundated by water more than once in the last 10 years?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"31","b_c":"12","val":"Yes"},{"id":"50","name":"Is there a pool on your property?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"32","b_c":"12","val":"Yes"},{"id":"51","name":"Building type:","req":"0","type":"checkbox","opt":"House on slab\/foundation,House on poles,Villa\/Townhouse,Unit\/Flat,Terrace\/semi,Course of construction,Granny flat,Heritage property,Holiday home,Mobile home,Display home,Nursing home Caravan,Retirement village","f_ord":"33","b_c":"12","val":"House on poles, Unit\/Flat, Holiday home, Mobile home, Display home"},{"id":"52","name":"Floor Construction:","req":"0","type":"checkbox","opt":"Concrete,Wood","f_ord":"31","b_c":"6","val":"Concrete, Wood"},{"id":"53","name":"Other","req":"0","type":"input","opt":"","f_ord":"32","b_c":"6","val":"Marble Sfloor"},{"id":"54","name":"Wall construction:If mixed construction, please detail % of each<\/small>","req":"0","type":"checkbox","opt":"Double Brick Timber\/,Brick Veneer Steel,Weatherboard,Fibro\/Asbestos,Concrete\/Hebel,Stone\/ Sandstone,Cement,Polystyrene\/EP,Mud brick","f_ord":"33","b_c":"12","val":"Weatherboard, Fibro\/Asbestos, Cement, Mud brick"},{"id":"55","name":"other","req":"0","type":"input","opt":"","f_ord":"34","b_c":"12","val":"Mud bv construction"},{"id":"56","name":"Roof:","req":"0","type":"checkbox","opt":"Cement Tiles,Iron,Slate,Terracotta,Colour bond,Steel,Wood","f_ord":"35","b_c":"12","val":"Cement Tiles, Slate, Colour bond"},{"id":"57","name":"other","req":"0","type":"input","opt":"","f_ord":"36","b_c":"12","val":"steael wooddd roof"},{"id":"58","name":"Sandwich Foam Panel construction (EPS)","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"37","b_c":"4","val":"Yes"},{"id":"59","name":"If yes, percentage %","req":"0","type":"input","opt":"","f_ord":"38","b_c":"4","val":"45%"}]', true);
//$max_cols = 3;
//$max_tds = 1;
echo("<table>" . PHP_EOL);
echo(" <tbody>" . PHP_EOL);
foreach($rows as $key => $row)
{
 echo("  <tr>" . PHP_EOL);
 switch($row["b_c"])
 {
  case 4:
   echo("   <td>{$row['name']}</td><td>{$row['name']}</td><td>{$row['name']}</td>" . PHP_EOL);
//   $max_tds = 3;
   break;
  case 6:
   echo("   <td>{$row['name']}</td><td>{$row['name']}</td><td></td>" . PHP_EOL);
//   $max_tds = 2;
   break;
  case 12:
   echo("   <td>{$row['name']}</td><td></td><td></td>" . PHP_EOL);
//   $max_tds = 1;
   break;
  default:
   echo("   <td>{$row['name']}</td><td></td><td></td>" . PHP_EOL);
   break;
 }
 echo("  </tr>" . PHP_EOL);
// echo("Key: " . $key . PHP_EOL);
// print_r($row);
}
echo(" </tbody>" . PHP_EOL);
echo("</table>" . PHP_EOL);
?>
</body>
</html>

一旦我有了更好的理解,我可以帮助更改此代码。

Table 显示正确的输出。在foreach循环中首先需要检查$field['b_c']==6
,此检查是否两个 tds 在 tr 内配对然后关闭 tr,如果没有配对并且源数据具有单个连续的 $field['b_c']==6 行然后也关闭 tr。第二个需要检查是否 $field['b_c']==4 并在行小于三或等于三时关闭 tr。 对于 $field['b_c']==12 只有一行和一列。

<!DOCTYPE HTML>
<html lang="en">
<head>
 <title>Loop Test</title>
 <style>
.table{
    /*width:100%;*/
    border-collapse:collapse;
    border:1px solid #ccc;
    margin:30px;
    padding:8px;
}
.table tr,.table tr>td{
     border:1px solid #ccc;
     padding:5px;
}
 </style>
</head>
<body>
<?php
$rows = json_decode('[{"id":"21","name":"THE APPLICANT","req":"0","type":"heading","opt":"","f_ord":"0","b_c":"12","val":""},{"id":"22","name":"Applicant(s) Name","req":"0","type":"input","opt":"","f_ord":"1","b_c":"12","val":"Ali Hasan"},{"id":"23","name":"Email","req":"0","type":"input","opt":"","f_ord":"2","b_c":"6","val":"ghulamseratalikhan@gmail.com"},{"id":"24","name":"Telephone","req":"0","type":"input","opt":"","f_ord":"3","b_c":"6","val":"03100865443"},{"id":"25","name":"s this Property currently insured?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"4","b_c":"12","val":"No"},{"id":"26","name":"Name of insurer(s)","req":"0","type":"input","opt":"","f_ord":"5","b_c":"6","val":"Insurer Name"},{"id":"27","name":"Current insurer expiry date","req":"0","type":"date_picker","opt":"","f_ord":"6","b_c":"6","val":"2021-08-20 00:00:00"},{"id":"28","name":"D.O.B. of eldest insured","req":"0","type":"date_picker","opt":"","f_ord":"7","b_c":"6","val":"2006-08-24 00:00:00"},{"id":"29","name":"Applicant postal address","req":"0","type":"input","opt":"","f_ord":"8","b_c":"12","val":"1535 service road g-11\/2 Islamabad first floor"},{"id":"30","name":"Suburb","req":"0","type":"input","opt":"","f_ord":"9","b_c":"12","val":"Capital Terriority"},{"id":"31","name":"State","req":"0","type":"input","opt":"","f_ord":"10","b_c":"6","val":"Capital"},{"id":"32","name":"Postcode","req":"0","type":"input","opt":"","f_ord":"11","b_c":"6","val":"58000"},{"id":"33","name":"PERIOD OF INSURANCE","req":"0","type":"heading","opt":"","f_ord":"12","b_c":"12","val":""},{"id":"34","name":"Cover required From","req":"0","type":"input","opt":"","f_ord":"13","b_c":"6","val":"Rashid Ali"},{"id":"35","name":"Cover required to","req":"0","type":"input","opt":"","f_ord":"14","b_c":"6","val":"Hasan Ali"},{"id":"36","name":"INSURED DETAILS","req":"0","type":"heading","opt":"","f_ord":"15","b_c":"12","val":""},{"id":"37","name":"What are the premises you wish to insure? ","req":"0","type":"note","opt":"","f_ord":"16","b_c":"12","val":""},{"id":"38","name":"Address of property","req":"0","type":"input","opt":"","f_ord":"17","b_c":"12","val":"F-10\/4 house 45 street 345 near F-10 marakaz"},{"id":"39","name":"CONSTRUCTION DETAILS","req":"0","type":"heading","opt":"","f_ord":"21","b_c":"12","val":""},{"id":"40","name":"Cover type required","req":"0","type":"checkbox","opt":"Specified events","f_ord":"22","b_c":"4","val":""},{"id":"41","name":"OR ","req":"0","type":"note","opt":"","f_ord":"23","b_c":"4","val":""},{"id":"42","name":"","req":"0","type":"checkbox","opt":"Accidental damage (specified events + accidental damage)","f_ord":"24","b_c":"4","val":"Accidental damage (specified events + accidental damage)"},{"id":"43","name":"Year built","req":"0","type":"date_picker","opt":"","f_ord":"25","b_c":"6","val":"2014-08-22 00:00:00"},{"id":"44","name":"Number of storeys","req":"0","type":"input","opt":"","f_ord":"26","b_c":"6","val":"3"},{"id":"45","name":"Is the building elevated or multi storey?","req":"0","type":"checkbox","opt":"Single storey,Single storey elevated,Double storey,Three or more storey","f_ord":"27","b_c":"12","val":"Double storey, Three or more storey"},{"id":"46","name":"Is the property well maintained structurally sound and secured against wind and rain?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"28","b_c":"12","val":"Yes"},{"id":"47","name":"Is the property undergoing renovations over ,000, OR, under construction, OR, to be demolished?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"29","b_c":"12","val":"Yes"},{"id":"48","name":"Is your home location on a site exceeding 20,000 square metres?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"30","b_c":"12","val":"No"},{"id":"49","name":"Has the land where the building or contents are been flooded or inundated by water more than once in the last 10 years?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"31","b_c":"12","val":"Yes"},{"id":"50","name":"Is there a pool on your property?","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"32","b_c":"12","val":"Yes"},{"id":"51","name":"Building type:","req":"0","type":"checkbox","opt":"House on slab\/foundation,House on poles,Villa\/Townhouse,Unit\/Flat,Terrace\/semi,Course of construction,Granny flat,Heritage property,Holiday home,Mobile home,Display home,Nursing home Caravan,Retirement village","f_ord":"33","b_c":"12","val":"House on poles, Unit\/Flat, Holiday home, Mobile home, Display home"},{"id":"52","name":"Floor Construction:","req":"0","type":"checkbox","opt":"Concrete,Wood","f_ord":"31","b_c":"6","val":"Concrete, Wood"},{"id":"53","name":"Other","req":"0","type":"input","opt":"","f_ord":"32","b_c":"6","val":"Marble Sfloor"},{"id":"54","name":"Wall construction:If mixed construction, please detail % of each<\/small>","req":"0","type":"checkbox","opt":"Double Brick Timber\/,Brick Veneer Steel,Weatherboard,Fibro\/Asbestos,Concrete\/Hebel,Stone\/ Sandstone,Cement,Polystyrene\/EP,Mud brick","f_ord":"33","b_c":"12","val":"Weatherboard, Fibro\/Asbestos, Cement, Mud brick"},{"id":"55","name":"other","req":"0","type":"input","opt":"","f_ord":"34","b_c":"12","val":"Mud bv construction"},{"id":"56","name":"Roof:","req":"0","type":"checkbox","opt":"Cement Tiles,Iron,Slate,Terracotta,Colour bond,Steel,Wood","f_ord":"35","b_c":"12","val":"Cement Tiles, Slate, Colour bond"},{"id":"57","name":"other","req":"0","type":"input","opt":"","f_ord":"36","b_c":"12","val":"steael wooddd roof"},{"id":"58","name":"Sandwich Foam Panel construction (EPS)","req":"0","type":"checkbox","opt":"Yes,No","f_ord":"37","b_c":"4","val":"Yes"},{"id":"59","name":"If yes, percentage %","req":"0","type":"input","opt":"","f_ord":"38","b_c":"4","val":"45%"}]', true);
//count tds inside a rows when 'b_c'=6 or 'b_c'=4. 
    $tempSixCount = 0;
    $tempfourCount = 0;
    $table='<table class="table"><tbody>';
foreach ($rows as $k=>$field) {
            $field_name = $field['name'];
            $value = $field['val'];
            if ($field['b_c'] == '' || $field['b_c'] == 0) {
                $field['b_c'] = 12;
            }
    //first check if b_c=6
    if($field['b_c'] == 6){
        //opening tr @row start here for b_c=6
            if($tempSixCount==0){
               $table .='<tr>';
            }
$table .='<td colspan="2"><strong>'.$field_name.':</strong> '.$value.'</td>'; 
//increasing tds inside tr for b_c=6
           $tempSixCount++;
//second check if b_c=4
        }elseif($field['b_c'] == 4){
    //opening tr @row start here for b_c=4
            if($tempfourCount==0){
               $table .='<tr>';
            }
$table .='<td colspan=""><strong>'.$field_name.':</strong> '.$value.'</td>';
//increasing tds inside tr for b_c=4
           $tempfourCount++;
//on else condition it will check b_c=12 and also close tr for above b_c=6 and b_c=4 
      }else{
//here check if for b_c=6 has not pair of two tds inside a tr and has single td then clsoe tr and start again counting tds from 0.  
                    if($tempSixCount>0)
                    {
                        $table .='</tr>';
                        $tempSixCount = 0;
                    }
//here check if for b_c=4 has not pair of three tds inside a tr and has single td or two tds then clsoe tr and start again counting tds from 0.  
                    if($tempfourCount>0)
                    {
                        $table .='</tr>';
                        $tempfourCount = 0;
                    }
// open and clsoe tr having single tds bcoz b_c=12 has one row and one column 
                    if($field['b_c'] == 12)
                    {
$table .='<tr><td colspan="3"><strong>'.$field_name.':</strong> '.$value.'</td></tr>';
                    } 
            }
//here check if for b_c=6 has pair of two tds inside a tr then clsoe tr and start again counting tds from 0.
                if ($tempSixCount == 2)
                {
                    $table .='</tr>';
                    $tempSixCount = 0;
                }
//here check if for b_c=4 has pair of three tds inside a tr then clsoe tr and start again counting tds from 0.
                if ($tempfourCount == 3)
                {
                    $table .='</tr>';
                    $tempfourCount = 0;
                }
}
 $table .='</tbody></table>';
 echo $table;
?>
</body>
</html>