Functions with same parameter

Asked

Viewed 57 times

0

I have two functions that are triggered from the product code, my functions are used to complete the next fields, but I can’t use Join.

The correct one would be to create a part file for the second function since they use the same parameter ?

Or you can do in the same file functions with equal parameters but perform different things?

Below I’ll leave my index script

  $(document).ready(function(){
        $("input[name='codigo_produto']").on("change", function(){
                var $descri = $("input[name='descri']");
                var $barcode = $("input[name='barcode']");
                var $id = $("input[name='id']");
                var $unid = $("select[name='unid']");
                var $familia = $("select[name='familia']");
                var $tipo = $("select[name='tipo']");
                var $id_depto = $("select[name='id_depto']");
                var $grupo = $("select[name='grupo']");
                var $cod_referencial = $("input[name='cod_referencial']");
                var $codigo_interno = $("input[name='codigo_interno']");
                var $embalagem = $("input[name='embalagem']");
                var $inativo = $("input[name='inativo']");                    

                $.getJSON('function_pro-1.php',{ 
                        codigo_produto: $( this ).val(),
                            executar : 1
                },function( json ){
                        $descri.val ( json.descri );
                        $barcode.val( json.barcode );
                        $id.val( json.id );
                        $unid.val( json.unid );
                        $familia.val( json.familia );
                        $tipo.val ( json.tipo );
                        $id_depto.val( json.id_depto );
                        $grupo.val( json.grupo );
                        $cod_referencial.val( json.cod_referencial );
                        $codigo_interno.val( json.codigo_interno );
                        $embalagem.prop('checked', !!+json.embalagem);
                        $inativo.prop('checked', !!+json.inativo );
                     
                        
                });
        });
});


$(document).ready(function(){
        $("input[name='codigo_produto']").on("change", function(){

                var $un_medida = $("input[name='un_medida']");
                var $operacao = $("input[name='operacao']");
                var $fator = $("input[name='fator']");
                var $default_venda = $("input[name='default_venda']");
                var $fator_carga = $("input[name='fator_carga']");
                
                $.getJSON('function_pro-1.php',{ 
                        convunid: $( this ).val(),
                        executar : 2
                },function( json ){

                        $un_medida.val ( json.un_medida );
                        $operacao.val ( json.operacao );
                        $fator.val ( json.fator );
                        $default_venda.val ( json.default_venda );
                        $fator_carga.val ( json.fator_carga );
                });
        });
});                 

Function-1.php

    <?php
include_once("conn.php");

$opc = isset($_GET['executar']) $_GET['executar'] : 0; 

function codigo_produto($codigo_produto, $conn){
    
    $result = "SELECT * FROM cadpro WHERE codigo_produto = '$codigo_produto' ";

    $resultado = $conn->query($result);

    // DECLARA A VARIAVEL
    $valores = array();

    if($resultado){
        $row = mysqli_fetch_assoc($resultado);
        $valores['descri'] = $row['descricao'];
        $valores['barcode'] = $row['barcode'];
        $valores['id'] = $row['id'];
        $valores['unid'] = $row['unidade'];
        $valores['familia'] = $row['familia'];
        $valores['tipo'] = $row['tipo'];
        $valores['id_depto'] = $row['id_depto'];
        $valores['grupo'] = $row['grupo'];
        $valores['cod_referencial'] = $row['cod_referencial'];
        $valores['codigo_interno'] = $row['codigo_interno'];
        $valores['embalagem'] = $row['embalagem'];
        $valores['inativo'] = $row['inativo'];
    } else {
        return json_encode(array( 'error' => mysqli_error($conn) ));        
    }

    return json_encode($valores);
    
}
if($opc === 1){
    echo codigo_produto($_GET['codigo_produto'], $conn);
 }else if($opc === 2){
    echo convunid($_GET['codigo_produto'], $conn);
 }else if($opc === 3){
    echo codigo_produto($_GET['codigo_produto'], $conn);
    echo convunid($_GET['codigo_produto'], $conn);
 }else{
    echo json_encode(array('msg' => 'opcao inválida'));
 }
//    if(isset($_GET['codigo_produto'])){
//        echo codigo_produto($_GET['codigo_produto'], $conn);
//    }
//    
function codigo($codigo_produto, $conn){

       $result = "SELECT * FROM CONVUNID WHERE codigo_produto = '$codigo_produto' ";

       $resultado = $conn->query($result);

       // DECLARA A VARIAVEL
       $valores = array();

       if($resultado){
           $row = mysqli_fetch_assoc($resultado);

           $valores['un_medida'] = $row['un_medida'];
           $valores['operacao'] = $row['operacao'];                   
           $valores['fator'] = $row['fator'];
           $valores['default_venda'] = $row['default_venda'];
           $valores['fator_carga'] = $row['fator_carga'];

       } else {
           return json_encode(array( 'error' => mysqli_error($conn) ));        
       }

       return json_encode($valores);                
   }

    if($opc === 1){
    echo codigo_produto($_GET['codigo_produto'], $conn);
 }else if($opc === 2){
    echo convunid($_GET['codigo_produto'], $conn);
 }else if($opc === 3){
    echo codigo_produto($_GET['codigo_produto'], $conn);
    echo convunid($_GET['codigo_produto'], $conn);
 }else{
    echo json_encode(array('msg' => 'opcao inválida'));
 }
   
?>

  • 1

    I don’t understand the problem

  • my functions are used to complete the next fields from the product code field, but I cannot use Join, the correct would be to create a file separately or have how to do in a same file functions that run from a key field

  • 1

    Could explain what the problem is?

  • in case I have two functions to fill fields from the product code, I wonder if there is the possibility to leave the functions in the same file ? but the parameters informed are the product codes and conflict occurs

  • 1

    Maybe if [Edit] the question adding a better description of such a conflict becomes easier for those who answer

  • @Bacco I edited, see if you can answer

  • You may have N functions in the same file not understood what gives conflict nor how

  • the parameters in the functions, note that the convunid function has the same parameter of the code_product function, and this conflicts , but if I take the same function and play in another file the two work, but I did not want to create 8 files for each function

  • I think I get it now, you need just one function and not both at the same time right? Because it doesn’t send a flag together indicating which one to use.

  • in the case it is so, I have products with unit conversions, here in the company the products arrive per unit and kilogram. ai to resell can convert the unit to box, gram, etc. So if the code of the product entered has so much record in cadpro table that it refers to the first function and record in the second convunid that refers to convunid, it would have to execute both at the same time

  • how to make this flag

  • Just as you send the ajax code to php you can send another variable that tells you which function to run or both. For example, have a variable called execute the values may be 1 => to codigo_produto() 2 => convunid() and 3 for both.

  • could help me by editing only the first function for my understanding ?

Show 8 more comments

1 answer

1


Create a flag telling which function should be executed the first step is to set this in javascript.

$.getJSON('function_pro-1.php',{ 
           codigo_produto: $(this).val(),
           executar: 1

//Código omitido

In php mount an if that reads this flag and executes the function as requested.

$opc = isset($_GET['executar']) ? (int) $_GET['executar'] : 0;

if($opc === 1){
   echo codigo_produto($_GET['codigo_produto'], $conn);
}else if($opca === 2){
   echo convunid($_GET['codigo_produto'], $conn);
}else if($opc === 3){
   echo codigo_produto($_GET['codigo_produto'], $conn);
   echo convunid($_GET['codigo_produto'], $conn);
}

Browser other questions tagged

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