Jump to content

Getting values into variables in MySQL result field


SoapLady

Recommended Posts

I have a MySQL table with various email messages I want to send out.  (Want to do it that way so they can be edited in our Admin area).

Example: 
Table: Email 
FieldName:  Email_content

[color=red]<p>Dear $firstname,</p>
<p>We received a request to send you your login information.  It is as follows:</p>
<p>username: <b>$username</b></p>
<p>password: <b>$password</b></p>[/color]

Once I get the data (via SELECT query), I can access it fine, but can't get the variables replaced by the values (which come from $_SESSION array).

This DOES NOT work; the text with  $username and $password unreplaced shows up in the email.
[code]<snipped from inside class>

[SELECT QUERY]
$row = mysql_fetch_array($result)
$this->mailText = $row[email_content];

function __construct($firstname, $username, $password)
{
$this->htmlContent = "$this->mailText";
}

// $this->htmlContent is then passed to the email sending function
<end snippet>[/code]

However, if I do the exact same thing but put the TEXT in the function, the variables are replaced and the email is correct:

[code]function __construct($firstname, $username, $password)
{
$this->htmlContent = "<p>Dear $firstname,</p>
<p>We received a request to send you your login information.  It is as follows:</p>
<p>username: <b>$username</b></p>
<p>password: <b>$password</b></p>";
}
[/code]

I'm sure there is something fairly simple I need to do, I just can't figure out what it is!

Help would be appreciated.

SoapLady
Hmmm.  It seems variable interpolation isn't being carried out on your returned database text.  The following should work as a workaround, though it's untested.  Obviously you can extend the arrays to include other variables you need.

[code]
function __construct($firstname, $username, $password)
{
$find = array('$firstname', '$username', '$password' );
$replace = array( $firstname, $username, $password );

$this->htmlContent = str_replace( $find, $replace, $this->mailText );
}
[/code]

Hope that helps! :)
Maybe try something using eval?  I'm not sure how this will work in your situation, but something like...

[code]
function __construct($firstname, $username, $password)
{
$php_code = '$this->htmlContent = "' . $this->mailText . '";';
eval( $php_code );
}
[/code]

$this->htmlContent should now be correct?  Well, it MIGHT be.  ;)
or
[code]
<?php
session_start();

$msg = '<p>Dear $firstname,</p>
<p>We received a request to send you your login information.  It is as follows:</p>
<p>username: $username</p>
<p>password: $password</p>';

$s = array('$firstname', '$username', '$password');
$r = array($_SESSION['firstname'], $_SESSION['username'], $_SESSION['password']);
$msg = str_replace ($s, $r, $msg);

echo $msg;
?>
[/code]

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.