【发布时间】:2018-11-28 16:37:24
【问题描述】:
我有存储在 MySQL 数据库中的登录数据。从这些数据中,我成功地能够按性别组织每月对登录进行分组。例如,1 月 1400 名男性用户和 1500 名女性用户登录。
现在我正在努力在表格中显示数据。
目前,我的设置是这样的:
<div role="tabpanel" class="tab-pane fade active in" id="tab_content1" aria-labelledby="home-tab">
<table id="datatable-fixed-header" class="table table-striped table-bordered">
<thead>
<tr>
<th>Month</th>
<th>Male Logins</th>
<th>Female Logins</th>
</tr>
</thead>
<tbody>
<?php
foreach ($maleLogin as $maleLog){
$mthDateTime = DateTime::createFromFormat('n', $maleLog->month);
foreach ($femaleLogin as $femaleLog) {
?>
<tr>
<td><?php echo $mthDateTime->format('F').' '.$maleLog->year; ?></td>
<td><?php echo $maleLog->count;?></td>
<td><?php echo $femaleLog->count;?></td>
</tr>
<?php } }?>
</tbody>
</table>
</div>
问题在于我的嵌套 for 循环。 Currently, it displays the data like so.
【问题讨论】:
-
表的结构是什么?我认为最好的方法是在同一个对象/数组上获取男性/女性日志的计数,所以你应该只使用一个 for 循环。
-
@MikeVelazco 你是什么意思结构?我必须使用两个循环的原因是因为男性和女性的登录数据来自他们接受的个人表。
标签: php html mysql codeigniter nested-loops