2016-06-14 8 views
0

私は単一のforeachループでデータを表示します。私はそれらの両方がcallsminutesincomingcallsのように同じ列を持つ2つのテーブルdailystatsmonthlystatsを持っているなどイム使ってYiiのPHPフレームワークは、ここに私のコントローラphpのforeachループに2つの配列を表示しますか?

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 
     )); 
} 

ここでは、テーブルの私の見解であるです。

 <?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;?> 

私はそれをどのように行うのです。これはmodelMonthlyのみ毎月の統計を示しているが、私は同じforeachループでは、あまりにも毎日の統計にmodelDailyを表示したいですか?私はarray_combineなどを試みたが、それを行うことができません。 SOFで検索しましたが、解決策を見つけることができませんでした。上記

私のコードは、この

enter image description here

のようなだけで毎月の統計を示しています。しかし、私は以下のように表示したいです。私はすでにテーブルを作っていますが、modelDailymodelMonthlyの両方を同じforeachループで使用することはできません。

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 
    } 
} 

次のステップ:私は、両方のあなたの配列は数値インデックスを持っている場合は、あなたが定期的にforループを使用することができます

enter image description here

<?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 } } ?> 

答えて

1

..異なるものを試してみましたが、それを行うことができていますtheadに必要なヘッダーthを追加し、余分にtdをループ内に追加しています。


また、あなたはindependantly両方のテーブルを作成し、CSSを使用してそれらを配置することができます。この質問の範囲にはありませんが、これが私が推奨するものです。

+0

パーフェクトを使用shoudl。私はあなたの両方の答えから 'forループ'と 'alternative'をヒントしました。独立して2つのテーブルを使用するのではなく、上記の答えのように' else else'の中に 'foreachループ 'を2つ使った' one table'を使用しました –

+0

Glad Iあなたは私の答えを明瞭にするために受け入れられたものとしてマークすることができます(さらに私のためのブラニーポイント!)。 –

0

あなたはこの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 } } ?> 
+0

これは危険なことに、両方の配列が同じ数の項目を持つと仮定します。ここにはこれが当てはまるかもしれませんが、あなたは確信する方法がありません。 –

+0

@HypolitePetovanはい.. issetの方が良いです – scaisEdge

関連する問題