1

tb_stock:

+------------+--------------+-------------+------------+
| id_stock   | name_stock   |  brand_id   |  price     |
+------------------------------------------------------+
| 1          | iPhone XS    |      1      |  1299      |
| 2          | galaxy s10   |      2      |  1000      |
| 3          | galaxy s10+  |      2      |  1100      |
| 4          | IWatch       |      1      |  500       |
+------------------------------------------------------+

From table above, i want to show only one brand in my checkbox loop. I have used foreach to get the value. But i got all of the brand in my checkbox. what i want is show only one brand and loop it.

controller:

public function index(){
$data['getStok'] = $this->M_input->getStok();
this->page->view('product', $data);

}

model:

public function getStok(){
  $query = $this->db->get('tb_stok');
   return $query->result();
}

view:

<?php if(count($getStok)):?>
   <?php foreach ($getStok as $a):?>
    <div class="form-group">
       <div class="col-sm-12">
         <input type="checkbox" class="minimal" 
                id="<?php echo $a->name_stok;?>" 
                value=<?php echo $a->price;?>>

       <label><?php echo $a->name_stok;?></label>
            </div>
        </div>
      <?php endforeach;?>
    <?php else :?>
  <?php endif ;?>
  • I'm afraid It's unsure what the actual question is. Could you elaborate the `what i want is show only one brand and loop it.` means? – dev-cyprium Mar 22 '19 at 21:47
  • @dev-cyprium there's 2 brand_id in my table "1, 2", what i want is loop my table data and show only one of brand data that have id '1' or '2'. so, if i loop data where brand_id = 1, it will loop apple brand only. – codingan sampah Mar 23 '19 at 07:26

1 Answers1

0

If you need to show only one brand at a time, you can do it in several ways:

  1. Filter only one brand in the view for loop. This is not the optimal way, because you move your business logic to the View level of MVC.
  2. Filter a single brand in the model. This is a better way, because Model considered to be a proper place for business logic.

Here you can find more explanations about business logic in MVC

You can achieve the second approach with just a few changes in your model (and controller)

controller:

public function index() 
{
    // assuming you get the brand ID to display from GET request 
    $brandId = $this->input->get('brand_id');
    $data['getStok'] = $this->M_input->getStokByBrandId($brandId);
    this->page->view('product', $data);
}

model:

public function getStokByBrandId($brandId)
{
    $query = $this->db->where('brand_id', $brandId)->get('tb_stok');
    return $query->result();
}

view:

no changes needed
krlv
  • 2,310
  • 1
  • 12
  • 15
  • Actually i have solved this problem. same as your model, i doing it in the same way. Thanks for your response. I'll mark your answer as accepted answer. – codingan sampah Mar 23 '19 at 07:16