title: For Loop Problem Iterate Once in Codeigniter 4
body: <p>I am wondering how my code is iterate once. It should 5 iterations. But when I removed <strong>return $result</strong>, it was showing error in View <em><strong>Invalid argument supplied for foreach()</strong></em>. I've been trying by adding output code in <strong>Model</strong> and removed all code in <strong>View</strong>. Finally it show 5 iterations as what I want. But, how can I show the result in View?</p>
  <p>My Model</p>
  <pre><code>public function getAllItem()
      {
          for($i=1; $i&lt;=5; $i++){
              for($j=$i+1; $j&lt;=5; $j++){
                  $builder = $this-&gt;db-&gt;table('rating')
                              -&gt;select('user.idUser, user.nmUser, rating.idBengkel,
                                      MAX( if(rating.idBengkel = '.$i.' , rating.nilai, 0 )) itemX,
                                      MAX( if(rating.idBengkel = '.$j.' , rating.nilai, 0 )) itemY')
                              -&gt;join('user','user.idUser = rating.idUser', 'left')
                              -&gt;where('rating.idBengkel', $i)
                              -&gt;orWhere('rating.idBengkel', $j)
                              -&gt;groupBy('rating.idUser');
                  //var_dump($builder);
                  
                  $query = $builder-&gt;get();
                  $result = $query-&gt;getResult();
  
                  return $result;
  }
  </code></pre>
  <p>My Controller</p>
  <pre><code>public function pcc_method()
      { 
          $model = new Admin_model();
          $data['all'] = $model-&gt;getAllItem();
  
          echo view('templates/header', $data);
          echo view('pcc_method',$data);
          echo view('templates/footer');
      }
  </code></pre>
  <p>My View</p>
  <pre><code>&lt;table class=&quot;table table-bordered&quot;&gt;
          &lt;thead class=&quot;thead-dark&quot;&gt;
            &lt;tr&gt;
              &lt;th scope=&quot;col&quot;&gt;idUser&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;nmUser&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;itemX&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;itemY&lt;/th&gt;
            &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
            &lt;!-- Looping select db from Admin.php --&gt; 
  
            &lt;?php foreach($all as $row):?&gt;
              &lt;tr&gt;
                &lt;td&gt;&lt;?= $row-&gt;idUser;?&gt;&lt;/td&gt;
                &lt;td&gt;&lt;?= $row-&gt;nmUser;?&gt;&lt;/td&gt;
                &lt;td class=&quot;text-center col-sm-3&quot;&gt;&lt;?= $row-&gt;itemX;?&gt;&lt;/td&gt;
                &lt;td class=&quot;text-center col-sm-3&quot;&gt;&lt;?= $row-&gt;itemY;?&gt;&lt;/td&gt;
              &lt;/tr&gt;
            &lt;?php endforeach;?&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
  </code></pre>
  <p>Please give me solution for this problem.
  Regards!</p>