arrays - Use the same variable for two PHP functions -
<?php require_once 'config.php'; // function 1 begins function saveinvoice( array $data){ if( !empty( $data ) ){ global $con; $count = 0; if( isset($data['data'] )){ foreach ($data['data'] $value) { if(!empty($value['length'] ))$count++; } } if($count == 0)throw new exception( "please add atleast 1 item 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 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, of 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, of required fields missing" ); } } else{ throw new exception( "please check, of required fields missing" ); } } // function 1 ends // function 2 begins function saveinvoicedetail(array $wreceipt_items, $warehouse = ''){ global $con; foreach ($wreceipt_items $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 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); } } function getinvoices($warehouse){ global $con; $data = []; $query = "select * wreceipt warehouse = '$warehouse'"; if ( $result = mysqli_query($con, $query) ){ while($row = mysqli_fetch_assoc($result)) { array_push($data, $row); } } return $data; } ?> // function 2 ends
i have 2 functions: 1 registers , other shows data. interested in second one, since not take value of $warehouse variable (of first function) use in query of second function here: $query = "select * wreceipt warehouse = $warehouse
does have idea why happens?
pd: use show (function 2) data: <?php $invoices = getinvoices($warehouse); if( !empty( $invoices ) ){ foreach ($invoices $value) { $value['warehouse']; } } ?>
this perhaps issue of variable scope. can't see variable saveinvoice()
being fed main script, if passing variable called $data
contains keys code
, data
, , warehouse
same array should used on getinvoices()
in mainscript instead of $invoices = getinvoices($warehouse);
again variable scope inside of functions...
inside of function saveinvoice( array $data){
... getinvoices($warehouse);
valid use because $warehouse
declared variable value inside function.
outside of saveinvoice()
variable $warehouse
not exist (unless have declared global scope in code not posted question).
for reason, think solution (in second posted block of code) call getinvoices()
this: $invoices = getinvoices($data['warehouse']);
additionally, recommend not mysqli escape values return. advice use mysqli prepared statements in saveinvoicedetail()
, other database querying functions.
lastly, take close @ array containing success
. return success=>true
if there no success. part requires more development.
Comments
Post a Comment