Pass external variable or Constant into a Function

0

How to pass a variable into a function with this variable on the outside. I do not know what else I can 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'] . '&nbsp; Placa:&nbsp;' . $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'] .   
   '&nbsp; Telefone:&nbsp; ' . $cor['tel'] . '</option>';   
    }

    $txt2 .= "</select>";

    echo $txt2;
    break;
    }  
    ?>
    
asked by anonymous 03.10.2015 / 23:58

2 answers

1

Change the signature of the function by adding the new parameter and if you want to set a default value.

function get_modelos($id_marca, $ptv=10) {

Then just call:

get_modelos($id_marca, 'van');
    
04.10.2015 / 00:01
0

According to the documentation in this link , there are some ways to do it.

<?php

  $a = 1; /* escopo global */

  function Teste()
  {
    echo $a; /* referencia uma variável do escopo local (não definida) */
  }

  Teste();
?>

or even using global variables

<?php
  $a = 1;
  $b = 2;

  function Soma()
  {
      global $a, $b;

      $b = $a + $b;
  }

  Soma();
  echo $b;
?>

but I think it's the sensible thing to pass the variable as a function parameter

<?php
  $ptv = 'Van';//   Essa é a variavel que entrar no Where do select

  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;
  }
?>

All these examples are at the link above ...

    
04.10.2015 / 00:13