Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nested foreach() with multiple POST to URL with file_get_contents

    I'm having an issue figuring out why my code won't work and I could use a little help.

    My script loops through an array of numbers, which are then sent via POST to a url which returns a JSON array for each of the posted numbers.

    I need to use the values in each of the arrays separately so I'm trying to loop through the array of returned data.

    Here is my script....

    PHP Code:
    //Processes the trace numbers

    $tracenumbers $_POST['tracenumbers'];
    $tracenumbers trim($tracenumbers);
    $tracenumbers explode("\n",$tracenumbers);
    $n 0;

    echo 
    '<nav>';

    foreach (
    $tracenumbers as $value) {
        
    $n++;
        echo 
    '<a href="#tab-'.$n.'" class="tabber-handle">'.$value.'</a>';
    }
    echo 
    '</nav>';

    $c 0;
    foreach (
    $tracenumbers as $tracevalue) {
        
        
    //Process the trace numbers
        
    $traceurl 'https://jet-airways-stl.com/gt5ws.nsf/ws_trace?openagent&s=single&a='.$tracevalue.'&p=B&output=JSON';
        
    $tracejson file_get_contents($traceurl);
        
    $trace[] = json_decode($tracejsonTRUE);
        
        foreach (
    $trace as $tracedata) {
            
    $c++;
            
    //Check that the array is not empty
            
    if (count($trace) > 0) {
            
    // Set the variables
            
    $awbnumber $tracedata[awbs][0][awbnumber];
            if (
    $tracedata[awbs][0][part]) {$part $tracedata[awbs][0][part];} else {$part '-';}
            if (
    $tracedata[awbs][0][awbOrigin]) {$awbOrigin $tracedata[awbs][0][awbOrigin];} else {$awbOrigin '-';}
            if (
    $tracedata[awbs][0][awbDestination]) {$awbDestination $tracedata[awbs][0][awbDestination];} else {$awbDestination '-';}
            if (
    $tracedata[awbs][0][totalPieces]) {$totalPieces $tracedata[awbs][0][totalPieces];} else {$totalPieces '-';}
            if (
    $tracedata[awbs][0][totalWeight]) {$totalWeight $tracedata[awbs][0][totalWeight];} else {$totalWeight '-';}
            if (
    $tracedata[awbs][0][partOrigin]) {$partOrigin $tracedata[awbs][0][partOrigin];} else {$partOrigin '-';}
            if (
    $tracedata[awbs][0][partDestination]) {$partDestination $tracedata[awbs][0][partDestination];} else {$partDestination '-';}
            if (
    $tracedata[awbs][0][partPieces]) {$partPieces $tracedata[awbs][0][partPieces];} else {$partPieces '-';}
            if (
    $tracedata[awbs][0][partWeightKilos]) {$partWeightKilos $tracedata[awbs][0][partWeightKilos];} else {$partWeightKilos '-';}
            if (
    $tracedata[awbs][0][region]) {$region $tracedata[awbs][0][region];} else {$region '-';}
            if (
    $tracedata[awbs][0][agentName]) {$agentName $tracedata[awbs][0][agentName];} else {$agentName '-';}
            if (
    $tracedata[awbs][0][agentAddress1]) {$agentAddress1 $tracedata[awbs][0][agentAddress1];} else {$agentAddress1 '-';}
            if (
    $tracedata[awbs][0][agentCity]) {$agentCity $tracedata[awbs][0][agentCity];} else {$agentCity '-';}
            if (
    $tracedata[awbs][0][agentState]) {$agentState $tracedata[awbs][0][agentState];} else {$agentState '-';}
            if (
    $tracedata[awbs][0][agentZip]) {$agentZip $tracedata[awbs][0][agentZip];} else {$agentZip '-';}
            if (
    $tracedata[awbs][0][agentPhone]) {$agentPhone $tracedata[awbs][0][agentPhone];} else {$agentPhone '-';}
            if (
    $tracedata[awbs][0][ConsolDateTime]) {$ConsolDateTime $tracedata[awbs][0][ConsolDateTime];} else {$ConsolDateTime '-';}
            if (
    $tracedata[awbs][0][ArrivedDateTime]) {$ArrivedDateTime $tracedata[awbs][0][ArrivedDateTime];} else {$ArrivedDateTime '-';}
            
    $ArrivedDateTime $tracedata[awbs][0][ArrivedDateTime];
            if (
    $tracedata[awbs][0][DelveredDateTime]) {$DelveredDateTime $tracedata[awbs][0][DelveredDateTime];} else {$DelveredDateTime '-';}
            if (
    $tracedata[awbs][0][signee]) {$signee $tracedata[awbs][0][signee];} else {$signee '-';}
            
    $events $tracedata[awbs][0][events];
            if (
    $tracedata[awbs][0][ppcc]) {$ppcc $tracedata[awbs][0][ppcc];} else {$ppcc '-';}
            
    $otherParts $tracedata[awbs][0][otherParts];
            if (
    $tracedata[hazmat]) {$hazmat $tracedata[hazmat];} else {$hazmat 'NO';}
            
                echo 
    '<div class="tabber-tab" id="tab-'.$c.'">
                        <div id="traceheader">
                            <div class="traceheadersection">AWB Origin</div>
                            <div class="traceheadersection">AWB Destination</div>
                            <div class="traceheadersection">Total Pieces</div>
                            <div class="traceheadersection">Total Weight (Kilos)</div>
                            <div class="traceheadersection">PP/CC</div>
                            <div class="traceheadersection">Hazmat</div><br />
                            <div class="traceheadersectionbottom">'
    .$awbOrigin.'</div>
                            <div class="traceheadersectionbottom">'
    .$awbDestination.'</div>
                            <div class="traceheadersectionbottom">'
    .$totalPieces.'</div>
                            <div class="traceheadersectionbottom">'
    .$partWeightKilos.'</div>
                            <div class="traceheadersectionbottom">'
    .$ppcc.'</div>
                            <div class="traceheadersectionbottom">'
    .$hazmat.'</div>
                        </div>
                        
                        <div id="tracebody">'
    ;
                        
                        if (
    $tracedata[DelveredDateTime]) {
                            
    $arrived "Arrived At Destination";
                            echo 
    '<div id="traceleft">Signed:<br /><span class="tracebold">'.$signee.'</span></div>
                            <div id="traceright">Delivered<br /><span class="tracebold">'
    .$ArrivedDateTime.'</span></div>
                            <div id="tracecenter"><span class="tracebold">'
    .$arrived.'</span></div>
                            <div id="clearall"></div><br /><br />'
    ;
                        } else if (
    time() > strtotime($ConsolDateTime)) {
                            
    $arrived "In Route";
                            echo 
    '<div id="traceleft">Shipped:<br /><span class="tracebold">'.$ConsolDateTime.'</span></div>
                            <div id="traceright">Estimated Arrival:<br /><span class="tracebold">'
    .$ArrivedDateTime.'</span></div>
                            <div id="tracecenter"><span class="tracebold">'
    .$arrived.'</span></div>
                            <div id="clearall"></div><br /><br />'
    ;
                        } else if (
    time() < strtotime($ConsolDateTime)) {
                            
    $arrived "Awaiting Shipment";
                        }
                        
                    echo 
    '</div>';
                             
                    echo 
    '    <div id="traceheader">
                            <div class="traceheadersection">Part</div>
                            <div class="traceheadersection">Origin</div>
                            <div class="traceheadersection">Destination</div>
                            <div class="traceheadersection">Pieces</div>
                            <div class="traceheadersection">Weight (Kilos)</div>
                            <div class="traceheadersection">DIM Weight (Kilos)</div><br />
                            <div class="traceheadersectionbottom"><span class="tracebold">'
    .$part.'</span></div>
                            <div class="traceheadersectionbottom">'
    .$partOrigin.'</div>
                            <div class="traceheadersectionbottom">'
    .$partDestination.'</div>
                            <div class="traceheadersectionbottom">'
    .$partPieces.'</div>
                            <div class="traceheadersectionbottom">'
    .$partWeightKilos.'</div>
                            <div class="traceheadersectionbottom">DIM Weight (Kilos)</div>
                            </div>
                            
                            <div id="clearall"></div><br />
                            
                            <div id="traceheaderclear">
                                <div id="traceleft">'
    .$agentAddress1.'<br />'.$agentCity.', '.$agentState.' '.$agentZip.'</div>
                                <div id="traceright"><b>Phone Number</b><em>'
    .$agentPhone.'</em><br /><b>Fax Number</b><em>Fax</em></div>
                                <div id="tracecenter"><span class="tracebold">Destination Company<br />'
    .$agentName.'</span></div>
                            </div>
                            
                            <div id="clearall"></div><br />
                            
                            <div id="traceheaderclear"><b>External Comments:</b> </div>
                            
                            <div id="traceheaderclear"><b>Shipment Exception:</b> </div><br />
                            
                            <div><span class="tracebold">Movement History</span></div>
                            <div id="traceheaderclear">
                                <div id="traceleft" style="text-align:center;">Date/Time</div>
                                <div id="traceright" style="text-align:center;">Action</div>
                                <div id="tracecenter" style="text-align:center;">Location</div>
                                <div id="traceleft"><b>Date/Time</b></div>
                                <div id="traceright"><b>Action</b></div>
                                <div id="tracecenter"><b>Location</b></div>
                            </div>
                        </div>'
    ;
                
            } else {
                echo 
    '<div class="tabber-tab" id="tab-'.$c.'">
                    <div id="tracebody"><div id="tracecenter"><span class="tracebold">We can\'t find a record of that shipment.</span></div></div>
                </div>'
    ;
            }
        }

    Here are two sample numbers to submit to the URL 20543865592 205-61748676

  • #2
    New to the CF scene
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone?

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    419
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Try starting by debugging your code. After the post is received, print out the contents and exit, then see if the results are as expected. If it is, then continue on looking elsewhere, if it's not, then something is messed up before that and should be debugged before it reaches that point.

  • #4
    New to the CF scene
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dubz View Post
    Try starting by debugging your code. After the post is received, print out the contents and exit, then see if the results are as expected. If it is, then continue on looking elsewhere, if it's not, then something is messed up before that and should be debugged before it reaches that point.
    I've done that. In the OP I described the issue being with the foreach() loop... specifically the nested one. If I enter four numbers, I get back four loops. The problem is, the only variables I'm getting are the last set (or from the last loop). I'm assuming that there's something wrong with that nested foreach loop and my previously set variables are being reset with new ones, and finally the last loop sticks.

    I'm sure it's something with the nested foreach arguments, or where I have the loop positioned. I just need some help figuring it out.

  • #5
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi

    Notice the quotes

    Change all missing quotes throughout and things should start to work.

    PHP Code:
     $awbnumber $tracedata['awbs'][0]['awbnumber']; 

    Though my results varied depending on which test number I used (note: tested single set of numbers from provided numbers)

    for example with this number 20543865592 I got

    Notice: Undefined index:

    DelveredDateTime
    signee
    ppcc
    hazmat

    Those indexes are not present in the returned data array for that number.

    LT
    Last edited by low tech; 05-28-2014 at 04:11 AM. Reason: update
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •