how can i append a $name variable inside a $lang variable? - php

i have a lang file with lots of pre-made statements
the one i'm working one is used as a template to email users
$lang['ADD_STORE_REQUEST_EMAIL_TITLE'] = " New store has been added";
as you guys can probably guessed, the code above is what would be the email title
my question is, how can i put variable in there? say i wanna have the email title as "hello john, new store has been added"
mail(ADMINISTRATOR_EMAIL,$lang['ADD_STORE_REQUEST_EMAIL_TITLE']"From: no-reply#gmail.com");
i tried adding the 'name' variable before and after the $lang, but it wont work. i tried appending it by name+$lang.... but still wont show up

You can do it like this:
$lang['ADD_STORE_REQUEST_EMAIL_TITLE'] = " New store".$yourVar." has been added";
or
$lang['ADD_STORE_REQUEST_EMAIL_TITLE'] = " New store has been added".$yourVar;

You can use the function sprintf to accomplish this.
Also. To concatenate strings in PHP, you need to use . instead of +
Ex:
mail('email#example.com', sprintf('This is a %s day', 'fine'), sprintf('Hello mr. %s', $name);
This will result in an email being sent to email#example.com with the subject "This is a fine day" and body "Hello mr. {some name from the variable"}

Related

PHP variable Syntax using _GET

I have a redirectURL that currently redirects to a default page and works great
$WA_redirectURL = "mypage.php";
I want to insert a _GET value that is available but am getting a syntax error. Here is what I am trying. I thought the periods would allow me to get the value into the URL?
$WA_redirectURL = "mypage.php?EmpNumber=". echo $_GET['EmpNumber'] .";
You don't use echo when concatenating strings:
$WA_redirectURL = "mypage.php?EmpNumber=". echo $_GET['EmpNumber'] .";
should be:
$WA_redirectURL = "mypage.php?EmpNumber=". $_GET['EmpNumber'];
(The last quote is also an error).
You use echo to send text to the user.
If you want to join strings together use the period to join things.
For example:
echo "hello"; // this will print hello to the screen
$testing = "hello "."world!";
echo $testing; // this will print hello world! to the screen.

Append Var to URL in PHP

I can't seem to get this to work...what I have is a link being auto-generated to send out in an email, and I need to be able to append the user's email address to the URL, but I can't seem to get it formatted correctly. Here's what I have:
href=\"http://example.com/unsubscribe/\" . $email . \"\"
Where the resulting link in the user's email should be
http://example.com/unsubscribe/myemail#myemail.com
The way the unsubscribe is structured, it wants it the above format instead of with ?email=. Can I do this?
EDIT 12/3/13:
Thanks for all the help, guys! Part of my problem is that I wasn't passing the email var through my generate email function. :/
Here's how I ended up setting it up:
$unsubscribeurl = 'http://example.com/unsubscribe/';
$unsubplus = $unsubscribeurl . $email;
$unsubscribelink = 'UNSUBSCRIBE';
Then to set the link up, I just used $unsubscribelink.
Creating the url:
$url = 'http://example.com/unsubscribe/' . urlencode($email);
Creating the link:
$link = 'link';
Using the link in html context:
<?php echo $link ?>
Or:
link
urlencode()
htmlspecialchars()

Smarty Template Variable inside Variable

I have text this stored in my database:
Your email is: {$email} and your name is {$name}.
$text = get from db this field;
I read this field from the database
I assign with smarty this:
$smarty->assign('text', $text);
$smarty->assign($name, 'maria');
$smarty->assign($email, 'maria#email.it');
and visualize in my html page with this:
{$text}
Result is:
Your email is: {$email} and your name is {$name}.
My problem is Smarty not renders a variable inside a variable.
can someone help me?
You could do this
$smarty->assign('name', 'maria');
$smarty->assign('email', 'maria#email.it');
$smarty->assign('text',$smarty->fetch('string:'.$text));
check out http://www.smarty.net/docs/en/resources.string.tpl for more details
EDIT
If you store your templates in a database, I would recommend you to read about custom template resources http://www.smarty.net/docs/en/resources.custom.tpl
Or if you use Smarty 2
$template = "Your email is: {$email} and your name is {$name}."
require_once( $smarty->_get_plugin_filepath( 'function', 'eval' ));
$smarty->assign('name', 'maria');
$smarty->assign('email', 'maria#email.it');
$compiled = smarty_function_eval(array('var'=>$template), $smarty);

Cant get field values with space from mysql using php

I am trying to pass variable that containing the values with space through href but I fail to get the expected output with space.
The code I used is:
print " <a href=update.php?id='$id'&name=$name&dob='$dob'&email='$email'>Update Details</a> <br>
Student ID: $id<br> Student Name: $name<br> Date Of Birth: $dob<br> Email ID: $email<br>";
In update.php I could see the link as
localhost/student_portal/update.php?id='abc'&name=Giridharan
and I didn't get the full name and dob and email
My variables with values are as follows:
$id=abc
$name=Giridharan Rengarajan
$dob=1993-07-22
$email=rgiridharan.93#gmail.com
What should I do to get all the four values in update.php?
Since spaces are not legal parts of the query string you have to encode them.
Eg:
Use rawurlencode / rawurldecode
<a href=update.php?id='$id'&name=rawurlencode($name)&dob='$dob'&email='rawurlencode($email)'>Update Details</a>
You can get the variables in update.php by:
<?php
$id = $_GET['id'];
$name = $_GET['name'];
$dob = $_GET['dob'];
$email = $_GET['email'];
For proper creation of url you can use http_build_query.
See examples here http://www.php.net/manual/en/function.http-build-query.php
Create array of your params, put it into this function and to your update script like a string.

PHP : mailto function gets breaking when populating text having lines and url

Hi I am creating a task assignment page, in which after creating the task admin can send a mail to assignee to know the task. I cant use PHP mail function since I cant configure office SMTP mail address. So I am using mailto function.
Issue happens when my Commenst got symbols like " &, ", ' . It will stop executing the remaining part of code
In my mail it just populates the first line. So my comments is getting broken.
Here is my code to mailto function :
';
You put everything into one line which brings you into devils kitchen. Instead put the functionality apart (e.g. first load the data from the database), then process the data (e.g. build the mailbox name, the subject and the body) and then finally create the mail URL.
<?php
# load data from database
$assignee = mysql_result($result, $i, 'assignee');
$comments = mysql_result($result, $i, 'comments');
# process data
$mailbox = sprintf('%s#symantec.com', rawurlencode($assignee));
$headers = array(
'subject' => 'Task Assignment',
'body' => "Hello $assignee,\n\nDescription : $comments ...\n\nFor more details log in to ...",
);
# generate the mailto URL
$url = sprintf('mailto:%s?%s', $mailbox, http_build_query($headers));
You can then just output the $url:
printf('<img src="images/mail.png" width="32" height="32" p="center">',
$url);
You need to explicity urlencode everything that is non alphanumeric.
Note, urlencode (the php command) doesn't urlencode everything such as underscore and letters and perhaps other things.
Example:
$test="this: is it's dog/+_";
echo "<br>";
echo urlencode($test);
echo "<br>";
echo $test;
produces:
this%3A+is+it%27s+dog%2F%2B_
this: is it's dog/+_

Categories