0
How to pass a variable into a function with that variable on the outside. I don’t know what else to do .
<?php
$id_transfer = $_SESSION["id_transfer"];
$ptv = $_GET["tipo"];
/////// Função Pegar o Fornecedor//////////////
function get_marcas() {
$idt = IDT;
$uf2 = UF;
$sql = "select * from fornecedores where id_transfer = $idt AND uf =
'$uf2'";
$result = mysql_query($sql) or die(mysql_error());
$marcas = array();
while($row = mysql_fetch_assoc($result)){
$marcas[] = $row;
}
return $marcas;
}
/////// Função Pegar o Veiculo//////////////
function get_modelos($id_marca,$ptv) {
if(!ctype_digit($id_marca)) return array();
$sql = sprintf("select * from veiculos where tipo = '$ptv' AND
id_fornecedor = %d",
$id_marca);
$result = mysql_query($sql) or die(mysql_error($conexao));
$modelos = array();
while($row = mysql_fetch_assoc($result)){
$modelos[] = $row;
}
return $modelos;
}
switch ($_POST['acao']) {
case "exibeModeloSelect":
$txt = '<select name="id_veiculo" class="form-control">';
$txt .= '<option value="">Selecione o Veiculo</option>';
foreach(get_modelos($_POST['id_marca'],$ptv) as $modelo) {
$txt .= '<option value="'.$modelo['id_veiculo'].'">' .
$modelo['modelo'] . ' Placa: ' . $modelo['placa'] .
'</option>';
}
$txt .= "</select>";
echo $txt;
break;
}
/////// Função Pegar o Motorista//////////////
function get_cor($id_marca) {
if(!ctype_digit($id_marca)) return array();
$id_marca = mysql_real_escape_string($id_marca);
$sql = sprintf("select * from motoristas where id_fornecedor = %d",
$id_marca);
$result = mysql_query($sql) or die(mysql_error());
$modelos = array();
while($row = mysql_fetch_assoc($result)){
$cor[] = $row;
}
return $cor;
}
switch ($_POST['acao']) {
case "exibeModeloSelect":
$txt2 = '<br><select name="id_motorista" class="form-control">';
$txt2 .= '<option value="">Selecione o Motorista</option>';
foreach(get_cor($_POST['id_marca']) as $cor) {
$txt2 .= '<option value="'.$cor['id'].'">' . $cor['nomecompleto'] .
' Telefone: ' . $cor['tel'] . '</option>';
}
$txt2 .= "</select>";
echo $txt2;
break;
}
?>
There are several ways to enter the value of this variable in the scope of the function, as you can see in the answers below, exemplify several ways to do it. To be clearer and more objective, can you tell where the value of this variable comes from? Or where you want it to come from ?
– Edilson
This variable comes from another query that lists the type of vehicle
– Fabio Henrique
Okay, you can edit the question and add that other part as well ?
– Edilson
I put the complete code but I did not put the query because it would be too big the code here, but I put up there a variable as if it was coming from a $_GET that would be in the same
– Fabio Henrique
I just wanted the Where type = '$ptv to get that variable #$ptv
– Fabio Henrique
Look, comment on the rest of the code, and do:
print($ptv)
orecho $ptv
. And say what you’ve returned.– Edilson
It’s coming back Van
– Fabio Henrique
Obs in the first function "Function get_marks() { " I can put variables inside it coming from constants more in the second nor do constants work.
– Fabio Henrique
Have you tried returning the value of
$_POST['id_marca']
? Look, if possible, put the whole code on Pastebin together with the tables in use.– Edilson