在字符串内的while循环中运行一个函数?

时间:2013-08-17 01:48:04

标签: php mysql

我试图生成一封电子邮件,在正文中包含一些已排序的数据。 我的电子邮件发送但功能中没有正文中的数据。函数也是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的脚本我得到了所需的输出。 有什么方法可以让这个工作吗?在此先感谢尼克

1 个答案:

答案 0 :(得分:0)

由于您的通话试图包含该功能的结果,因此您只需要确保返回所有沿途生成的线路。有很多方法可以做到这一点,其中之一是:

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;
}