Select model according to the brand

1

I have the function that returns all the registered brands: (With fields ID and BRAND)

public function lista(){

        $lista = array();

        $n = 0;
        $db = new mysql();
        $exec = $db->executar("SELECT * FROM automovel_marca order by marca asc");
        while($data = $exec->fetch_object()){

            $lista[$n]['id'] = $data->id;
            $lista[$n]['marca'] = $data->marca;

            $n++;
        }
        return $lista;
}

And the function that returns the models: (With fields ID, IDMARCA and MODEL)

public function lista(){

        $lista = array();

        $n = 0;
        $db = new mysql();
        $exec = $db->executar("SELECT * FROM automovel_modelo order by modelo asc");
        while($data = $exec->fetch_object()){

            $lista[$n]['id'] = $data->id;
            $lista[$n]['idmarca'] = $data->idmarca;
            $lista[$n]['modelo'] = $data->modelo;

            $n++;
        }
        return $lista;
}

I was able to pull them individually in FORMS, the problem is that I wanted the FORM MODEL field of the FORM to be related to the MARK field, when the person selects the mark it displays all the models referring to that mark.

The DB structure looks like this:

automovel_marca (id, brand) automovel_modelo (id, idmarca, modelo) - in this case the idmarca follows the relation of the automovel_marca id

    
asked by anonymous 23.08.2017 / 14:52

1 answer

1

I do not know the structure of your tables, but let's basically change the two locations below in the car model_list method:

public function lista($idmarca){

        $exec = $db->executar("SELECT * FROM automovel_modelo where idmarca = ".$idmarca." order by modelo asc");

Then when calling the list of model_objects you would pass the parameter $ idmarca.

    
23.08.2017 / 14:55