回声在heredoc / nowdoc语法 [英] Echo in heredoc / nowdoc syntax

查看:102
本文介绍了回声在heredoc / nowdoc语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Heredoc和Nowdoc语法,我正在努力理解如何突破它,以便我可以添加其他功能。



我想在这部分之后突破它(如果可能的话):

  User :: sendNewticket($ send_to,维护票.Input :: get('st_id'),

但是当我发生语法错误添加?> 之后'维护单'.Input :: get('st_id'),?> / p>

无论如何,我暂时放弃了,如果我能弄清楚如何使用以下语法回应以下内容,我会很乐意:

 '$ fn。'< br> 
'。$ time。'< br>
' $ stc。'< br>

类似于这个 echo $ fn;



这是我的情况的相关代码。

 <?php 

include($ _ SERVER ['DOCUMENT_ROOT']。/core/init.ph p);

//新数据
$ send_to = $ _POST ['send_to'];
$ rec_message = $ _POST ['rec_message'];

//消息
$ status = $ _POST ['status'];
$ st_id = $ _POST ['st_id'];
$ posted_by = $ _POST ['posted_by'];
$ posted_on = $ _POST ['posted_on'];
$ unit = $ _POST ['unit'];
$ subject = $ _POST ['subject'];
$ content = $ _POST ['content'];

// Loader& Flash消息
echo'< meta http-equiv =refreshcontent =5; URL = / admin / maintenance / email-ticket.php?st_id ='$ st_id。'>';
会话:: flash('email-ticket','< h3 class =orange-txalign =center>您的电子邮件已发送!< / h3>');

//回复
// $ id = $ _POST ['id'];
// $ from_name = $ _POST ['from_name'];
// $ st_time = $ _POST ['st_time'];
// $ st_content = $ _POST ['st_content'];

//从数据库拉出对话框
$ st_messages = DB :: getInstance() - > query(SELECTid,st_id,from_name,st_content `st_time` FROM`st_messages` WHERE`st_id` = $ st_id ORDER BY id ASC);


foreach($ st_messages-> results()as $ mt){
if($ mt-> from_name =='Support Team'){
} else {
}

$ fn = escape($ mt-> from_name);
$ time = escape(date(F d,Y - h:i a,strtotime($ mt-> st_time)));
$ stc = nl2br(escape($ mt-> st_content));

// START EMAIL
用户:: sendNewticket($ send_to,'维护票'.Input :: get('st_id'),
'你好 - < br> < br>
'.Input :: get('rec_message')。'< br>< br>


< strong>票信息: / strong>< br>< br>

< strong>状态:< / strong>'输入:: get('status')'< br>
< strong> Ticket#:< / strong>'。Input :: get('st_id')。'< br>
< strong>发布者:< / strong> :get('posted_by')。'< br>
< strong>发布于:< / strong>'输入:: get('posted_on')'< br>
< strong>建筑单位:< / strong>'。Input :: get('unit')。'< br>
< strong>主题:< / strong> ('subject')。'< br>
< strong> Ticket Message:< / strong>'。Input :: get('content')。'< br>< br>

< strong>对话:< / strong>< br>< br&

'。$ fn。'< br>
'。$ time。'< br>
'。$ stc。'< br&
---------------------------------< br>< br>


谢谢,< br>
支持团队');
}
?>

我想要做的是将支持票对话电子邮件发送给一个收件人。
像这样:





我从消息$ _POST获取原始消息。以及新数据$ _POST的电子邮件地址和电子邮件。



对于对话,我试图从数据库中提取该信息表



我的当前代码通过电子邮件通过电子邮件通话,但以多个电子邮件发送电子邮件。



示例,如果会话中有3个条目发送3封电子邮件:




  • 电子邮件1包含:管理员回复1

  • <电子邮件2包含:用户回复1
  • 电子邮件3包含:管理员回复2



我想如果我得到回音,它会将整个对话分组成一个电子邮件。



这是我试图获得的最终电子邮件输出的一个例子:



您好 -



嘿,请查看此机票并妥善处理此问题。
感谢



票证信息:




  • 状态:OPEN

  • 票号#201406016

  • 发布者:用户名

  • 2014年6月27日 - 03:46 pm

  • 建筑单位:D47

  • 主题:1

  • 消息:1



对话:



支持团队



2014年7月1日 - 09:19 am



管理员回复1






用户名



2014年7月1日 - 09:19 am



用户回复1






支持小组



2014年7月1日 - 09:19 am



管理员回复2






谢谢,



支持小组






END电子邮件示例



任何建议都将有所帮助。



更新



我开始下这更好一点。我收到的唯一错误是我的foreach循环不知道如何纠正它。



这是代码:

  $ st_messages = DB :: getInstance() - > query(SELECTid,st_id,from_name,st_content,st_time st_messages` WHERE`st_id` = $ st_id ORDER BY id ASC); 

// START EMAIL
$ recMessage = Input :: get('rec_message');
User :: sendNewticket($ send_to,'Maintenance Ticket'.Input :: get('st_id'),<< TEXT
Hello - < br>< br>
$ recMessage
TEXT

foreach($ st_messages-> results()as $ mt){

$ fn =echo escape($ mt- > from_name);;
$ time =echo escape(date(\F d,Y - h:ia\,strtotime($ mt-> st_time)));;
$ stc =echo nl2br(escape($ mt-> st_content));;
}


<< TEXT2
< strong>对话:< / strong>< br>< br>

$ fn< br>
$ time< br>
$ stc< br& ;< br>

感谢,< br>
支持团队
);
TEXT2

?>


解决方案

我相信这是你打算做的,这有点awkard:

  $ fn = escape($ mt-> from_name); 
$ time = escape(date(F d,Y - h:i a,strtotime($ mt-> st_time)));
$ stc = nl2br(escape($ mt-> st_content));

// START EMAIL
User :: sendNewticket($ send_to,'Maintenance Ticket'。Input :: get('st_id'),<< TEXT
你好 - < br>< br>
TEXT
。Input :: get('rec_message')。<< TEXT2
< br>< br>

< strong>对话:< / strong>< br>< br>

$ fn< br>
$ time< br>
$ stc< br>

TEXT2
);

heredoc 的开始必须是行的最后一件事,所以在此之后,你下拉并立即进入文本块。 heredoc的结束必须是唯一的在线,所以连接它,你必须下拉一行才能使用运算符()。您也不需要在heredoc内使用任何引号或附加操作符。



事实上,这是非常直观的,即使StackOverflow语法荧光笔也是错误的,当我执行此操作时,第一个<< TEXT 正常工作:

  $ mt = new stdClass(); 
函数escape($ v){return $ v;}
class User {static function sendNewTicket($ a,$ b,$ c){echo $ c;}}
class Input { static function get($ a){return'Got something';}}
$ send_to ='';
$ mt-> from_name ='Kitty';
$ mt-> st_time ='11月5日';
$ mt-> st_content =<< CONTENT
亲爱的朱迪,

Lorem Dipsum和所有爵士乐。

面条,
Kitty。
内容;

$ fn = escape($ mt-> from_name);
$ time = escape(date(F d,Y - h:i a,strtotime($ mt-> st_time)));
$ stc = nl2br(escape($ mt-> st_content));

// START EMAIL
User :: sendNewticket($ send_to,'Maintenance Ticket'。Input :: get('st_id'),<< TEXT
你好 - < br>< br>
TEXT
。Input :: get('rec_message')。<< TEXT2
< br>< br>

< strong>对话:< / strong>< br>< br>

$ fn< br>
$ time< br>
$ stc< br>

TEXT2
);

它生成:

 你好 - < br>< br>得到一些< br>< br> 

< strong>对话:< / strong>< br>< br>

Kitty< br>
2014年11月5日 - 12:00 am< br>
亲爱的朱迪,< br />
< br />
Lorem Dipsum和所有爵士乐。< br />
< br />
Toodles,< br />
Kitty。< br>

虽然我相信你的数据会更好看。



编辑:和 Blizz 非常正确地指出,如果您保存 rec_message 到变量中,可以避免连接:

  $ recMessage =输入::得到( 'rec_message'); 
User :: sendNewticket($ send_to,'Maintenance Ticket'。Input :: get('st_id'),<< TEXT
Hello - < br>< br>
$ recMessage
< br>< br>

< strong>对话:< / strong>< br>< br>

$ fn< br>
$ time< br>
$ stc< br>

TEXT
);






编辑2 使用额外的消息,您应该使用这样的方式来简化电子邮件:

  $ messages =''; 
foreach($ st_messages-> results()as $ mt)
{

$ fn = escape($ mt-> from_name);
$ time = escape(date(F d,Y - h:i a,strtotime($ mt-> st_time)));
$ stc = nl2br(escape($ mt-> st_content));

$ messages。=<<<< MESSAGE

$ fn< br>
$ time< br>
$ stc< br>< br>

MESSAGE;
}

$ recMessage = Input :: get('rec_message');
User :: sendNewticket($ send_to,'Maintenance Ticket'。Input :: get('st_id'),<< TEXT
Hello - < br>< br>
$ recMessage
< strong>对话:< / strong>< br>< br>

$ messages

感谢,&br> ;
支持团队

TEXT
);
?>

其中一些基本的虚拟文本产生:



< pre class =lang-html prettyprint-override> Hello - < br>< br>
有东西
< strong>对话:< / strong>< br>< br>


人< br>
2014年11月11日 - 12:00 am< br>
我的消息blah blah< br>< br>

sdfasdf< br>
2014年4月11日 - 12:00 am< br>
我的消息asdfasdf blah blah< br>< br>


感谢,< br>
支持团队


I've been reading up on the Heredoc and Nowdoc syntax, and I'm trying to understanding how to break out of it so I can add other functionality.

I would like to break out of it after this part (if possible):

User::sendNewticket($send_to, 'Maintenance Ticket '.Input::get('st_id'),

but throws a syntax error when I add ?> after 'Maintenance Ticket '.Input::get('st_id'), ?>

Anyways, I gave up on that for the time being and I would be good to go if I could figure out how to echo the following using this syntax:

'.$fn.'<br>
'.$time.'<br>
'.$stc.'<br>

Similar to this echo $fn;

Here is relevant code to my situation.

<?php

include($_SERVER['DOCUMENT_ROOT'] . "/core/init.php");

// new data
$send_to           = $_POST['send_to'];
$rec_message       = $_POST['rec_message'];

//Message
$status            = $_POST['status'];
$st_id             = $_POST['st_id'];
$posted_by         = $_POST['posted_by'];
$posted_on         = $_POST['posted_on'];
$unit              = $_POST['unit'];
$subject           = $_POST['subject'];
$content           = $_POST['content'];

//Loader & Flash Message
echo '<meta http-equiv="refresh" content="5;URL=/admin/maintenance/email-ticket.php?st_id='.$st_id.'">'; 
Session::flash('email-ticket', '<h3 class="orange-tx" align="center">Your Email has been sent!</h3>');

//Replies
//$id                = $_POST['id'];
//$from_name           = $_POST['from_name'];
//$st_time         = $_POST['st_time'];
//$st_content          = $_POST['st_content'];

//Pulling Conversation from database
$st_messages = DB::getInstance()->query("SELECT `id`,`st_id`,`from_name`,`st_content`,`st_time` FROM `st_messages` WHERE `st_id` = $st_id ORDER BY id ASC");


foreach ($st_messages->results() as $mt) {
if($mt->from_name=='Support Team'){
} else {
}

$fn = escape ($mt->from_name);
$time = escape (date("F d, Y - h:i a", strtotime ($mt->st_time)));
$stc = nl2br (escape($mt->st_content));

//START EMAIL
User::sendNewticket($send_to, 'Maintenance Ticket '.Input::get('st_id'), 
'Hello -<br><br>
'.Input::get('rec_message').'<br><br>


<strong>Ticket Info:</strong><br><br>

<strong>Status: </strong>'.Input::get('status').'<br>
<strong>Ticket #: </strong>'.Input::get('st_id').'<br>
<strong>Posted By: </strong>'.Input::get('posted_by').'<br>
<strong>Posted On: </strong>'.Input::get('posted_on').'<br>
<strong>Building Unit: </strong>'.Input::get('unit').'<br>
<strong>Subject: </strong>'.Input::get('subject').'<br>
<strong>Ticket Message: </strong>'.Input::get('content').'<br><br>

<strong>Conversation:</strong><br><br>

'.$fn.'<br>
'.$time.'<br>
'.$stc.'<br>
---------------------------------<br><br>


Thank You,<br>
Support Team');
}
?>

What I'm trying to do is email a support ticket conversation to one recipient. Like this:

I'm getting the "Original Message" from the Message $_POST. And the email address and email message from the New Data $_POST.

For the Conversation I'm trying to pull that info from the database table.

My current code emails the conversation, but emails each conversation in multiple emails.

Example, If there are 3 entries in the conversation it sends 3 emails:

  • Email 1 contains: Admin reply 1
  • Email 2 contains: User reply 1
  • Email 3 contains: Admin reply 2

I think if I get the echo working it will group the entire conversation into one email.

This is an example of the final email output I trying to obtain:

Hello -

Hey Please view this ticket and take care of the problem. Thanks

Ticket Info:

  • Status: OPEN
  • Ticket #: 201406016
  • Posted By: User Name
  • Posted On: June 27, 2014 - 03:46 pm
  • Building Unit: D47
  • Subject: 1
  • Ticket Message: 1

Conversation:

Support Team

July 01, 2014 - 09:19 am

Admin reply 1


User Name

July 01, 2014 - 09:19 am

User reply 1


Support Team

July 01, 2014 - 09:19 am

Admin reply 2


Thank You,

Support Team


END Email Example

Any suggestions would be helpful.

UPDATE

I'm starting to understand this a little better. The only error I'm receiving is in my foreach loop not sure how to correct it.

Here is the code:

$st_messages = DB::getInstance()->query("SELECT `id`,`st_id`,`from_name`,`st_content`,`st_time` FROM `st_messages` WHERE `st_id` = $st_id ORDER BY id ASC");

//START EMAIL
$recMessage = Input::get('rec_message');
User::sendNewticket($send_to, 'Maintenance Ticket '.Input::get('st_id'), <<<TEXT
Hello -<br><br>
$recMessage
TEXT

foreach ($st_messages->results() as $mt) {

$fn = "echo escape ($mt->from_name);";
$time = "echo escape (date(\"F d, Y - h:i a\", strtotime ($mt->st_time)));";
$stc = "echo nl2br (escape($mt->st_content));";
}


<<<TEXT2
<strong>Conversation:</strong><br><br>

$fn<br>
$time<br>
$stc<br><br>

Thanks,<br>
Support Team
);
TEXT2

?>

解决方案

I believe this is what you were intending to do, it's a bit awkard looking:

$fn = escape($mt->from_name);
$time = escape(date("F d, Y - h:i a", strtotime($mt->st_time)));
$stc = nl2br(escape($mt->st_content));

//START EMAIL
User::sendNewticket($send_to, 'Maintenance Ticket ' . Input::get('st_id'), <<<TEXT
Hello -<br><br>
TEXT
        . Input::get('rec_message') . <<<TEXT2
<br><br>

<strong>Conversation:</strong><br><br>

$fn<br>
$time<br>
$stc<br>

TEXT2
);

The start of a heredoc must be the last thing on a line, so after that you drop down and immediately go into the text block. The end of the heredoc must be the only thing on the line, so to concatenate it you have to drop down a line before you can use the operator (.). You also don't need to use any quotation marks or append operators when inside a heredoc.

It's so unintuitive in fact, that even the StackOverflow Syntax highlighter gets it wrong, the first <<<TEXT works fine when I ran this:

$mt = new stdClass();
function escape($v){return $v;}
class User{static function sendNewTicket($a, $b, $c){echo $c;}}
class Input{static function get($a){return 'Got something';}}
$send_to = '';
$mt->from_name = 'Kitty';
$mt->st_time = '5th November';
$mt->st_content = <<<CONTENT
    Dear Judy,

        Lorem Dipsum and all that jazz.

        Toodles,
            Kitty.
CONTENT;

$fn = escape($mt->from_name);
$time = escape(date("F d, Y - h:i a", strtotime($mt->st_time)));
$stc = nl2br(escape($mt->st_content));

//START EMAIL
User::sendNewticket($send_to, 'Maintenance Ticket ' . Input::get('st_id'), <<<TEXT
Hello -<br><br>
TEXT
        . Input::get('rec_message') . <<<TEXT2
<br><br>

<strong>Conversation:</strong><br><br>

$fn<br>
$time<br>
$stc<br>

TEXT2
);

It produced:

Hello -<br><br>Got something<br><br>

<strong>Conversation:</strong><br><br>

Kitty<br>
November 05, 2014 - 12:00 am<br>
    Dear Judy,<br />
<br />
        Lorem Dipsum and all that jazz.<br />
<br />
        Toodles,<br />
            Kitty.<br>

Though I'm sure it'll look better with your data.

Edit: And as Blizz very correctly points out below, if you save the rec_message into a variable, you can avoid the concatenation altogether:

$recMessage = Input::get('rec_message');
User::sendNewticket($send_to, 'Maintenance Ticket ' . Input::get('st_id'), <<<TEXT
Hello -<br><br>
$recMessage
<br><br>

<strong>Conversation:</strong><br><br>

$fn<br>
$time<br>
$stc<br>

TEXT
);


Edit 2: With the extra messages, you should use something like this to simplify the email:

$messages = '';
foreach($st_messages->results() as $mt)
{

    $fn = escape($mt->from_name);
    $time = escape(date("F d, Y - h:i a", strtotime($mt->st_time)));
    $stc = nl2br(escape($mt->st_content));

    $messages .= <<<MESSAGE

$fn<br>
$time<br>
$stc<br><br>

MESSAGE;
}

$recMessage = Input::get('rec_message');
User::sendNewticket($send_to, 'Maintenance Ticket ' . Input::get('st_id'), <<<TEXT
Hello -<br><br>
$recMessage
<strong>Conversation:</strong><br><br>

$messages

Thanks,<br>
Support Team

TEXT
);
?>

Which with some basic dummy text produces:

Hello -<br><br>
Got something
<strong>Conversation:</strong><br><br>


Person<br>
November 11, 2014 - 12:00 am<br>
My message blah blah<br><br>

sdfasdf<br>
April 11, 2014 - 12:00 am<br>
My message asdfasdf blah blah<br><br>


Thanks,<br>
Support Team

这篇关于回声在heredoc / nowdoc语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆