How to Exchange Arrays query for Selects query in Mysql

Asked

Viewed 202 times

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;
  }
  • 1

    vc should store this data in a database. Mysql, Sqlite, Postgre, SQL Server, Oracle, Firebird, etc..

  • this I have already stored. I wanted to know how to pass the query to mysql

  • Are you using PDO or Mysqli?

  • For now in mysql, even though I know it is already in disuse...

  • I didn’t understand the problem, um while simple resolve, inside it, make the assignment, $tabela_modelo[] = $row;

  • So I tried and it didn’t work ... I put here how I did the query http://jsfiddle.net/cz4ph2w2/

Show 1 more comment

2 answers

2


By the example shown, I believe that just do the assignment in the array and return it at the end of the function, thus:

ctype_digit() checks whether $id_marca is integer otherwise returns an empty array and sprintf() force the sql formatting, if %d whole-wheat.

function get_marcas() {
   $conexao = get_conexao();
   $sql = "select * from tabela_marca";
   $result = mysql_query($sql, $conexao) or die(mysql_error($conexao));

   $marcas = array();
   while($row = mysql_fetch_assoc($result)){
      $marcas[] = $row;
   }
   return $marcas;
}

function get_modelos($id_marca) {
   if(!ctype_digit($id_marca)) return array();

   $conexao = get_conexao();

   $sql = sprintf("select * from tabela_modelo where id_marca = %d", $id_marca);
   $result = mysql_query($sql, $conexao) or die(mysql_error($conexao));

   $modelos = array();
   while($row = mysql_fetch_assoc($result)){
      $modelos[] = $row;
   }
   return $modelos;
}

Manual - sprinf

Manual - ctype_digit

  • Perfect was exactly what I wanted.... only one thing... when the second select the "Where id_tag = $id_tag does not find an id it from this error "Warning: Invalid argument supplied for foreach() in"... ?

  • You can’t hide it, you need to treat it.

  • @Fabiohenrique, I changed the answer see if you answer now.

0

Try it like this

$tabela_modelos = array();
$tabela_modelos[] = array('id_marca' => 1, 'id_modelo' => 1, 'ds_modelo' => 'Vectra');
$tabela_modelos[] =array('id_marca' => 1, 'id_modelo' => 2, 'ds_modelo' => 'Corsa');
$tabela_modelos[] =array('id_marca' => 1, 'id_modelo' => 3, 'ds_modelo' => 'Meriva');
$tabela_modelos[] =array('id_marca' => 2, 'id_modelo' => 4, 'ds_modelo' => 'Uno');
$tabela_modelos[] =array('id_marca' => 2, 'id_modelo' => 5, 'ds_modelo' => 'Tempra');
$tabela_modelos[] =array('id_marca' => 2, 'id_modelo' => 6, 'ds_modelo' => 'Pálio');
$tabela_modelos[] =array('id_marca' => 3, 'id_modelo' => 7, 'ds_modelo' => 'Ranger');
$tabela_modelos[] =array('id_marca' => 3, 'id_modelo' => 8, 'ds_modelo' => 'Eco');
$tabela_modelos[] =array('id_marca' => 3, 'id_modelo' => 9, 'ds_modelo' => 'Fiesta');

$cadastros = 0;

foreach($tabela_modelos as $tabela_modelo){
$id_marca = $tabela_modelo['id_marca'];
$id_modelo = $tabela_modelo['id_modelo'];
$ds_modelo = $tabela_modelo['ds_modelo'];

$sql = 'SELECT * FROM TABELA WHERE CAMPO1 = "'.$id_marca.'"' ';

if(mysql_query($sql)){
$cadastrados++
}
}
  • Hello Friend sorry for my iguinorancia.. but I did not understand what you did

  • Don’t you want to make a query with expensive $table_templates array?? then I am doing the foreach to go through the array and insert in the variables and then I do the query by passing the variable, this query was just an example of how to concatenate the variable in the query

  • No... I want to make a query in the database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.