View Single Post
  #10  
Old 09-07-2007, 08:02 AM
slugmandrew slugmandrew is offline
Junior Member
 
Join Date: Aug 2007
Posts: 10
Send a message via MSN to slugmandrew
Question Using array_map to add htmlspecialchars function

Mike,

Thanks for your help yet again. This looks like a useful function. At first I couldn't really work out how to use it but I think I have it now.

So in your previous example, you are taking each row as an array, $row, using array_map to stripslashes and add htmlspecialchars, and then passing the 'cleaned' array back into $row itself?

It took me a while to work out how to change this code:

PHP Code:
if (isset($_POST['doUpdate'])) 
  {
    
    
$sfid $_POST['sfid'];
    
$ct htmlspecialchars($_POST['City']);
    
$st htmlspecialchars($_POST['State']);
    
$phone htmlspecialchars($_POST['Phone']);
    
$fax htmlspecialchars($_POST['Fax']);
    
$name htmlspecialchars($_POST['AccountName']);  // added 'specialchars' to remove &
    
    
$fieldsToUpdate = array('Id' => $sfid,
                            
'Name'=>$name,
                            
'BillingCity'=>$ct,
                            
'BillingState'=>$st,
                            
'Phone'=>$phone,
                            
'Fax'=>$fax);  // create array from posted variables
    
    
$sObject = new SObject();  // declare new sObject
    
$sObject->fields $fieldsToUpdate;  // set fields of new sObject
    
$sObject->type 'Account';  // set type to account
    
$acct $mySforceConnection->update(array ($sObject));  // update sObject

    
header('Location: welcome.php');   // redirect to welcome.php
  

into something that used array_map on the last 5 values but not on the id.
I saw some examples that declared an array and then used array_map to pass the values into another array, so I came up with this:

PHP Code:

  
if (isset($_POST['doUpdate'])) 
  {
    
$sfid $_POST['sfid'];
    
$ct $_POST['City'];
    
$st $_POST['State'];
    
$phone $_POST['Phone'];
    
$fax $_POST['Fax'];
    
$name $_POST['AccountName'];  
    
    
$fieldsToClean = array('Id'=>'',
                            
'Name'=>$name,
                            
'BillingCity'=>$ct,
                            
'BillingState'=>$st,
                            
'Phone'=>$phone,
                            
'Fax'=>$fax);  // create array to clean from posted variables
                            
    
$fieldsToUpdate array_map('htmlspecialchars'$fieldsToClean);   // map the first array using htmlspecialchars, with empty id                    
    
$fieldsToUpdate['Id'] = $sfid;  // add the id field
        
    
$sObject = new SObject();  // declare new sObject
    
$sObject->fields $fieldsToUpdate;  // set fields of new sObject
    
$sObject->type 'Account';  // set type to account
    
$acct $mySforceConnection->update(array ($sObject));  // update sObject

    
header('Location: welcome.php');   // redirect to welcome.php
  

Declaring the id field before the array_map function was the only way I could get this to work. I tried using array_map on the main 5 first and then adding the id field to the array afterwards, but it didn't update the records correctly. Is this due to the order of items in the array? I thought being an associative array it wouldn't matter that the Id was at the end.

Any feedback is appreciated.
Reply With Quote