在PHP中使用关联数组创建多个html表
我有一个数组,可以访问数据,但是当我尝试基于一个键创建单独的表时,该键设置为从数据库中提取的数据所属的数字月份在PHP中使用关联数组创建多个html表,php,html,foreach,associative-array,Php,Html,Foreach,Associative Array,我有一个数组,可以访问数据,但是当我尝试基于一个键创建单独的表时,该键设置为从数据库中提取的数据所属的数字月份 I have: array(1) { [5]=> array(12) { ["ID"]=> string(5) "1001" ["created_timestamp"]=> string(26) "2015-06-11 19:10:00" ["firstname"]=> string(4) "John"
I have:
array(1) {
[5]=>
array(12) {
["ID"]=>
string(5) "1001"
["created_timestamp"]=>
string(26) "2015-06-11 19:10:00"
["firstname"]=>
string(4) "John"
["lastname"]=>
string(3) "Doe"
}
}
array(1) {
[5]=>
array(12) {
["ID"]=>
string(5) "1002"
["created_timestamp"]=>
string(26) "2015-06-12 19:10:00"
["firstname"]=>
string(4) "Jane"
["lastname"]=>
string(3) "Doe"
}
}
array(1) {
[6]=>
array(12) {
["ID"]=>
string(5) "1003"
["created_timestamp"]=>
string(26) "2015-07-16 19:20:00"
["firstname"]=>
string(4) "John"
["lastname"]=>
string(4) "John"
}
}
[5]表示当前月份-1。我要做的是创建一个表和标题,但只创建一次,然后用名字和姓氏填充该表
foreach($m as $key => $value)
{
if(key($m) != $currentKey)
{
$currentKey = key($m);
}
if($key == date("n", strtotime($value['created_timestamp']))-1);
{
for($i=$currentKey; $i<=$lastkey; $i++) {
echo "<br>".count($key);
$table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
<thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
$table .= '<tbody>';
$table .= '<tr><td>'.date("F Y", strtotime($m[$currentKey]['created_timestamp'])).'</td><td>' . $value['ID'] . '</td><td>' . $value['lastname'] . ' ' . $value['firstname'] . '</td></tr>';
$table .= '</tbody>';
$table .= '</table>';
}
}
}
foreach($m作为$key=>$value)
{
if(key($m)!=currentKey)
{
$currentKey=key($m);
}
如果($key==date(“n”,strottime($value['created\u timestamp']))-1);
{
对于($i=$currentKey;$i一个脏的解决方案。因为缺少信息
以下是一些假设:
-数组按ASC顺序排序
-每个数组中只有一项[ID、名称、时间戳]
-如果有更多项目,则可以在其中添加一个循环
-$m是多维数组。
注:未测试
$currentKey = 0;
$count= 0;
$lastKey = count($m) - 1;
foreach($m as $key => $value) {
if(key($m) != $currentKey) {
$currentKey = key($m);
if($count== 0) {
echo "<br>".count($key);
$table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
<thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
$table .= '<tbody>';
} else {
$table .= '</tbody>';
$table .= '</table>';
$table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
<thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
$table .= '<tbody>';
}
}
if($key == date("n", strtotime($value['created_timestamp']))-1) {
$table .= '<tr><td>'.date("F Y", strtotime($value['created_timestamp'])).'</td><td>' . $value['ID'] . '</td><td>' . $value['lastname'] . ' ' . $value['firstname'] . '</td></tr>';
}
if($lastKey == $count) {
$table .= '</tbody>';
$table .= '</table>';
}
$count++;
}
$currentKey=0;
$count=0;
$lastKey=计数($m)-1;
foreach($m作为$key=>$value){
if(key($m)!=currentKey){
$currentKey=key($m);
如果($count==0){
回显“
”.count($key);
$table.='
蒙蒂德客户;
$table.='';
}否则{
$table.='';
$table.='';
$table.='
蒙蒂德客户;
$table.='';
}
}
如果($key==date(“n”,strottime($value['created\u timestamp']))-1){
$table.=''.date(“fy”,strottime($value['created_timestamp'])。'$value['ID'].'$value['lastname'].'$value['firstname'.';
}
如果($lastKey==$count){
$table.='';
$table.='';
}
$count++;
}
$m
是你的多维数组吗?没错,伙计。看来我只是看错了方向。谢谢!酷..很高兴我能帮忙..干杯!