jra3 Posted June 20, 2011 Share Posted June 20, 2011 Hello, I'm trying to hack out a project at work... not a programmer, don't even pretend to be one. I'm trying to take form data to send to Google Docs spreadsheet. I can get the code to work if everything is hard coded. I'm trying to make the code more dynamic for reuse. So, I'm trying to do this through a dynamic array. I'm getting an unexpected T_DOUBLE_ARROW. I suspect it is happening because there is not a comma in the array separating the value from the index, but I don't know how to do it (or if I even phrased that correctly). Here is the array loop: $form_fields = array('$questions', '$suggestions', '$name'); $fields = array(); while($field_counter < sizeof($form_fields) ) { $fields[] = 'entry.'.$field_counter.'.single'=>urlencode($form_fields[$field_counter]); ++$field_counter; } Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/239921-dynamic-array-unexpected-t_double_arrow/ Share on other sites More sharing options...
AbraCadaver Posted June 20, 2011 Share Posted June 20, 2011 It depends. Do you want a multi-dimensional array (numerical containing an associative) or do you want the $fields array to be a single associative array indexed with your key that you are building? // multi-dimensional $fields[] = array('entry.'.$field_counter.'.single'=>urlencode($form_fields[$field_counter])); // associative $fields['entry.'.$field_counter.'.single'] = urlencode($form_fields[$field_counter]); Quote Link to comment https://forums.phpfreaks.com/topic/239921-dynamic-array-unexpected-t_double_arrow/#findComment-1232420 Share on other sites More sharing options...
AbraCadaver Posted June 20, 2011 Share Posted June 20, 2011 You also haven't defined $field_counter before the loop and your array values are probably not what you think they are because they are in single quotes. Whatever you are doing, it seems overly complex, but given your code I would simplify it: $form_fields = array($questions, $suggestions, $name); $fields = array(); foreach($form_fields as $key => $value) { $fields["entry.{$key}.single"] = urlencode($value); } If you give more detail there is probably a better way to skin your cat. Quote Link to comment https://forums.phpfreaks.com/topic/239921-dynamic-array-unexpected-t_double_arrow/#findComment-1232429 Share on other sites More sharing options...
jra3 Posted June 20, 2011 Author Share Posted June 20, 2011 AbraCadaver, Thanks for the help. Your two suggestions have eliminated errors and the script runs, but inserts null data. Here is what this is... this is a script that will eventually take a form in Flash, and will parse the POST data and then send to a Google Spreadsheet. This example is just using sample data. So immediately below is what I have in full. Maybe what I am trying to do with variable names in an Array is not allowed; I guess you learn by trying. Below is my code that I built using an example which follows at the end of this reply. So again the current state... the script runs with no errors. It does post to my Google Spreadsheet, but it does not post the hard-coded sample data. My current Code: <body> <?php //Import Data from Snap!, this is just putting some plug data into the variables for now. Will eventually be a script to grab post data from a Flash survey. $questions = "Sample question question data."; $suggestions = "Sample suggestion data"; $name = "A Name"; //Set array values from Google Form, example has 3 inputs $form_fields = array($questions, $suggestions, $name); //In order to post to Google Spreadsheet Form //http://spreadsheets.google.com/formResponse?formkey=dFdYSTlzUVJsSomeReallyLongKeyGoesHereqemU2YUE6MA.. //Questions "entry.0.single" //Suggestions is "entry.1.single" //Name is "entry.2.single" $url = 'https://spreadsheets.google.com/spreadsheet/formResponse?formkey=dGN2aWV3NTZHbnhBeWJGcElianBqeFE6MQ&ifq'; // Create post array to send results to Google Spreadsheets $field_counter = 0; $fields = array(); while($field_counter < sizeof($form_fields) ) { $fields[] = array('entry.'.$field_counter.'.single'=>urlencode($form_fields[$field_counter])); ++$field_counter; } $fields[] = array('submit'=>'submit'); // Begining of code for posting to Google Spreadsheet $fields_string = ''; //url-ify the data for the POST foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } //rtrim($fields_string,"& "); $fields_string = substr($fields_string, 0, strlen($fields_string)-1); $result = "Fields_String: [" . $fields_string . "]<br />"; //set POST variables for Google Spreadsheets //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); //execute post $result .= "Curl Results: [" . curl_exec($ch) . "]<br />"; //close connection curl_close($ch); ?> </body> The original example code that I used to do this: 1: //In order to post to Google Spreadsheet Form 2: //http://spreadsheets.google.com/formResponse?formkey=dFdYSTlzUVJsSomeReallyLongKeyGoesHereqemU2YUE6MA.. 3: //Name "entry.0.single" 4: //Email is "entry.1.single" 5: //Phone is "entry.2.single" 6: //Comment is "entry.3.single" 7: //IP address is "entry.4.single" 8: $url = 'http://spreadsheets.google.com/formResponse?formkey=dFdYSTlzUVJsSomeReallyLongKeyGoesHereqemU2YUE6MA..'; 9: // Create post array to send results to Google Spreadsheets 10: $fields = array( 11: 'entry.0.single'=>urlencode($name), 12: 'entry.1.single'=>urlencode($email), 13: 'entry.2.single'=>urlencode($phone), 14: 'entry.3.single'=>urlencode($comments), 15: 'entry.4.single'=>getRealIpAddr(), 16: 'submit'=>'submit' 17: ); 18: 19: // Begining of code for posting to Google Spreadsheet 20: $fields_string = ''; 21: //url-ify the data for the POST 22: foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 23: //rtrim($fields_string,"& "); 24: $fields_string = substr($fields_string, 0, strlen($fields_string)-1); 25: $result = "Fields_String: [" . $fields_string . "]<br />"; 26: 27: //set POST variables for Google Spreadsheets 28: //open connection 29: $ch = curl_init(); 30: 31: //set the url, number of POST vars, POST data 32: curl_setopt($ch,CURLOPT_URL,$url); 33: curl_setopt($ch,CURLOPT_POST,count($fields)); 34: curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 35: curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 36: curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 37: 38: //execute post 39: $result .= "Curl Results: [" . curl_exec($ch) . "]<br />"; 40: 41: //close connection 42: curl_close($ch); Quote Link to comment https://forums.phpfreaks.com/topic/239921-dynamic-array-unexpected-t_double_arrow/#findComment-1232451 Share on other sites More sharing options...
AbraCadaver Posted June 20, 2011 Share Posted June 20, 2011 This should do it for that part: $questions = "Sample question question data."; $suggestions = "Sample suggestion data"; $name = "A Name"; $form_fields = array($questions, $suggestions, $name); $fields = array(); foreach($form_fields as $key => $value) { $fields["entry.{$key}.single"] = urlencode($value); } $fields['submit'] = 'submit'; Quote Link to comment https://forums.phpfreaks.com/topic/239921-dynamic-array-unexpected-t_double_arrow/#findComment-1232472 Share on other sites More sharing options...
jra3 Posted June 20, 2011 Author Share Posted June 20, 2011 That did the trick. Again, thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/239921-dynamic-array-unexpected-t_double_arrow/#findComment-1232527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.