I am wondering how my code is iterate once. It should 5 iterations. But when I removed return $result, it was showing error in View Invalid argument supplied for foreach(). I've been trying by adding output code in Model and removed all code in View. Finally it show 5 iterations as what I want. But, how can I show the result in View?
My Model
public function getAllItem()
{
for($i=1; $i<=5; $i++){
for($j=$i+1; $j<=5; $j++){
$builder = $this->db->table('rating')
->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')
->join('user','user.idUser = rating.idUser', 'left')
->where('rating.idBengkel', $i)
->orWhere('rating.idBengkel', $j)
->groupBy('rating.idUser');
//var_dump($builder);
$query = $builder->get();
$result = $query->getResult();
return $result;
}
My Controller
public function pcc_method()
{
$model = new Admin_model();
$data['all'] = $model->getAllItem();
echo view('templates/header', $data);
echo view('pcc_method',$data);
echo view('templates/footer');
}
My View
<table class="table table-bordered">
<thead class="thead-dark">
<tr>
<th scope="col">idUser</th>
<th scope="col">nmUser</th>
<th scope="col">itemX</th>
<th scope="col">itemY</th>
</tr>
</thead>
<tbody>
<!-- Looping select db from Admin.php -->
<?php foreach($all as $row):?>
<tr>
<td><?= $row->idUser;?></td>
<td><?= $row->nmUser;?></td>
<td class="text-center col-sm-3"><?= $row->itemX;?></td>
<td class="text-center col-sm-3"><?= $row->itemY;?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
Please give me solution for this problem. Regards!
I am sorry to answer this because the problem is in VIEW
and I've asked my friend who gave good solution. Like what @Javier Larroulet
was said that the array has to be stored into new declared Array
. In my code I am using $all[]
that storing array $result
.
Model
public function getAllItem()
{
for($i=1; $i<=5; $i++){
for($j=$i+1; $j<=5; $j++){
$builder = $this->db->table('rating')
->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')
->join('user','user.idUser = rating.idUser', 'left')
->where('rating.idBengkel', $i)
->orWhere('rating.idBengkel', $j)
->groupBy('rating.idUser');
//var_dump($builder);
$query = $builder->get();
$result = $query->getResult();
$all[] = $result;
}
}
return $all;
}
In View, I was checking with <?php echo $all[0][0]->idUser;?>
to show the result and I got it. Then I use nested loop
because that is corresponding.
View
<?php foreach($all as $row):?>
<table class="table table-bordered">
<thead class="thead-dark">
<tr>
<th scope="col">idUser</th>
<th scope="col">nmUser</th>
<th scope="col">itemX</th>
<th scope="col">itemY</th>
</tr>
</thead>
<tbody>
<?php foreach($row as $key => $val):?>
<tr>
<td><?= $val->idUser;?></td>
<td><?= $val->nmUser;?></td>
<td class="text-center col-sm-3"><?= $val->itemX;?></td>
<td class="text-center col-sm-3"><?= $val->itemY;?></td>
</tr>
<?php endforeach;?>
<?php endforeach;?>
</tbody>
</table>