php - 在字符串内的 while 循环内运行函数?

标签 php mysql

我正在尝试生成一封电子邮件以在正文中包含一些排序的数据。 我的电子邮件已发送,但正文中没有函数的数据。 by 函数也是 while 循环,它们会生成回显的 html 内容,如果这有意义的话? 这是其中一项功能。

function ye($coid){
$yest = date("Y-m-d", time() - 86400);
$inouty= mysql_query("SELECT clockings.id AS cid,clockings.uid, clockings.con,        clockings.coff, staff.sname ,staff.id, staff.act, staff.coid FROM clockings LEFT JOIN staff ON clockings.uid=staff.id WHERE clockings.dte = '$yest' AND staff.coid =$coid ORDER BY staff.id, clockings.id")or die(mysql_error());
     while ($row = mysql_fetch_assoc($inouty)) {
       $sname= $row['sname'];
       $in=  $row['con'];
       $out= "- ". $row['coff'];
       $id = $row['cid'];               
echo"$sname $in $out<br>";}}   

这就是我所说的

$html_text = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
                  <html>
                     <head>
                        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
                        <title>Email</title>
                     </head>

                     <body>details Current status for $nicedate as of $now<br>". tod($coid)."<br>Yesterdays Clockings<br>". ye($coid)."</body>
                  </html>";

不,如果我更改函数以返回值而不是回显,我只会得到第一行结果而不是完整循环,但这包含在电子邮件正文文本中,并且当我在末尾调用该函数时在我的函数中使用 echo 的脚本我得到了所需的输出。 有什么办法可以让它发挥作用吗?预先感谢尼克

最佳答案

由于您的调用试图包含函数的结果,因此您只需确保返回沿途生成的所有行。有很多方法可以做到这一点,这里是一个:

function ye($coid)
{
  $yest = date("Y-m-d", time() - 86400);
  $inouty= mysql_query("SELECT clockings.id AS cid,clockings.uid, clockings.con,        clockings.coff, staff.sname ,staff.id, staff.act, staff.coid FROM clockings LEFT JOIN staff ON clockings.uid=staff.id WHERE clockings.dte = '$yest' AND staff.coid =$coid ORDER BY staff.id, clockings.id")or die(mysql_error());

  $result = "";

  while ($row = mysql_fetch_assoc($inouty)) {
     $sname = htmlspecialchars($row['sname']);
     $in    = htmlspecialchars($row['con']);
     $out   = "- ". htmlspecialchars($row['coff']);

     $result .= "$sname $in $out<br>";
  }

  return $result;
} 

关于php - 在字符串内的 while 循环内运行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18284323/

相关文章:

php - OrientDB PHP/Gremlin 桥

php - 通过Ajax提交表单

php - 选择 msql 中的计数公式 - 速度

mysql - "!= or !="选择一切

php - 抓取给定关键字前后的 x 个单词?

php - 为什么 APC 无法在 Travis-CI 上的 PHP v5.5 中工作?

PHP如何将数组的下一个元素递归附加到父级

php - 返回不同模型的对象列表的 JOIN 查询?

c# - 如何使用 ado.net 从 mssql 中选择数据

PHPMyAdmin 数据库用 0 而不是字符串填充字段