【问题标题】:HTML table based on data presented in an array基于数组中数据的 HTML 表格
【发布时间】:2018-09-24 14:09:58
【问题描述】:

我需要根据存储在数组中的数据制作一个 HTML 表格。

首先,我有如下嵌套数组:

$cars=Array(

Array("DDT","Detroit","USA",

    Array(
                Array("DDT-55555","1x1","100Hp"),
                Array("DDT-66666","2x2","200Hp"),
                Array("DDT-77777","3x3","300Hp"),
                Array("DDT-77777","4x4","400Hp")
    )),


Array( "AMM","London","UK",

    Array(
                Array("AMM-888","6x6","500Hp")
    )),

);

$countries=Array();

然后我有一个带有选项标签的表单,用于选择命名国家:

echo "<form method='post' action='***.php'>

<select name='country' onchange='this.form.submit()'>";
foreach($countries as $country){
        echo "<option>".$country;
}   


echo "</select></form>";

我需要的是一个 HTML 表格,它根据我从选择表单中选择的内容从汽车数组中生成数据,如下所示:

<thead>
   <tr>
     <th>Mark</th>
     <th>City</th>
     <th>Country</th>
     <th>Model</th>
     <th>Numbers</th>
     <th>Horsepower</th>
   </tr>
</thead>

不能让它工作..

【问题讨论】:

  • 嗨,亚历克斯,如果我理解正确,您想根据您的选择表单中的依赖值插入包含现有数组中的值的表吗?
  • 基本上是的..

标签: php html arrays html-table


【解决方案1】:

你需要检查每个数组的循环

$table= array();
foreach($cars as $currCar){    
    if($currCar[$selectedValue]){
      $table = key($currCar[$selectedValue])+1;
    }    
}
if(count($table)){
echo "<tbody>";
foreach($table as $currRow){
echo "<tr>";
    foreach($currRow as $currTd){
      echo "<td>";
        $currTd;
      echo "</td>";
    }
   echo "</tr>";
}
 echo "</tbody>";
}

【讨论】:

  • 你们的国家有同样的钥匙吗?来自country 数组和cat 数组
  • 您需要获取value并与$selectedValue交换
猜你喜欢
  • 2016-05-01
  • 2012-05-05
  • 1970-01-01
  • 2014-06-14
  • 2012-12-20
  • 1970-01-01
  • 2010-11-28
  • 2021-09-20
  • 2016-12-14
相关资源
最近更新 更多