Use the same variable in two PHP functions

1
<?php require_once 'config.php';
// INICIA FUNCION 1
function saveInvoice( array $data){ 
    if( !empty( $data ) ){              
    global $con; 
    $count = 0; 
    if( isset($data['data'] )){

         foreach ($data['data'] as $value) {
                    if(!empty($value['length'] ))$count++;
                                }
        }
        if($count == 0)throw new Exception( "Please add atleast one item to warehouse form." );
        if( !empty( $data)){
          $codigo1 = mysqli_real_escape_string( $con, trim( $data['codigo1'] ) );
          $shipper = mysqli_real_escape_string( $con, trim( $data['shipper'] ) );
          $codigo2 = mysqli_real_escape_string( $con, trim( $data['codigo2'] ) );
          $consignee = mysqli_real_escape_string( $con, trim( $data['consignee'] ) );
          $carrier = mysqli_real_escape_string( $con, trim( $data['carrier'] ) );
          $supplier = mysqli_real_escape_string( $con, trim( $data['supplier'] ) );
          $tracking = mysqli_real_escape_string( $con, trim( $data['tracking'] ) );
          $vlb_total = mysqli_real_escape_string( $con, trim( $data['airtotal'] ) );
          $ft3_total = mysqli_real_escape_string( $con, trim( $data['oceantotal'] ) );
          $weight_total = mysqli_real_escape_string( $con, trim( $data['totalweight'] ) );
          $method = mysqli_real_escape_string( $con, trim( $data['method'] ) );
          $user = mysqli_real_escape_string( $con, trim( $data['user'] ) );
          $office = mysqli_real_escape_string( $con, trim( $data['office'] ) );
          $notes = mysqli_real_escape_string( $con, trim( $data['notes'] ) );
          $total_air_usd = mysqli_real_escape_string( $con, trim( $data['totalairusd'] ) );
          $total_ocean_usd = mysqli_real_escape_string( $con, trim( $data['totaloceanusd'] ) );                 
          $warehouse = mysqli_real_escape_string( $con, trim( $data['warehouse'] ) );

         if(empty($warehouse)){
           $uuid = uniqid();
         $query = "INSERT INTO wreceipt ('warehouse', 'codigo1', 'shipper', 'codigo2', 'consignee', 'carrier', 'supplier', 'tracking', 'vlb_total', 'ft3_total', 'weight_total', 'method', 'user', 'office', 'notes', 'total_air_usd', 'total_ocean_usd', 'created', 'uuid') VALUES ('', '$codigo1', '$shipper', '$codigo2', '$consignee', '$carrier', '$supplier', '$tracking', '$vlb_total', '$ft3_total', '$weight_total', '$method', '$user', '$office', '$notes', '$total_air_usd', '$total_ocean_usd', NOW() + INTERVAL 1 HOUR, '$uuid')";
          }else{
             throw new Exception( "Please check, some of the required fields missing" );
            }
           if(!mysqli_query($con, $query)){
            throw new Exception(  mysqli_error($con) );
            }else{
           if(empty($warehouse))$warehouse = mysqli_insert_id($con);
             }

           if( isset( $data['data']) && !empty( $data['data'] )){
           saveInvoiceDetail( $data['data'], $warehouse );
                                }
           return [
                   'success' => true,
                   'uuid' => $uuid,
                   'message' => 'Warehouse Saved Successfully.',
                   'warehouse' => $warehouse
                ]; 
         }else{
            throw new Exception( "Please check, some of the required fields missing" );             }
          }else{
        throw new Exception( "Please check, some of the required fields missing" );         
    }
}
// FINALIZA FUNCION 1

//INICIA FUNCION 2        
    function saveInvoiceDetail(array $wreceipt_items, $warehouse = '')
    {
   global $con;

  foreach ($wreceipt_items as $wreceipt_item){
  $desc = mysqli_real_escape_string( $con, trim( $wreceipt_item['desc'] ) );
  $length = mysqli_real_escape_string( $con, trim( $wreceipt_item['length'] ) );
  $width = mysqli_real_escape_string( $con, trim( $wreceipt_item['width'] ) );
  $height = mysqli_real_escape_string( $con, trim( $wreceipt_item['height'] ) );
  $weight = mysqli_real_escape_string( $con, trim( $wreceipt_item['weight'] ) );
  $quantity = mysqli_real_escape_string( $con, trim( $wreceipt_item['quantity'] ) );
  $volumeweight = mysqli_real_escape_string( $con, trim($wreceipt_item['volumeweight'] ) );
  $volume = mysqli_real_escape_string( $con, trim( $wreceipt_item['volume'] ) );
  $weightrow = mysqli_real_escape_string( $con, trim($wreceipt_item['weightrow'] ) );

  $query = "INSERT INTO wreceipt_items ('id', 'warehouse', 'desc', 'length', 'width', 'height', 'weight', 'quantity', 'volumeweight', 'volume', 'weightrow')
 VALUES (NULL, '$warehouse', '$desc', '$length', '$width', '$height', '$weight', '$quantity', '$volumeweight', '$volume', '$weightrow' )";
  mysqli_query($con, $query);
                    }
     }
//FINALIZA FUNCION 2

//INICIA FUNCION 3                 
function getInvoices($warehouse){
  global $con;
  $data = [];
  $query = "SELECT * FROM wreceipt WHERE warehouse = '$warehouse'";
  if ( $result = mysqli_query($con, $query) ){
       while($row = mysqli_fetch_assoc($result)) {
           array_push($data, $row);
              }
         }    return $data; } ?>
//FINALIZA FUNCION 3

This is the whole code, I can not get the value of the variable $ warehouse = mysqli_insert_id ($ with); leave the first function to use it in the second function, specifically here: $ query="SELECT * FROM wreceipt WHERE warehouse = '$ warehouse '"; . This query gives me the ROW of "warehouse" .. is what I can not do. Some help? I would pay for the solution, I have made too many attempts to find the way it works but the value of Warehouse seems that it does not leave the function 1

getInvoices is shown in another php document more or less like this:

<?php
require_once 'config.php';
require_once 'warehouse-save.php';

    $invoices = getInvoices($warehouse);

        if( !empty( $invoices ) ){ 
        foreach ($invoices as $value) {
        <?= $value['warehouse'] ?>
            } 
        }else{
        echo "No Invoice Found";
        }
?>
Pero el problema no debe ser aqui porque todo funciona bien cuando cambio la variable warehouse en el query por un valor fijo. POR EJEMPLO "SELECT * FROM wreceipt WHERE warehouse = 420 " ;**. (este funciona).
    
asked by Irving Ngr 26.07.2017 в 00:25
source

1 answer

0

You need to indicate the parameter in the function:

function getInvoices(){
//                   ^

The code should look more like this:

function getInvoices($warehouse){
//                   ^^^^^^^^^^

Edit

<?php
require_once 'config.php';
require_once 'warehouse-save.php';

$invoices = getInvoices($warehouse);

That variable $warehouse Where did you declare it? In this context, it is a global variable ... however in your functions you are using it as a local variable.

I do not know, maybe a solution would suffice that:

function saveInvoice( array $data){ 
  global $con;
  global $warehouse;
  // ...
}

But you'll have to test it to see if it's useful ... Of course, you'll have to make sure that before calling getInvoices($warehouse) you've called saveInvoice() or $warehouse it will not be initialized.

    
answered by 26.07.2017 в 00:27