1
I’m not sure how to exchange arrays for query in sql, in this code below you have an array with records like to know how to exchange the array $tabela_modelo
by a select in Mysql using the same array columns:
function get_marcas() {
// Aqui criamos um array bidimensional, que poderá vi do banco de
// dados da mesma forma
// basta você fazer um select * from tabela_marca -> a tabela_marca
// deve conter: id_marca, ds_marca
$marcas = array(
array('id_marca' => 11, 'ds_marca' => 'JW'),
array('id_marca' => 1, 'ds_marca' => 'Transfer'),
);
return $marcas;
}
function get_modelos($id_marca) {
// select * from tabela_modelo where id_marca = $id_marca
//
$tabela_modelo = array(
array('id_marca' => 1, 'id_modelo' => 1, 'ds_modelo' => 'Vectra'),
array('id_marca' => 1, 'id_modelo' => 2, 'ds_modelo' => 'Corsa'),
array('id_marca' => 1, 'id_modelo' => 3, 'ds_modelo' => 'Meriva'),
array('id_marca' => 2, 'id_modelo' => 4, 'ds_modelo' => 'Uno'),
array('id_marca' => 2, 'id_modelo' => 5, 'ds_modelo' => 'Tempra'),
array('id_marca' => 2, 'id_modelo' => 6, 'ds_modelo' => 'Pálio'),
array('id_marca' => 3, 'id_modelo' => 7, 'ds_modelo' => 'Ranger'),
array('id_marca' => 3, 'id_modelo' => 8, 'ds_modelo' => 'Eco'),
array('id_marca' => 3, 'id_modelo' => 9, 'ds_modelo' => 'Fiesta')
);
$modelo = array();
$cont = 0;
for($i=0; $i < count($tabela_modelo); $i++) {
if($tabela_modelo[$i]['id_marca'] == $id_marca) {
$modelo[$cont]['id_marca']= $tabela_modelo[$i]['id_marca'];
$modelo[$cont]['ds_modelo'] = $tabela_modelo[$i]['ds_modelo'];
$cont++;
}
}
return $modelo;
}
switch ($_POST['acao']) {
case "exibeModeloSelect":
$txt = '<select name="id_motorista">';
$txt .= '<option value="">Selecione o Motorista</option>';
foreach(get_modelos($_POST['id_marca']) as $modelo) {
$txt .= '<option value="'.$modelo['id_modelo'].'">' .
$modelo['ds_modelo'] . '</option>';
}
$txt .= "</select>";
echo $txt;
break;
}
vc should store this data in a database. Mysql, Sqlite, Postgre, SQL Server, Oracle, Firebird, etc..
– Daniel Omine
this I have already stored. I wanted to know how to pass the query to mysql
– Fabio Henrique
Are you using PDO or Mysqli?
– rray
For now in mysql, even though I know it is already in disuse...
– Fabio Henrique
I didn’t understand the problem, um
while
simple resolve, inside it, make the assignment,$tabela_modelo[] = $row;
– rray
So I tried and it didn’t work ... I put here how I did the query http://jsfiddle.net/cz4ph2w2/
– Fabio Henrique