Jump to content

VARIABLE TO STRING ISSUE


jasonabuck

Recommended Posts

$updatetext generates the stuff commented out in the ARRAY below.

 

If I generate the text and cut and paste it in the ARRAY below, it works fine, but when I try use the Variable, is doesn't work inside the Array.

 

Please advise on a workable solution.

 

// POPULATE THIS ARRAY WITH FIELDNAME => VALUE TO UPDATE

$updatetext = (string)$message;

$fieldsToUpdate = array (

/* 'Website_Username__c' => 'xxjasonxx',

'Website_Password__c' => 'PoWvHTQWfZl9U',

'Salutation' => 'Mr.',

'FirstName' => 'Jason A',

'LastName' => 'Buckxxx',

'Title' => 'Application Developer',

'AccountId' => '0015000000RP0XAAA1',

'Level__c' => 'Director',

'Primary_Job_Function__c' => 'Operations',

'Email' => '[email protected]',

'Phone' => '8582227088',

'MailingStreet' => '17914 St. Andrews',

'MailingCity' => 'Poway',

'MailingState' => 'CA',

'MailingPostalCode' => '92064',

'MailingCountry' => 'United States' */

 

(string)$updatetext

 

);

  $sObject1 = new SObject();

  //$sObject1->fields = $fieldsToUpdate;

  $sObject1->fields = $fieldsToUpdate;

  $sObject1->type = $_REQUEST['SFTable'];

  $sObject1->Id = $_REQUEST['SFID'];

 

Thanks in advance,

 

Jason Buck

Link to comment
https://forums.phpfreaks.com/topic/189049-variable-to-string-issue/
Share on other sites

You can't just inject a string into an array and expect the array to parse the string.

 

I assume you are incrementally building $message or no? If yes, instead of building $message, add elements to the $fieldsToUpdate array:

// Instead of $message .= 'field => bla bla'
$fieldsToUpdate['field'] = 'bla bla';

 

http://php.net/manual/en/language.types.array.php

Okay, so below is my code for the entire page.

 

The page that POSTS to this page is a form, where the INPUT FIELD NAMES are the corresponding database field names.

 

When I do the post, I need to get the FIELD NAME and its VALUE in this FORMAT

$fieldsToUpdate = array ( 'FIELDNAME' => 'VALUE', 'FIELDNAME2' => 'VALUE2');

 

Generated Dynamically by the FOR EACH LOOP for the $_POST.

 

 

<?php
session_start();


ini_set('display_errors', 1); 
ini_set('log_errors', 1); 
ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); 
error_reporting(E_ALL);


// define('DRIVE_VAR', 'd:\\'); // DEV SERVER IS D... PRODUCTION IS E
define('DRIVE_VAR', 'e:\\'); // PRODUCTION SERVER IS E

define('SERVER_FILES', DRIVE_VAR .'modx\\'); 
define('SALESFORCE_USER', 'xxxxxxx.com');
define('SALESFORCE_PASS', 'xxxxxxx6');
define('SALESFORCE_TOKEN', 'xxxxxxxxxxxx');
define('SALESFORCE_WSDL',  SERVER_FILES . 'lib\\crm\salesforce\\partner.wsdl.xml');
define("SOAP_CLIENT_BASEDIR", SERVER_FILES . 'lib\\crm\\salesforce\\soapclient\\');

require_once (SOAP_CLIENT_BASEDIR.'SforcePartnerClient.php');
require_once (SOAP_CLIENT_BASEDIR.'SforceHeaderOptions.php');


/*  UPDATE RECORDS */
if(isset($_REQUEST['SFUpdate'])){

// LOOP THROUGH ALL POSTED VALUES
$message = "";

foreach($_POST as $key => $data) {
  if($key != 'required') {
  if($key != 'submit') { // Each one of these IFs eliminates non database fields from the $message string being generated
	  if($key != 'SFUpdate') { 
	   if($key != 'RETURNURL') {
		   if($key != 'SFID') {
			   if($key != 'SFTable'){
      if($data !='') {
	  if($message != '') {
		$message .= ", ";
	  } 
         $message .= "'".$key. "'"." => "."'".$data."'";
   
		  }
	  }
		   } // != SFTable
	   } // != SFID
	  } // != RETURNURL
  } // != SFUpdate
  		} // != submit
	}


	echo $message;

	// END LOOP THROUGH POSTED VALUES

try {
$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection(SALESFORCE_WSDL);
$mylogin = $mySforceConnection->login(SALESFORCE_USER, SALESFORCE_PASS . SALESFORCE_TOKEN);


// REQUEST THE FIELDNAMES AND VALUES
//	echo $prof_Id. "<br>";

// POPULATE THIS ARRAY WITH FIELDNAME => VALUE TO UPDATE
$updatetext = (string)$message;
$fieldsToUpdate = array ( 
		/* 'Website_Username__c' => 'xxjasonxx', 
		'Website_Password__c' => 'PoWvHTQWfZl9U', 
		'Salutation' => 'Mr.', 
		'FirstName' => 'Jason A', 
		'LastName' => 'Buckxxx', 
		'Title' => 'Application Developer', 
		'AccountId' => '0015000000RP0XAAA1', 
		'Level__c' => 'Director', 
		'Primary_Job_Function__c' => 'Operations', 
		'Email' => '[email protected]', 
		'Phone' => '8582227088', 
		'MailingStreet' => '17914 St. Andrews', 
		'MailingCity' => 'Poway', 
		'MailingState' => 'CA', 
		'MailingPostalCode' => '92064', 
		'MailingCountry' => 'United States' */
					  
				(string)$updatetext
					  
	);


  $sObject1 = new SObject();
  //$sObject1->fields = $fieldsToUpdate;
  $sObject1->fields = $fieldsToUpdate;
  $sObject1->type = $_REQUEST['SFTable'];
  $sObject1->Id = $_REQUEST['SFID'];

  $response = $mySforceConnection->update(array ($sObject1));
  if(isset($_REQUEST['RETURNURL'])){
  $returnURL = $_REQUEST['RETURNURL'];
  }

// print_r($response);
// END HOW TO UPDATE RECORD
} catch (Exception $ex) {
  print_r($ex);
  //echo $mySforceConnection->getLastRequest();

}
} // IF UPDATE

if(isset($_REQUEST['SFInsert'])){

// REQUEST THE FIELDNAMES AND VALUES


}

?>


You're making it waaaay more complex than it really is.

<?php
$fieldsToUpdate = array();

foreach($_POST as $key => $data) {
if ($key != 'required'
	&& $key != 'submit'
	&& $key != 'SFUpdate'
	&& $key != 'RETURNURL'
	&& $key != 'SFID'
	&& $key != 'SFTable'
	&& $data != ''
) {
	$fieldsToUpdate[$key] = $data;
}
}

Archived

This topic is now archived and is closed to further replies.

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