Pass external variable or Constant into a Function

Asked

Viewed 1,031 times

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'] . '&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;
    }  
    ?>
  • 1

    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 ?

  • This variable comes from another query that lists the type of vehicle

  • Okay, you can edit the question and add that other part as well ?

  • 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

  • I just wanted the Where type = '$ptv to get that variable #$ptv

  • Look, comment on the rest of the code, and do: print($ptv) or echo $ptv. And say what you’ve returned.

  • It’s coming back Van

  • 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.

  • 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.

Show 4 more comments

2 answers

1

Change the function signature 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');

0

according to the documentation in this link, there are some ways to do

<?php

  $a = 1; /* escopo global */

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

  Teste();
?>

or using the global variables

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

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

      $b = $a + $b;
  }

  Soma();
  echo $b;
?>

but I think it’s wise 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 in the link above...

  • I tested this last example and gave this error... Warning: Missing argument 2 for get_templates(), called in

  • When you call function you passed the second parameter? get_modelos($id_marca,$ptv) but remember to start the variable and make sure it is valid...

Browser other questions tagged

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