0

i am trying to make a php tracking page for the shipment i send , i made a html page that sends ajax query to my php file and then shows all of the details i have successfully grapped the info but the issue is when i made the loop for the waybill numbers it shows them on by one, meaning if i have 3 waybills lets say [ 50003 , 3902 , 39499] the results will be id : 50003 , status : picked , id : 3902 , status : picked , id : 39499 , status : picked , then it will be id : 50003 , status : left city , id : 3902 , status : left city , id : 39499 , status : left city , and so on .. which is very bad

i need it to be like : id : 50003 , status1 : picked , status 2 : left city : status 3 : delivered and like this

this is my php :

<?php
 ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


  $waybill2 = explode(PHP_EOL, $_POST['waybill']) ;

  $value = "<int>".implode('</int><int>', $waybill2)."</int>";

 $curl = curl_init();

  curl_setopt_array($curl,array(
    CURLOPT_URL => 'https://infotrack.naqelexpress.com/NaqelAPIServices/NaqelAPI/9.0/XMLShippingService.asmx?wsdl',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>"<?xml version='1.0' encoding='utf-8'?>
  <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
    <soap:Body>
      <TraceByMultiWaybillNo xmlns='http://tempuri.org/'>
         <ClientInfo>
          <ClientAddress>
            <PhoneNumber>******</PhoneNumber>
            <FirstAddress>******</FirstAddress>
            <Location>******</Location>
            <CountryCode>******</CountryCode>
            <CityCode>******</CityCode>
          </ClientAddress>
          <ClientContact>
            <Name>zeyad</Name>
            <Email>******</Email>
            <PhoneNumber>******</PhoneNumber>
            <MobileNo>******</MobileNo>
          </ClientContact>
          <ClientID>******</ClientID>
          <Password>*****</Password>
          <Version>9.0</Version>
        </ClientInfo>
        <WaybillNo>
$value
        </WaybillNo>
      </TraceByMultiWaybillNo>
    </soap:Body>
  </soap:Envelope>
  ",
  
    CURLOPT_HTTPHEADER => array(
      'Content-Type: text/xml'
    ),
  ));
  
  $response = curl_exec($curl);

  curl_close($curl);
 $response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
$xml = new SimpleXMLElement($response);
$body = $xml->xpath('//soapBody')[0];
$array = json_decode(json_encode((array)$body), true); 

$data = $array['TraceByMultiWaybillNoResponse']['TraceByMultiWaybillNoResult']['Tracking'];



$waybils = array();
foreach ($data as $index => $val1) {
    echo "<pre>";
    $waybils[] = $val1['WaybillNo'];
    print_r($waybils);
    foreach( $waybils as $e ){
       echo $e . $val1['ArabicActivity']; 
    
    }
// echo $val1['WaybillNo']."  && ".$val1['ArabicActivity'] ;
echo "</pre>";
}

and here is html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="scri.js" ></script>
</head>
<body>
    <div class="container">
        <div class="row">
            <h1>
                Type Here
            </h1>
           <form class="md-6" id="sds_contact_form">
            <div class="form-group">
                <label for="exampleFormControlTextarea1">Example textarea</label>
                <textarea class="form-control way" name="waybill" id="exampleFormControlTextarea1" rows="3"></textarea>
              </div>
              <div class="form-group mt-6">
                <input type="button" class="send" onClick="onClick()" value="submit" name="send"/>
            </div>
           </form> 
        </div>
        <div class="succ"></div>
    </div>
  <script>

    
    function onClick()
{
      var $waybill = $('.way').val();
      var myLineBreak = $waybill.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '<br />');
      var succ = $('.succ');
    $.ajax({
        url:'g.php',
        type:'post',
        method:'post',
         data: {'waybill': $waybill},
       success: function(data){
           succ.html(data)
       },
         dataType: "json",
        error:function queryError(request, textStatus, errorThrown) {
    $('.succ').html("error " + request.responseText, textStatus + " " + errorThrown);
}
    });


}





     
  </script>
</body>
</html>

This is the output of $data

array (
  0 => 
  array (
    'StationCode' => 'Buraydah',
    'Date' => '2021-05-11T22:48:00',
    'ActivityCode' => '1',
    'Activity' => 'Picked Up at : Buraydah',
    'ArabicActivity' => 'تم إستلام الشحنة من الراسل : بريدة',
    'WaybillNo' => '52636025',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 
    array (
    ),
    'RefNo' => '41337_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '1',
  ),
  1 => 
  array (
    'StationCode' => 'Buraydah',
    'Date' => '2021-05-11T22:48:00',
    'ActivityCode' => '1',
    'Activity' => 'Picked Up at : Buraydah',
    'ArabicActivity' => 'تم إستلام الشحنة من الراسل : بريدة',
    'WaybillNo' => '52636027',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 
    array (
    ),
    'RefNo' => '41356_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '1',
  ),
  2 => 
  array (
    'StationCode' => 'Buraydah',
    'Date' => '2021-05-12T02:13:04.833',
    'ActivityCode' => '47',
    'Activity' => 'Miscode: Correct city',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636026',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'JUBAIL(KSA)',
    'RefNo' => '41369_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  3 => 
  array (
    'StationCode' => 'Buraydah',
    'Date' => '2021-05-12T02:15:39.37',
    'ActivityCode' => '47',
    'Activity' => 'Miscode : Correct city : JEDDAH(KSA)',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636027',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'JEDDAH(KSA)',
    'RefNo' => '41356_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  4 => 
  array (
    'StationCode' => 'Buraydah',
    'Date' => '2021-05-12T02:19:38.67',
    'ActivityCode' => '47',
    'Activity' => 'Miscode : Correct city : RIYADH(KSA)',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636025',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'RIYADH(KSA)',
    'RefNo' => '41337_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  5 => 
  array (
    'StationCode' => 'Buraydah',
    'Date' => '2021-05-12T07:40:17.567',
    'ActivityCode' => '47',
    'Activity' => 'Terminal Handling',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636026',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'ELQRUHHCBU27 - tripID - 31036',
    'RefNo' => '41369_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  6 => 
  array (
    'StationCode' => 'Buraydah',
    'Date' => '2021-05-12T07:40:18.22',
    'ActivityCode' => '47',
    'Activity' => 'Load to Trip : ELQRUHHCBU27 - tripID - 31036',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636025',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'ELQRUHHCBU27 - tripID - 31036',
    'RefNo' => '41337_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  7 => 
  array (
    'StationCode' => 'Buraydah',
    'Date' => '2021-05-12T07:40:18.22',
    'ActivityCode' => '47',
    'Activity' => 'Load to Trip : ELQRUHHCBU27 - tripID - 31036',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636027',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'ELQRUHHCBU27 - tripID - 31036',
    'RefNo' => '41356_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  8 => 
  array (
    'StationCode' => 'RIYADH',
    'Date' => '2021-05-12T14:43:33.587',
    'ActivityCode' => '47',
    'Activity' => 'Cons.Arrived at : Sort Facility - RIYADH - ELQRUHHCBU27 - tripID - 31036',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636025',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'Sort Facility - RIYADH - ELQRUHHCBU27 - tripID - 31036',
    'RefNo' => '41337_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  9 => 
  array (
    'StationCode' => 'RIYADH',
    'Date' => '2021-05-12T14:43:34.067',
    'ActivityCode' => '47',
    'Activity' => 'Terminal Handling',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636026',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'Sort Facility - RIYADH - ELQRUHHCBU27 - tripID - 31036',
    'RefNo' => '41369_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  10 => 
  array (
    'StationCode' => 'RIYADH',
    'Date' => '2021-05-12T14:43:34.133',
    'ActivityCode' => '47',
    'Activity' => 'Cons.Arrived at : Sort Facility - RIYADH - ELQRUHHCBU27 - tripID - 31036',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636027',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'Sort Facility - RIYADH - ELQRUHHCBU27 - tripID - 31036',
    'RefNo' => '41356_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  11 => 
  array (
    'StationCode' => 'RIYADH',
    'Date' => '2021-05-12T14:45:55.877',
    'ActivityCode' => '47',
    'Activity' => 'Transit : Transit Facility',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636025',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 
    array (
    ),
    'RefNo' => '41337_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  12 => 
  array (
    'StationCode' => 'RIYADH',
    'Date' => '2021-05-12T14:45:55.877',
    'ActivityCode' => '47',
    'Activity' => 'Transit: Transit Facility',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636026',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 
    array (
    ),
    'RefNo' => '41369_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  13 => 
  array (
    'StationCode' => 'RIYADH',
    'Date' => '2021-05-12T14:45:55.877',
    'ActivityCode' => '47',
    'Activity' => 'Transit : Transit Facility',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636027',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 
    array (
    ),
    'RefNo' => '41356_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  14 => 
  array (
    'StationCode' => 'RIYADH',
    'Date' => '2021-05-13T00:08:10.397',
    'ActivityCode' => '47',
    'Activity' => 'Load to Trip : RUHTMM2 - tripID - 31076',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636025',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'RUHTMM2 - tripID - 31076',
    'RefNo' => '41337_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  15 => 
  array (
    'StationCode' => 'RIYADH',
    'Date' => '2021-05-14T15:13:24.773',
    'ActivityCode' => '47',
    'Activity' => 'Terminal Handling',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636026',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'RUHQJBFRI01 - tripID - 31082',
    'RefNo' => '41369_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
  16 => 
  array (
    'StationCode' => 'RIYADH',
    'Date' => '2021-05-14T16:44:00.457',
    'ActivityCode' => '47',
    'Activity' => 'Load to Trip : RUHJEDFRI - tripID - 31085',
    'ArabicActivity' => 'Terminal Handling',
    'WaybillNo' => '52636027',
    'ClientID' => '9020654',
    'HasError' => 'false',
    'ErrorMessage' => 
    array (
    ),
    'Comments' => 'RUHJEDFRI - tripID - 31085',
    'RefNo' => '41356_O_110521',
    'DeliveryStatusID' => '0',
    'EventCode' => '47',
  ),
)
Zeyad
  • 1
  • 2
  • 1
    Most of this code is not relevant to your issue. I would suggest reading the help center article about creating a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). The core of your issue is `foreach ($data as $index => $val1)`, so you should focus only on that. And you should provide us with a sample of `$data`, because we need to know the structure in order to find out where it goes wrong (and to be able to run the code for testing purposes). – El_Vanja May 14 '21 at 22:52
  • Thank you very much for kind reply ,, I really tried every thing before sending this post but none worked .. i have edited the post and added the outpot or $data array – Zeyad May 15 '21 at 06:09
  • A small tip: it's better to use `var_export` for this. `print_r` is easier to read, but the output of `var_export` can be directly copied into an editor to recreate the variable. Can you replace it? – El_Vanja May 15 '21 at 08:25
  • sure , i replaced it now – Zeyad May 15 '21 at 09:07
  • as you can see , it created new array for each waybill i was thinking if it is possible to merge all of the waybill numbers and ArabicActivity to make one array showing all data foreach request – Zeyad May 15 '21 at 09:08
  • Side note: you specify `dataType: "json"`, but you're sending HTML, not JSON. – El_Vanja May 15 '21 at 09:55
  • Grouping the data seems logical. Have a look at [this question](https://stackoverflow.com/questions/14113256/group-array-values-based-on-key-in-php). Once you have all the data for the same waybill, output should be straightforward. – El_Vanja May 16 '21 at 15:40

0 Answers0