Jump to content

dmsinc

New Members
  • Posts

    7
  • Joined

  • Last visited

dmsinc's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Requinix: Thank you, Although some of your answer lost me it was enough for me to try a alternate approach which eventually led to a working answer. Barand: Thank you, I have not implimented your suggestion yet, but I will as it is straight forward on sorting how i need. To both of you: Thnak you for the answers and help, below is the end code (sorting excluded for the moment) $to = "4813 River Basin Dr S, Jacksonville FL 32207"; $records = array( array("DV1012","30453423","9890 Hutchinson Park Dr Jacksonville, FL 32225"), array("DB3434","30404041","821 Orange Ave Crescent City, FL 32112"), array("DB3434","30605060","1972 Wells Road, Orange Park FL 32073"), array("DB4578","30605060","2 Independent Drive, Jacksonville FL 32202"), array("DB7841","30605060","5000 Norwood Avenue, Jacksonville FL 32208"), array("DB3235","30605060","9501 Arlington Expressway, Jacksonville FL 32225"), array("DB7894","30605060","Massey Avenue, Jacksonville, FL 32227"), array("DB2020","30121212","11200 Central Pkwy Jacksonville, FL 32224") ); foreach ($records as $key => $value) {$from = $from.urlencode($value[2])."|";} $result = array(); $to = urlencode($to); $data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&units=imperial&mode=driving&sensor=false"); $res=json_decode($data) or die("Error: Cannot read object"); $time = 0; $distance = 0; $limit = count ($res->rows); for ($i = 0; $i < $limit; $i++) { foreach($res->rows[$i]->elements as $road) { $time += $road->duration->value; $distance += $road->distance->value; $distance = round(($distance * 0.00062),2); $time = ceil($time / 60); $ticket = $records[$i][0]; $from = $res->origin_addresses[$i]; $to = $res->destination_addresses[0]; $result[$ticket] = "$time"; //The end result array is a ticket# with a matching travel "time" between common point A and the other address point B...C...D...etc.etc. echo "Ticket: ".$ticket."<br>"; echo "From: ".$from; echo "<br/>"; echo "To: ".$to; echo "<br/>"; echo "Time: ".$time." minutes"; echo "<br/>"; echo "Distance: ".$distance." miles"; echo "<hr>"; } }
  2. Evening everyone and Merry X-Mas (Happy Holidays) or whatever fits you best.... I have been trying over and over for about 12 hours to figure out how to get data from a specific JSON response and assign the specific values to a new array key/value pair. The various ways I have tried to figure this out are numerous so i'm going to avoid the runnig list of "I trieid this...and this...and this... etc etc." just understand I have reached a dead end point where I need help badly. Here is what our end goal is: "The company" is a "service industry" provider (plumbing, electrical etc. etc.) who wants to dispatch its technicians to new jobs based on which technician has the shortest travel time from any existing address where they already have a scheduled appointment that day and has available time in there schedule. Thus the dispatching system when a new service call is entered is going to give "recommended" technicians (up to 4) to be assigned the new service call based on the above mentioned criteria. (Efficient routing to save company gas cost) 1.) We have a "New service" street address assigned to the $to variable: $to = "4813 River Basin Dr S, Jacksonville FL 32207"; 2.) We will have "records" array which containes a series of records, each record consists of a ticket# a technician id# and a street address: $records = array( array("DV1012","30453423","9890 Hutchinson Park Dr Jacksonville, FL 32225"), array("DB3434","30404041","821 Orange Ave Crescent City, FL 32112"), array("DB3434","30605060","1972 Wells Road, Orange Park FL 32073"), array("DB4578","30605060","2 Independent Drive, Jacksonville FL 32202"), array("DB7841","30605060","5000 Norwood Avenue, Jacksonville FL 32208"), array("DB3235","30605060","9501 Arlington Expressway, Jacksonville FL 32225"), array("DB7894","30605060","Massey Avenue, Jacksonville, FL 32227"), array("DB2020","30121212","11200 Central Pkwy Jacksonville, FL 32224") ); 3.) We are going to prepare the records array for submission to Google's Distance Matrix API by URL encoding each array value into a single variable and then submit it: foreach ($records as $key => $value) {$from = $from.urlencode($value[2])."|";} $to = urlencode($to); $data = file_get_contents("//maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&units=imperial&mode=driving&sensor=false"); $res=json_decode($data) or die("Error: Cannot read object"); You may look at the ACTUAL live Google response from this here: http://tiny.cc/i6eerx (We have applied <pre> and var_dump($res) to the output) We are looking to get the following information back: The distance and travel time between the "New Service" address ($to) and each of the address' in the $records array ($from), now once that information is returned from Google (JSON response) we need to parse the response and narrow it down to 4 or less (Based on the 4 lowest drive times) to form a new array which ends like this: $results = array( array("DV1012","30453423","2.3 Miles","8 mins"), array("DB3434","30404041","2.8 Miles","9 mins"), array("DB3434","30605060","4.6 Miles","13.8 mins"), array("DB4578","30605060","5.7 Miles","15.2 min") ); OUR PROBLEM: Everything up until the the Google API response is fine, but iterating over the multidimensional arrays within multidimensional arrays of the JSON response is just not coming together in my head (or 2 dozen code attempts). I am able to ECHO an individual value like ( echo $res->rows[0]->elements[0]->distance->text; ) but this has no value for the end result i need to get to. My last thoughts and efforts was that that I was going to have to embed foreach statements within foreach statements to drill down through the various arrays but that hasn't worked very well and it seems like it shouldn't be how it has to be done. I would appreciate any help in showing me how the iteration code should be written and any explanation about the code so i can wrap my head around it.
  3. Josh - They are identical, letter for letter. I posted the PHP variable (copy/paste) in the first part of my question - 1'st line As a matter of fact - This AJAX call is accessed/called both in the automated way (like above) as well as a manual method (sumbitting the values through $_POST from a form) - and it works without a hitch. It's only when adding the PHP echo that it fails.
  4. I commented the IF check out and ran it again, the AJAX fails - I checked it in Firebug console and got: POST http://www.somewebsite.com/includes/ffl2.php 200 OK 840ms Also in Firebug, Console: The POST data sent shows as: Parametersapplication/x-www-form-urlencoded fflck chk var1 5 var2 76 var3 02993 I just cannot understand why the PHP echoed variable is breaking the AJAX, it shouldn't -- but it is. The variables and data is correct so i just dont get it..
  5. When I hard code the URL variable of this AJAX call, all is well, it executes and returns expected values. But when I replace part of the URL variable with a PHP variable, the AJAX fails to execute. There are no errors or flags being thrown by javaconsole or PHP. When i look at source view of page in either circumstance, they are identical - the PHP variable has been replaced properly. Why might the use of the PHP echo break the AJAX execution even though when viewing the source all appears to be normal? Hard coded version: <?php $domain="http://www.somewebsite.com/"; ?> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { if( $("#dig6").val() ) { $.ajax({ type : 'POST', url : 'http://www.somewebsite.com/includes/ffl2.php', data: { var1 : $('#dig1').val(), var2 : $('#dig2').val(), var3 : $('#dig6').val(), fflck : "chk" }, success:function (data) { $("#datatarget").append(data); } }); } }); </script> Version using PHP variable: <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { if( $("#dig6").val() ) { $.ajax({ type : 'POST', url : '<?php echo $domain; ?>includes/ffl2.php', data: { var1 : $('#dig1').val(), var2 : $('#dig2').val(), var3 : $('#dig6').val(), fflck : "chk" }, success:function (data) { $("#datatarget").append(data); } }); } }); </script> Snippet of 'view source' after page ready with PHP variable in AJAX call: <-- MORE CODE HERE --> $.ajax({ type : 'POST', url : 'http://www.somewebsite.com/includes/ffl2.php', data: { var1 : $('#dig1').val(), var2 : $('#dig2').val(), var3 : $('#dig6').val(), fflck : "chk" <-- MORE CODE HERE -->
  6. @Mace - Thank you! It still gave me some trouble as i was trying to figure out what types of elements/attributes could be designated in the 'FIND', but your example was the key and now its working great. - Great find and thanks for sharing!
  7. I am only a few months into PHP and don't grasp all the things that i guess the "expert" programmers do, so for that i'm sorry... i'm working on it. I need help with what code would accomplish what i need below, I have had only partial success but am now stuck. I have used CURL to post to a remote site form and return the results, the returned results is the full raw HTML of that forms results page. There is only 1 table of data i need from the entire page and the only unique identifier of the table is a unique CLASS attribute. I have been able, through trial and error to get the initial stage of assigning the HTML response to a PHP DOM object and identify the needed table by its class assignment using an example found on the net. But: 1.) I really am having a hard time understand the whole DOM object models and manipulation. 2.) The code below, returns the TABLE i need, but all HTML is stripped form it, thus i just get one long running line of text. I need that table and its content, html tags and all assigned to a variable. Please help. Example: ^^^^^^ Lots of misc HTML code above ^^^^^^ <table class="magicname" height=100 width=100> <tr> <td> <p>whatever content is in here </p> </td> </tr> <tr> <td> <p>whatever content is in here </p> </td> </tr> </table> vvvvv Lots of misc HTML code below vvvv My Current code: $classname = 'maintbl'; $dom = new DOMDocument; $dom->loadHTML($server_output); $xpath = new DOMXPath($dom); $results = $xpath->query("//*[@class=" . $classname . "]"); foreach($results as $node) { echo "{$node->nodeName} - {$node->nodeValue}<br>"; // or you can just print out the the XML: // $dom->saveXML($node);
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.