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.




4564 posts

Uber Geek

Trusted

#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
211 posts

Master Geek

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™




4564 posts

Uber Geek

Trusted

  #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?

 
 
 
 


7064 posts

Uber Geek

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?




4564 posts

Uber Geek

Trusted

  #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.


7064 posts

Uber Geek

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.




4564 posts

Uber Geek

Trusted

  #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!

1766 posts

Uber Geek

Subscriber

  #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);

 

 

 


 
 
 
 


Webhead
2518 posts

Uber Geek

Moderator
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.


Create new topic




News »

Freeview On Demand app launches on Sony Android TVs
Posted 6-Aug-2020 13:35


UFB hits more than one million connections
Posted 6-Aug-2020 09:42


D-Link A/NZ extends COVR Wi-Fi EasyMesh System series with new three-pack
Posted 4-Aug-2020 15:01


New Zealand software Rfider tracks coffee from Colombia all the way to New Zealand businesses
Posted 3-Aug-2020 10:35


Logitech G launches Pro X Wireless gaming headset
Posted 3-Aug-2020 10:21


Sony Alpha 7S III provides supreme imaging performance
Posted 3-Aug-2020 10:11


Sony introduces first CFexpress Type A memory card
Posted 3-Aug-2020 10:05


Marsello acquires Goody consolidating online and in-store marketing position
Posted 30-Jul-2020 16:26


Fonterra first major customer for Microsoft's New Zealand datacentre
Posted 30-Jul-2020 08:07


Everything we learnt at the IBM Cloud Forum 2020
Posted 29-Jul-2020 14:45


Dropbox launches native HelloSign workflow and data residency in Australia
Posted 29-Jul-2020 12:48


Spark launches 5G in Palmerston North
Posted 29-Jul-2020 09:50


Lenovo brings speed and smarter features to new 5G mobile gaming phone
Posted 28-Jul-2020 22:00


Withings raises $60 million to enable bridge between patients and healthcare
Posted 28-Jul-2020 21:51


QNAP integrates Catalyst Cloud Object Storage into Hybrid Backup solution
Posted 28-Jul-2020 21:40



Geekzone Live »

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


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



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.

Alternatively, you can receive a daily email with Geekzone updates.