Welcome Guest, Not a member yet? Register   Sign In
DataMapper CodeIgniter - Dropdown list
#1

[eluser]Unknown[/eluser]
I want to have a dropdown list with brands, and depending on the choice, it will appear the products of the brand selected.

My Tables

Code:
Brands
==================
id   |   Brand
---------------
1        Apple
2        Android

Code:
Products
========================================================================
id |    Name           |  Price |  Image      |  Category | Brand_id
------------------------------------------------------------------------------
1    Macbook Air          1000    mac1.jpg        computer       1
2    Macbook Pro          1500    mac2.jpg       computer        1
3    Android S3           400      s3.jpg          mobile        2


My Models

Brands.php

Code:
<?
        class Brands extends DataMapper {
           var $table = 'brands';
           var $has_many = array('Products');
        }
    ?>


Products.php

Code:
<?
        class Products extends DataMapper {
            var $table = 'products';
            var $has_one = array('Brands');
        }
    ?>


My Controller

ListBrands.php

Code:
<?

class ListBrands extends CI_Controller {
    function __construct() {
        parent::__construct();
    }

    function index() {
        $this->load->view('listbrands_view');
    }
}
?>



I don't know if my controller or models are correctly and also I don't know how to do what I'm asking for, a dropdown that will print the products of the brand selected.
#2

[eluser]WanWizard[/eluser]
There is nothing in your controller, so lets start with 'the brand selected'. Where do you select that? I assume you have some other controller with a form that contains a brands dropdown?

If not, you need that first.

First, make sure you have the array extension loaded (manually or in your config file). You can then create a key-value array for an HTML select using:

Code:
$b = new Brands;
$b->get();
$dropdown = $b->all_to_single_array('Brands');

Then pass $dropdown to your view to generate the select...




Theme © iAndrew 2016 - Forum software by © MyBB