0

So I have a query that sets results from a league and once that league is finished, I then plan to have it email the results from the league and I was wondering what would be the best way to do this.

My league code:

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

and then this is going to be the email that gets sent to every member that was in this league:

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

How would I use the information in the table above and retrieve it to put into the email?

  • 1
    [Little Bobby](http://bobby-tables.com/) says **[you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/)**. Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). I recommend `PDO`, which I [wrote a class for](https://github.com/GrumpyCrouton/GrumpyPDO) to make it extremely easy, clean, and more secure than using non-parameterized queries. Also, [This article](https://phpdelusions.net/pdo/mysqli_comparison) may help you choose between `MySQLi` and `PDO` – GrumpyCrouton Jan 10 '18 at 14:06
  • This is not part of your question but please use `==` instead of `=` in `if ($info['role'] = 'ADMIN')`, because this wouldn't be a check for equality, but an assignment which would yield `true` every time – Stefan Jan 10 '18 at 14:32

1 Answers1

0

well you can store results into a array and use it further like this

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

for sending emails for each member do something like this

foreach($newArray as $row){
  mailFunction($row["email"]); //Your function to send mail
 }
Vikas Kandari
  • 793
  • 6
  • 10