Thursday, 30 August 2018

php foreach loop with array

I have the below syntax:

function get_outstandingchecking($db)
{
    $result = $db->query("  SELECT distinct(haulier) as haulier from v2loads where  adminstatus='captured' and  DATEDIFF(now(), capturedate) >4 group by haulier,DATEDIFF(now(), capturedate) order by DATEDIFF(now(), capturedate) desc  ");
    return $result;
}

$outstandingchecking = get_outstandingchecking($db);
$outstandingchecking_row_count = $outstandingchecking->rowCount();

if ($outstandingchecking_row_count > 0) {
    $i=0;

    foreach ($outstandingchecking as $instance) {
        echo $instance[$i];
        $i=$i+1;
        //syntax email relevent haulier
    }
}

The $result query fetches an array of values(hauliers) that are affected. I then want to use each of the returned 'hauliers' and send email notifications to them.
The output shows only the first value in the query results, how can I get it to show all the values as part of the foreach loop?
Thanks as always,

Try
foreach($outstandingchecking as $instance){
   echo $instance[0];
}

You don't need $i when using foreach

0 comments:

Post a Comment