[英]Display two arrays in foreach loop in Php?
I want to display data in single foreach loop. 我想在单个foreach循环中显示数据。 I have two tables dailystats
and monthlystats
both of them have same columns like calls
, minutes
, incomingcalls
etc Im using Yii PHP Framework Here is my controller 我有两个表dailystats
和monthlystats
,它们都有相同的列,如calls
, minutes
, incomingcalls
等。我使用Yii PHP Framework,这是我的控制器
public function actionViewStats(){
$model = new Company();
$id = $_GET['id'];
$modelMonthly = $model->getCompanyUsageMonthly($id);
$modelDaily = $model->getCompanyUsageDaily($id);
$this->renderPartial('/shared/_company_stats', array(
'modelDaily' => $modelDaily,
'modelMonthly'=>$modelMonthly
));
}
Here is my view of table. 这是我的桌子视图。
<?PHP if(isset($modelMonthly) && sizeof($modelMonthly)!=0): ?>
<div class="ibox-content col-md-12 col-xs-12">
<div class="title col-md-2">
<h3>Current Usage</h3>
</div>
<div class="col-md-10">
<div class="col-md-12 col-xs-12">
<table class="table">
<thead>
<th>Minutes</th>
<th>Incoming Minutes</th>
<th>Calls</th>
<th>Incoming Calls</th>
</thead>
<tbody>
<?PHP
if(isset($modelMonthly)){
foreach($modelMonthly as $monthlystats){
?>
<tr>
<td><?php echo $monthlystats->minutes?></td>
<td><?PHP echo $monthlystats->incoming_minutes; ?></td>
<td><?PHP echo $monthlystats->calls; ?></td>
<td><?PHP echo $monthlystats->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
</tbody>
</table>
</div>
</div>
</div>
<?PHP endif;?>
This is showing only monthly stats modelMonthly
but i want to show daily stats modelDaily
too in the same foreach loop.. How do i do that? 这只显示每月统计modelMonthly
但我也想在同一foreach循环中显示每日统计modelDaily
..我该怎么做? I have tried array_combine etc but unable to do it. 我试过array_combine等,但无法做到。 Searched on SOF but unable to find solution for it. 在SOF上搜索,但无法找到解决方案。
My code above shows only Monthly stats like this 我上面的代码仅显示这样的每月统计信息
But I want to show like this below. 但是我想在下面显示这样。 I have already made the table but im not able to use both modelDaily
and modelMonthly
in same foreach loop. 我已经制作了表格,但无法在同一foreach循环中同时使用modelDaily
和modelMonthly
。 I have tried different things but unable to do it.. 我尝试了其他事情,但无法做到。
<?PHP
if(isset($modelMonthly)){
foreach($modelMonthly as $usage){
?>
<tr>
<td><?php echo $usage->minutes?></td>
<td><?PHP echo $usage->incoming_minutes; ?></td>
<td><?PHP echo $usage->calls; ?></td>
<td><?PHP echo $usage->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
If both your arrays have numerical indexes, you can use a regular for
loop: 如果两个数组都具有数字索引,则可以使用常规的for
循环:
for ($i = 0; $i < max(count($modelMonthly), count($modelDaily)); $i) {
if (isset($modelDaily[$i]) {
// Add the daily columns
} else {
// Add as much empty columns
}
if (isset($modelMonthly[$i]) {
// Add the monthly columns
} else {
// Add as much empty columns
}
}
The next step is adding the required headers th
in the thead
, and adding the extra td
inside the loop. 下一步是在thead
添加所需的标头th
,并在循环内添加额外的td
。
Alternatively, you can create both table independantly, and position them using CSS. 另外,您可以独立创建两个表,并使用CSS定位它们。 It is not in the scope of this question, but this is what I would recommend. 它不在此问题的范围内,但这是我的建议。
You shoudl use this tecnique 您应该使用此技术
foreach($modelMonthly as $index => $usage){
?>
<tr>
<td><?php echo $usage->minutes?></td>
<td><?PHP echo $usage->incoming_minutes; ?></td>
<td><?PHP echo $usage->calls; ?></td>
<td><?PHP echo $usage->incoming_calls; ?></td>
<td><?php if (isset($dailystats[$index]->minute)) echo $dailystats[$index]->minutes?></td>
<td><?PHP if (isset($dailystats[$index]->incoming_minutes)) echo $dailystats[$index]->incoming_minutes; ?></td>
<td><?PHP if (isset($dailystats[$index]->calls)) echo $dailystats[$index]->calls; ?></td>
<td><?PHP if (isset($dailystats[$index]->incoming_calls)) echo $dailystats[$index]->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.