Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


chevrolux

4962 posts

Uber Geek
+1 received by user: 2638
Inactive user


#243543 17-Dec-2018 16:57
Send private message

So just mucking around with a SMS sending page.

 

I do a SQL query to get the "message body"...

 

$smsSql = mysqli_query($conn, "SELECT * FROM `sms_msg` WHERE id='1'");
$smsData = mysqli_fetch_array($smsSql);

 

And then load the message itself in to a variable just to make it tidy for me going forward:

 

[code]$message = $smsData['msg'];[/code]

 

So if you were to echo $message it would print:

 

Hi $name, This is a test message. I have also sent an email to $email

 

So further up in the script I have already defined the $name, and $email variables. And when i send the message, I would like the values of $name & $email to be printed in that $message string.

 

To send the message I do this...

 

$result = $api->sendSms($message,$mobile);

 

Do I need to do a find/replace sort of thing? Or is there some kind of cool easy way with some special brackets/quotes/characters that will just magically re-write that message string for me.

 

I have no idea what to google, so am failing miserably.

 

 


Create new topic
TheoM
229 posts

Master Geek
+1 received by user: 55

ID Verified
Trusted

  #2146628 17-Dec-2018 17:27
Send private message

tl;dr: variable inside string literal

 

from https://stackoverflow.com/a/5368897/3831621

 

Use:

 

 

 

$message = "Hi {$name}, This is a test message. I have also sent an email to {$email}"

 

 

 

-e- string must be "double quoted" not 'single quoted'





Hi! I'm TheoM, but you know that already. I run Linux mirrors in NZ together with 2degrees. Like a mirror added? PM me!

 


 

https://theom.co.nz | https://theom.nz | https://mirrorlist.mirrors.theom.nz | Providing Free Mirrors Since Ages Ago™




chevrolux

4962 posts

Uber Geek
+1 received by user: 2638
Inactive user


  #2146645 17-Dec-2018 18:45
Send private message

Ahhhhhh very good.

So my only problem.. the value of $message is set from a sql query. So I have no way to use double quotes.

Could I just go "$message" in that api call?

Behodar
11102 posts

Uber Geek
+1 received by user: 6095

Trusted
Lifetime subscriber

  #2146648 17-Dec-2018 19:09
Send private message

So are $name and $email referenced in the SQL table rather than your PHP code?




chevrolux

4962 posts

Uber Geek
+1 received by user: 2638
Inactive user


  #2146652 17-Dec-2018 19:29
Send private message

Behodar:

 

So are $name and $email referenced in the SQL table rather than your PHP code?

 

 

Yea the SQL table is just two columns, an index and a message.

 

So the message is just in the SQL table as exactly "Hi $name, This is a test message. I have also sent an email to $email" (excluding the quotes of course)

 

It's like that so that a user can easily edit the messages from a form.

 

 

 

That's why I wonder if I need just do a find/replace type function on the string. I can just change the variables inside the string to have different delimiters.


Behodar
11102 posts

Uber Geek
+1 received by user: 6095

Trusted
Lifetime subscriber

  #2146654 17-Dec-2018 19:35
Send private message

chevrolux: That's why I wonder if I need just do a find/replace type function on the string. I can just change the variables inside the string to have different delimiters.

 

 

That's what I was going to suggest in a pinch.


chevrolux

4962 posts

Uber Geek
+1 received by user: 2638
Inactive user


  #2146712 17-Dec-2018 21:19
Send private message

Yea cool.

I only need 3 or 4 different variables so will just do it with str_replace

Thanks team!

 
 
 

Want to support Geekzone and browse the site without the ads? Subscribe to Geekzone now (monthly, annual and lifetime options).
itxtme
2102 posts

Uber Geek
+1 received by user: 557


  #2146724 17-Dec-2018 21:41
Send private message

I would use Heredoc myself, keeps your code tidy

 

 

$sql = <<<SQL
SELECT * FROM `sms_msg` WHERE id='1'
SQL;

$smsSql = mysqli_query($conn, $sql);
$a = mysqli_fetch_array($smsSql);


$message = <<<EOF
Hi {$name}, This is a test message. I have also sent an email to {$email}. I can use " marks, or ' marks without issue.
I can access my old query using the same method {$a['title']}
EOF;

 

$result = $api->sendSms($message,$mobile);

 

 

 


jarledb
Webhead
3319 posts

Uber Geek
+1 received by user: 1983

Moderator
ID Verified
Trusted
Lifetime subscriber

  #2146787 18-Dec-2018 00:19
Send private message

itxtme:

 

I would use Heredoc myself, keeps your code tidy

 

 

$sql = <<<SQL
SELECT * FROM `sms_msg` WHERE id='1'
SQL;

$smsSql = mysqli_query($conn, $sql);
$a = mysqli_fetch_array($smsSql);


$message = <<<EOF
Hi {$name}, This is a test message. I have also sent an email to {$email}. I can use " marks, or ' marks without issue.
I can access my old query using the same method {$a['title']}
EOF;

 

$result = $api->sendSms($message,$mobile);

 

 

 

I use Heredoc a lot myself, but be careful. If you add another character after the EOF; (a space, tab anything) it will throw an error.

 

So

 

EOF;

 

on one line, all by itself, no characters after.





Jarle Dahl Bergersen | Referral Links: Want $50 off when you join Octopus Energy? Use this referral code
Are you happy with what you get from Geekzone? Please consider supporting us by making a donation or subscribing.


Create new topic








Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.