将关联数组结果放入电子邮件中?

时间:2018-01-10 14:05:16

标签: php mysql email

所以我有一个查询来设定联盟的结果,一旦联赛结束,我就会计划通过电子邮件发送联盟的结果,我想知道最好的方法是什么。

我的联赛代码:

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

        <?php  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;

          while($row = $result->fetch_assoc()){ ?>


          <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 and email results
        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

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

            $headers = "From: localhost";

                    mail($to, $subject, $body, $headers);
                header("location: viewleagues.php");

        }
          } ?>
         </div>
       </div>

我如何使用上表中的信息并检索它以放入电子邮件?

1 个答案:

答案 0 :(得分:0)

你可以将结果存储到一个数组中并像这样进一步使用它

     $newArray = [];
      while($row = $result->fetch_assoc()){
          $newArray[] = $row;
      }

为每个成员发送电子邮件做类似的事情

foreach($newArray as $row){
  mailFunction($row["email"]); //Your function to send mail
 }