Here is my database :
-id product prodcut-company category
-1 latitude dell laptop
-2 . acer desktop
-3 . azus mouse
-4 . sony keyboard
+If I have many product separated by category, then I want loop it by category how can I coding and show it in one page then list it as a block like laptop , desktop,...
please help ! thank you
You may create a method in the model like this-
public function getData(){
$this->db->from("product_table");
$this->db->order_by("category");
$q = $this->db->get();
if($q->num_rows() > 0){
return $q->result();
}
return [];
}
Fetch data from model using this method in the controller and pass this value to the view.
$data[
'products' => $this->your_model->getData()
];
$this->load->view('your_view',$data);
Loop these values and print in the view.
...
<?php
if(!empty($products)){
$count = count($products);
$i = 0;
while($i < $count){
$category = $products[$i]->category;
echo '<h3>Category: '.$category.'</h3>';
echo '<table>
<tr><th>Product</th><th>Company</th><th>Category</th></tr>
';
for(; $i < $count && $category == $products[$i]->category ; $i++ ){
echo '
<tr>
<td>'.$products[$i]->product.'</td>
<td>'.$products[$i]->company.'</td>
<td>'.$products[$i]->category.'</td>
</tr>
';
}
echo '</table>';
}
}
?>
...