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.




4124 posts

Uber Geek


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

Master Geek

Trusted

  # 2146628 17-Dec-2018 17:27
One person supports this post
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™




4124 posts

Uber Geek


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

 
 
 
 


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




4124 posts

Uber Geek


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


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




4124 posts

Uber Geek


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

1676 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
2264 posts

Uber Geek

Moderator
Trusted
Lifetime subscriber

  # 2146787 18-Dec-2018 00:19
2 people support this post
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



Twitter and LinkedIn »



Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

Disney+ streaming service confirmed launch in New Zealand
Posted 20-Aug-2019 09:29


Industry plan could create a billion dollar interactive games sector
Posted 19-Aug-2019 20:41


Personal cyber insurance a New Zealand first
Posted 19-Aug-2019 20:26


University of Waikato launches space for esports
Posted 19-Aug-2019 20:20


D-Link ANZ expands mydlink ecosystem with new mydlink Mini Wi-Fi Smart Plug
Posted 19-Aug-2019 20:14


Kiwi workers still falling victim to old cyber tricks
Posted 12-Aug-2019 20:47


Lightning Lab GovTech launches 2019 programme
Posted 12-Aug-2019 20:41


Epson launches portable laser projector
Posted 12-Aug-2019 20:27


Huawei launches new distributed HarmonyOS
Posted 12-Aug-2019 20:20


Lenovo introduces single-socket servers for edge and data-intensive workloads
Posted 9-Aug-2019 21:26


The Document Foundation announces LibreOffice 6.3
Posted 9-Aug-2019 16:57


Symantec sell enterprise security assets for US$ 10.7 billion to Broadcom
Posted 9-Aug-2019 16:43


Artificial tongue can distinguish whisky and identify counterfeits
Posted 8-Aug-2019 20:20


Toyota and Preferred Networks to develop service robots
Posted 8-Aug-2019 20:11


Vodafone introduces new Vodafone TV device
Posted 7-Aug-2019 17:16



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.