Search code examples
phppdoforeachfetchall

PDO FetchAll Into PHP Mail


So here is where I get the current information from the database...

$ci = $currentinfo->fetchAll(PDO::FETCH_ASSOC);

And here is where I get some info from the past week...

$pi = $pastinfo->fetchAll(PDO::FETCH_ASSOC);

Now I need a foreach statement that puts all of the information into a PHP mail function. Something along the lines of this...

foreach ($ci as $civalue) {
foreach($pi as $pivalue)

$result1 = $ci["info1"]-$pi["info1"];
$result2 = etc...
        $body = $result1, etc...;
        $to = '$civalue["email"]';
        $subject = 'Weekly Recap';
        $headers = 'From: [email protected]';
        mail($to, $subject, $body, $headers);

Solution

  • You mean something like this:

    $result = array();
    for ($i = 0; $i < count($ci); $i++) {
        $result[] = $ci[$i]["info1"] - $pi[$i]["info1"];
    }
    
    
    $body = implode(', ', $result);
    $to = '$civalue["email"]';
    $subject = 'Weekly Recap';
    $headers = 'From: [email protected]';
    mail($to, $subject, $body, $headers);