Jump to content

PHP variable in a database


Go to solution Solved by .josh,

Recommended Posts

Hello. 

 

I am building a website where I need to send welcome emails.

 

Welcome email text is stored in the database, so i first have to retrieve it, then send.

 

Now I came across this issue - how do I make my code treat the text from the database AS a variable(s).

 
    public function retrieveConfirmationEmail() {
 
     $this->email_tag = 'Room Confirmation';
 
     $query = $this->db->prepare('SELECT * FROM emails WHERE email_tag = ?');
     $query->bindValue(1, $this->email_tag);
     $query->execute();
 
     $data = $query->fetch();
 
     $message = $data['message'];
     $subject = $data['subject'];
 
     $this->message  = $message;
     $this->subject  = $subject;
 
    }
 
 
 
public function sendConfirmationEmail() {
 
$headers  = "From: admin@homelocator.com" . "\r\n";
   $headers .= "Reply-To: admin@homelocator.com". "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 
        mail($this->email, $this->subject, $this->message, $headers);
 
}

The first method retrieves 'Message'. That message has to contain PHP variables such as $this->name, $this->title etc.

 

 

 

Any advice? :)

 

thx

Edited by Pain
Link to comment
https://forums.phpfreaks.com/topic/286069-php-variable-in-a-database/
Share on other sites

IMHO - I would design my db differently.  You're not going to put "variable" data into your "static" message, are you?  You s/b creating a table of users with their info separate from the messages that you wish to send.  Then, you pick a message and the users and do your mail generation. 

 

Unless you are sending totally unique messages to each person every time you send out mail, which I don't see working in a db-driven environment.

I'm sorry I should've been more clear on this. Each email has to be different. Please take a look at the full version of my class.

<?php
 
 
class Advert {
  
private $db;
 
    public $title;
    public $description;
    public $postcode;
    public $picture;
    public $email;
    public $date_posted;
    public $cost;
 
    public $message;
    public $subject;
    public $email_tag;
 
    public $link;
    public $id;
 
 
 
public function __construct($database) {
   $this->db = $database;
} 
 
 
 
    public function setDetails($title, $description, $postcode, $picture, $email, $date_posted, $cost) {
 
     $this->title       = $title;
$this->description = $description; 
$this->postcode    = $postcode;
$this->picture     = $picture;
$this->email       = $email;
$this->date_posted = $date_posted;
$this->cost        = $cost;
 
    }
 
 
 
public function insertAdvert() {
 
$query = $this->db->prepare("INSERT INTO rooms (title, description, postcode, picture, email, date_posted, cost) VALUES (?, ?, ?, ?, ?, ?, ?)");
 
$query->bindValue(1, $this->title);
$query->bindValue(2, $this->description);
$query->bindValue(3, $this->postcode);
$query->bindValue(4, $this->picture);
$query->bindValue(5, $this->email);
$query->bindValue(6, $this->date_posted);
$query->bindValue(7, $this->cost);
 
$query->execute();
 
}
 
 
 
public function setAdvertLink() {
 
$query = $this->db->prepare('SELECT id FROM rooms WHERE title = ? AND description = ? AND postcode = ? AND date_posted = ? AND email = ?');
 
$query->bindValue(1, $this->title);
$query->bindValue(2, $this->description);
$query->bindValue(3, $this->postcode);
$query->bindValue(4, $this->date_posted);
$query->bindValue(5, $this->email);
 
$query->execute();
 
$data = $query->fetch();
$id   = $data['id'];
        
        $this->link = 'http://www.home-decorators.co.uk/rooms/uk/london/'.$id;
        $_SESSION['last_id'] = $id;
 
}
 
 
 
    public function retrieveConfirmationEmail() {
 
     $this->email_tag = 'Room Confirmation';
 
     $query = $this->db->prepare('SELECT * FROM emails WHERE email_tag = ?');
     $query->bindValue(1, $this->email_tag);
     $query->execute();
 
     $data = $query->fetch();
 
     $message = $data['message'];
     $subject = $data['subject'];
 
     $this->message  = $message;
     $this->subject  = $subject;
 
    }
 
 
 
public function sendConfirmationEmail() {
 
$headers  = "From: admin@homelocator.com" . "\r\n";
   $headers .= "Reply-To: admin@homelocator.com". "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 
        mail($this->email, $this->subject, $this->message, $headers);
 
}
 
 
 
public function redirect() {
 
header('location: success.php');
 
}
 
 
 
}
 
 
 
?>

As you can see I'm getting those variables from $_POST. 

 

 

Obviously I could just write a welcome email like this:

$this->message = 'Hello ' . $this->name . ' your email is ' .$this->email;

But i want to hold this in the db. Hope that's a bit more clear. Sorry I'm really bad at explaining :)

Edited by Pain
  • Solution

put placeholders in your email message and then use str_replace to replace the placeholders with the values from the variables.

 

example:

$content = "Hello {{NAME}}, your email address is: {{EMAIL}}";
$name = 'your name';
$email = 'yname@gmail.com';

$replace = array(
  '{{NAME}}' => $name,
  '{{EMAIL}}' => $email
  // etc..
);
  
$content = str_replace(array_keys($replace),$replace,$content);

put placeholders in your email message and then use str_replace to replace the placeholders with the values from the variables.

 

example:

$content = "Hello {{NAME}}, your email address is: {{EMAIL}}";
$name = 'your name';
$email = 'yname@gmail.com';

$replace = array(
  '{{NAME}}' => $name,
  '{{EMAIL}}' => $email
  // etc..
);
  
$content = str_replace(array_keys($replace),$replace,$content);

genius-meme.jpg

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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