0
    foreach ($result as $user) {
  $replacements[$user['Email']] = array(
    '{FirstName}'=>$user['FirstName'],
    '{LastName}'=>$user['LastName'],
    '{Code}'=>$user['RandomCode']
  );
}

That is what i want to achieve , however, since i need variable instead of fixed input , i need to modify it to be a string, here is the code:

try{
$sql =
    'SELECT  *
    FROM     require_attributes
    where ListID=? 
    ';
$stmt = $conn->prepare($sql);
$stmt->execute(array($list));
$tagSet= $stmt->fetchAll();

}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="mail/campaign_view.php"> Back</a>'); 
    }




try{
$sql =
    'SELECT  s.*
    FROM     subscriber s ,list_sub ls
    where ls.ListID=? 
    AND ls.SubID=s.SubID
    ';
$stmt = $conn->prepare($sql);
$stmt->execute(array($list));
$resultSub= $stmt->fetchAll();
}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="mail/campaign_view.php"> Back</a>'); 
    }


//Put the result in array
foreach ($resultSub as $user) {
  $replacements[$user['Email']] = array(
  foreach ($tagSet as $tags)
  {$string=$string."'".$tags['Attribute']."'"."=>".$user[$tag['Attribute']].",";}
   $string = substr($string, 0, -1);
  echo $string;
  );

The first part is to collect the tags which means {FirstName}, {LastName} and so on , The tags are flexible so that means there are not fixed number of tag, the $user[$tag['Attribute']] is the value i need to collect. How can i retrieve the value and put it into the array just like what i want to achieve?

Currently it has an error of

Parse error: syntax error, unexpected T_ECHO, expecting ')' in C:\xampp\htdocs\fyp\mail\sendPersonal.php on line 186

Thank you.

1
  • 1
    I'm not sure what you're trying to do (can you maybe post an example of input/output?). But the way you do it is very wrong. You cannot do a foreach loop inside the declaration of an array. It's a syntax error as said in your error message. Commented Mar 25, 2012 at 21:18

1 Answer 1

2

Your problem is that you can't use a foreach (nor any thing like that) in a array() declaration.

Process this way instead:

//Put the result in array

foreach ($resultSub as $user) {
  $replacements[$user['Email']] = array();
  foreach ($tagSet as $tags)
  {
       $replacements[$user['Email']]['{'.$tags['Attribute'].'}']=$user[$tags['Attribute']];
  }
  echo print_r($replacements[$user['Email']], true);
);
Sign up to request clarification or add additional context in comments.

3 Comments

sorry but i find some difference in it
for the new version , it will print out Array ( [[email protected]] => Array ( ) [[email protected]] => Array ( ) ) , which the fixed version is ok but the new is not
can you define "new version" and "fixed version" please? Sorry :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.