Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/239921-dynamic-array-unexpected-t_double_arrow/
Share on other sites

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]);

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.

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);

 

 

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';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.