【问题标题】:using php foreach for data in an email使用 php foreach 获取电子邮件中的数据
【发布时间】:2018-06-19 20:07:45
【问题描述】:

所以我有一个查询,它从数据库中提取数据并放入一个表中,它也像这样进入一个数组:

<table class="list">
      <tr>
        <th>Rank</th>
        <th>Username </th>
        <th> Most Steps</th>
        <th> Average Steps </th>
        <th> Total Steps </th>
      </tr>

        <?php //get info for scores in the league
         if ($result = $link->query("SELECT SUM(DISTINCT step_count.steps) as total,  logins.nickname, MAX(step_count.steps) as maxsteps, ROUND(AVG (DISTINCT step_count.steps)) as average, logins.Email as email
            FROM step_count
            INNER JOIN logins on
              step_count.unique_id=logins.unique_id
            INNER JOIN leagues ON
              leagues.unique_id=logins.unique_id
          WHERE step_count.date BETWEEN '$info[start_date]' AND '$info[end_date]'
            GROUP BY logins.unique_id
            ORDER BY `total` DESC
          ", MYSQLI_USE_RESULT))
          $rank = 1;


          //add data to an arry for email use
          $leaguedata = array();

          while($row = $result->fetch_assoc()){
              $leaguedata[] = $row; ?>


          <tr>
           <td><?php echo $rank++; ?></td>
           <td><?php echo $row['nickname']; ?></td>
           <td><?php echo $row['maxsteps']; ?></td>
           <td><?php echo $row['average']; ?></td>
           <td><?php echo $row['total']; ?></td>
          </tr>

                <?php }  $result->close(); ?>

           </table>

然后我有一个区域,一旦联赛结束,我会在其中发送一封包含结果的电子邮件:

<?php //if admin and the date has past, change active to 0
       if ($info['role'] == "ADMIN") {
         if ($info['end_date'] < date("Y-m-d")) {

         $endleague = "UPDATE leagues SET active = 0
          WHERE joincode='$joincode'";

          mysqli_query($link,$endleague);

          echo "<h3><br/>This league has now ended, results will be sent to everyone via email!</h3>";

          //send league ending information
          $endrank = 1;

          $subject = "$league_name has ended!";
            $body="Results are in and these were the final scores:".PHP_EOL;
            $body.="$endrank++".PHP_EOL; 

            $headers = "From: localhost";

          foreach ($leaguedata as $email){
                    mail($email['email'], $subject, $body, $headers); }


        }
          } ?>

我不确定 foreach 循环如何在电子邮件正文中工作,以及如何让 $endrank++ 在正文中工作。

【问题讨论】:

    标签: php mysql email mysqli


    【解决方案1】:

    您的endrank 行可能是:

    $subject = "$league_name has ended!";
    $body="Results are in and these were the final scores:".PHP_EOL;
    $endrank++;
    $body.=$endrank.PHP_EOL;
    

    至于foreach 语句,假设您想从行中添加一些数据:

    // Keep anything static between all emails outside of your loop
    $endrank = 1;
    $endrank++;
    $subject = "$league_name has ended!";
    $headers = "From: localhost";
    
    // For your big array of all your rows ($leaguedata), do something for each row
    foreach ($leaguedata as $row){
    
        // Initialise your new text body
        $body;
    
        // Access anything from each row as $row['something']
        $body .= "Hello, $row['nickname']".PHP_EOL;
    
        // Pop in the league results which are common to all the emails
        $body .="Results are in and these were the final scores:".PHP_EOL;
        $body .=$endrank.PHP_EOL;
    
        // Send the email
        mail($row['email'], $subject, $body, $headers);
    }
    

    【讨论】:

    • $endrank 非常好用,谢谢!至于 foreach 语句,我的意思是在 $endrank 所在的实际电子邮件中
    • 好吧,$endrank 被放置在 $body 中,它只是连接在一起的 2 个字符串,实际上是您电子邮件的内容。当你说你不确定它是如何工作的 - 它是否有效,或者如果不是,你想要找出它是什么?
    • 我想将数组中不同的 $rows 添加到电子邮件中,以便它基本上向用户发送该联赛的结果,例如: foreach ($leaguedata as $nickname) { $body. ="姓名:$nickname['昵称']".PHP_EOL; }
    • 啊,更清楚了,我已经更新了我的答案,以展示如何在每封电子邮件中使用您的数据库变量。为这类工作做一个传统的for 循环也是有意义的
    猜你喜欢
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多