1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

HELP! - Storing php concatenated strings into database

Discussion in 'PHP' started by Fahey, Aug 5, 2012.

  1. #1
    Hey guys,
    I am working on making an admin panel for a script I created a few months back. One of the things I want to do in the admin panel is allow the users to customize the many different email messages the script sends out. An example of an email message is the following:

    $customrequest = "Hello ".$username.",<br /> Your custom request for a ".$req." has been approved and ".$cost." ".$currency." have been removed from your account."
    $customrequestsubject = $title." - Custom Request Approved!"

    MailUpdateRequest($customrequest, $username, $customrequestsubject);

    the mail function looks like this:

    function MailUpdateRequest($message, $username, $sub){
    global $requestemail, $contactemail, $title, $currency;
    $request = mysql_query("SELECT * FROM members WHERE username ='".$username."'");
    $email = mysql_result($request,0,"email");
    // multiple recipients
    $to = ''.$email.'';

    // subject
    $subject = $sub;

    // headers
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'To: '.$username.' <'.$email.'> '. "\r\n";
    $headers .= 'From: '.$title.' Request Department <'.$requestemail.'>' . "\r\n";

    // Mail it
    mail($email, $subject, $message, $headers);
    }


    Essentially, I want to put a text field in the admin panel so any non-programmer could customize this message. Below the text field I will explain the different php variables they can use and how to use them... only issue is I can't just directly read the mysql fields into the variables or else the php variables get read in as text.

    Help is very very much appreciated, financial compensation available if necessary.
     
    Fahey, Aug 5, 2012 IP
  2. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    then break the variables down more ..and don't use them as only your sentence structure $cost etc.

    // vars
    $customrequest = "Hello ".$username.",<br /> Your custom request for a ".$req." has been approved and ".$cost." ".$currency." have been removed from your account."
    $req = (your vars whatever they are) ;
    $cost = (your vars) ;
    $currency = (your vars);
     
    ezprint2008, Aug 5, 2012 IP
  3. Fahey

    Fahey Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Erm do you even know what your talking about?

    I have come up with a temporary solution for now, I feel this could be much simpler but here's what I have:

    In admin panel - inform the admin he can use ~username~, ~request~, etc. to load dynamic variables into his emails.

    In Code - grab the tilda-rich text from mysql, then send it along with any non-global variables into a big preg_replace like so:

    function populate(&$string, $username = NULL, $request = NULL, $current_status = NULL, $cost = NULL, $status_value = NULL){
    global $cost, $currency, $title;
    $patterns = array();
    $patterns[0] = '/\~username\~/i';
    $patterns[1] = '/\~request\~/i';
    $patterns[2] = '/\~cost\~/i';
    $patterns[3] = '/\~currency\~/i';
    $patterns[4] = '/\~current_status\~/i';
    $patterns[5] = '/\~status_value\~/i';
    $patterns[6] = '/\~title\~/i';
    $patterns[7] = '/\~cost\~/i';
    $replacements = array();
    $replacements[0] = $username;
    $replacements[1] = $request;
    $replacements[2] = $cost;
    $replacements[3] = $currency;
    $replacements[4] = $current_status;
    $replacements[5] = $status_value;
    $replacements[6] = $title;
    $replacements[7] = $cost;
    $string = preg_replace($patterns, $replacements, $string);
    }
     
    Fahey, Aug 6, 2012 IP
  4. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    yes I know, I meant that the way you asked the question wasn't relevant to what you were asking since you made no reference to how you were sending the vars to database (assuming its SQL)

    You are asking how to store the concatenated string of the user for a request to use a file in the database. The question I'm wondering is why the logic of this script is even in that direction?
    Why not a drop-down menu to let them choose with an AJAX <div> table next to it that displays the letters selected. Then they make the submission of form to send the letter they want to use and its cost already worked into the script handler. Then store the letter that was selected/purchased and the date to the WHERE username in SQL database.

    Why not that work flow logic? I don't understand your approach.


    UNLESS what you're trying to accomplish is : you want to provide a service to websites that can pay you to auto-respond their email/letters for them. Are you building something such as that?
    But if that were the case then the $vars would be lost from their site to yours. I don't understand your overall goal.
     
    ezprint2008, Aug 16, 2012 IP
  5. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #5
    Having PHP and HTML in the same file is a very poor design. You should use str_replace() to replace tokens within the HTML document with the dynamic data. This is essentially a basic template system.

    For example:

    
    <?php
    $template = file_get_contents('example.html');
    $tokens = array('{%CUSTOMER_NAME%}', '{%CUSTOMER_AGE%}');
    $replace = array('john','30');
    print str_replace($tokens, $replace, $template);
    ?>
    
    PHP:
    Also, there's no need to encapsulate variables with dots within quotes.

    
    <?php
    $a = 1; $b = 10; $c = $a * $b;
    print "$a x $b equals $c";
    ?>
    
    PHP:
    It's neater too.
     
    Last edited: Aug 18, 2012
    BRUm, Aug 18, 2012 IP