0

I am trying to compose an email content with heredoc. I have no idea how to access variables stored from some previous sql queries and insert them into the text.

Here's the sql queries:

$week=mysql_query('SELECT `Name`, `fname`, `Marca` FROM `personal` WHERE (`Responsabil`='.$id.') AND (`Protectie`>="'.$CurrentDate.'") AND (`Protectie`<"'.$WeekDate.'") AND (`Notificat`!=1)');
$exp=mysql_query('SELECT `Name`, `fname`, `Marca` FROM `personal` WHERE (`Responsabil`='.$id.') AND (`Protectie`<"'.$CurrentDate.'") AND (`Notificat`!=1)');
$week=mysql_fetch_assoc($week);
$exp=mysql_fetch_assoc($exp);

and the problem:

$content=<<<EMAIL
We inform you that the following people:
$week['Name'] $week['fname']
$exp['Name'] $exp['fname']
are due for inspection.
EMAIL;

I need to insert here all the names resulted form the queries. The queries are tested and working.

John Robertson
  • 1,486
  • 13
  • 16
user3864308
  • 25
  • 1
  • 9

2 Answers2

0

You need to wrap your results variables in curly brackets to make them display properly in the heredoc string:

$week['Name'] = "Bloggs";
$week['fname'] = "Joe";

$exp['Name'] = "Doe";
$exp['fname'] = "Jane";

$content = <<<EMAIL
We inform you that the following people:
{$week['Name']} {$week['fname']}
{$exp['Name']} {$exp['fname']}
are due for inspection.
EMAIL;

var_dump($content);

Returns:

string(87) "We inform you that the following people: Bloggs Joe Doe Jane are due for inspection."
iswinky
  • 1,951
  • 3
  • 16
  • 47
  • this works, but how do I post the hole content from $week lets say. If $week is an array of names. – user3864308 Aug 11 '14 at 11:52
  • You could build a string from the array results: `$str = ""; foreach ($week['Name'] as $name) {$str .= $name;}` then after it's looped, place `$str` in the Heredoc – iswinky Aug 11 '14 at 11:55
  • @iswinky you can also use `$week[fname]` quotes inside array are redundant – insanebits Aug 11 '14 at 11:56
  • @insanebits Didn't know that! Is that from 5.4? – iswinky Aug 11 '14 at 11:58
  • This will work. The only problem I have now are with linefeeds (\n) not show in the text – user3864308 Aug 11 '14 at 12:04
  • @iswinky I heard about it several days ago, this works from PHP 4+ however I did some testing, and it will produce warning `Use of undefined constant` which is not a viable option to use in a real code :) – insanebits Aug 11 '14 at 12:24
0

Try this,

$content=<<<EMAIL
We inform you that the following people:
{$week['name']} {$week['fname']}
{$exp['name']} {$exp['fname']}
are due for inspection.
EMAIL;

Please Note this will print only one record, if you want to display all the records from result set then you will need to loop on resultset.

Dharmesh Patel
  • 1,881
  • 1
  • 11
  • 12