如何将循环放入字符串php中?

时间:2018-09-07 04:00:22

标签: php arrays string

我要发送的电子邮件正文的此代码...所以我想循环输入该名称,这样我将显示为该名称abu,ali,rajesh名称

  $z = oci_parse($conn, "SELECT name FROM usr where usr_id=:num1");
oci_bind_by_name($z, ":num1",$INIT);
oci_execute($z);
$row = oci_fetch_row($z);
           $MERC_HAND_USR_ID_name[]=$row[0];

       }

$body='Dear Helpdesk Manager

     We would like to inform that a new report has been made by.

    Name:   '.foreach($MERC_HAND_USR_ID_name as $key => $value)
    {
       $value;

    }.'

    date:  '. $date.'
    time:  '. $time.'

    Thank You,

    - Helpdesk ';

我有可能这样做或有其他方法吗?

3 个答案:

答案 0 :(得分:2)

请尝试这个。

$body='Dear Helpdesk Manager

We would like to inform that a new report has been made by.';
foreach($MERC_HAND_USR_ID_name as $key => $value)
{
    $body.= '<br> &nbsp Name:   '.$value;
}
$body.='
<br><br>
date:  '.date("Y/m/d").'
<br>
time:  '. date("h:i:sa").'
<br><br>Thank You,<br><br>- Helpdesk ';

答案 1 :(得分:1)

您必须像下面那样更改$ body变量

$body='Dear Helpdesk Manager

 We would like to inform that a new report has been made by.

';
foreach($MERC_HAND_USR_ID_name as $key => $value)
{
  $body .= 'Name: ' . $value . ' ';

}
$body .= '

date:  '. $date.'
time:  '. $time.'

Thank You,

- Helpdesk ';

由于必须遍历$ MERC_HAND_USR_ID_name,因此必须通过将邮件正文附加到foreach循环中来准备邮件正文。

答案 2 :(得分:1)

您不能在字符串内使用循环。 您需要先使用名称创建一个字符串,然后将其与$ body字符串连接。

类似这样的东西:

$z = oci_parse($conn, "SELECT name FROM usr where usr_id=:num1");
oci_bind_by_name($z, ":num1",$INIT);
oci_execute($z);
$row = oci_fetch_row($z);
$MERC_HAND_USR_ID_name[]=$row[0];

//} // ==> This closes nothing

$names = '';

foreach($MERC_HAND_USR_ID_name as $key => $value) {
    $name .= 'Name: ' . $value; //here you can manipulate it however you want... like maybe add a space between values (. ' ') or a line break (<br/>)
}

$body='Dear Helpdesk Manager
       We would like to inform that a new report has been made by '. 
       $name .
       'date:  '. $date .
       'time:  '. $time.
       'Thank You,
       - Helpdesk ';