Welcome Guest, Not a member yet? Register   Sign In
Best Method to store a Checkbox
#6

First, If I were you I would do the sql part in the Model file. Also a foreach in another one is nod adviced. It's not good. You need to handle it in sql.


In your Model :


PHP Code:
public function getCategoriesWithProducts() {
        $builder $this->db->table('categories');
        $builder->select('categories.category_name, products.product_name');
        $builder->join('products''categories.category_id = products.category_id''left');
        $builder->orderBy('categories.category_name');
        return $builder->get()->getResultArray();
    





In Your Controller:


PHP Code:
public function showCategoriesWithProducts() {
    $categoryModel = new CategoryModel();
    $data['results'] = $categoryModel->getCategoriesWithProducts();
    return view('categories_view'$data);






And In your View:


PHP Code:
<?php foreach ($results as $row): ?>
    <h2><?= $row['category_name']; ?></h2>
    <p><?= $row['product_name']; ?></p>
<?php endforeach; ?>






In bocca al lupo!
Reply


Messages In This Thread
Best Method to store a Checkbox - by elbambolo - 01-02-2024, 09:32 AM
RE: Best Method to store a Checkbox - by demyr - 01-02-2024, 02:10 PM
RE: Best Method to store a Checkbox - by demyr - 01-03-2024, 04:20 AM
RE: Best Method to store a Checkbox - by demyr - 01-07-2024, 09:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB