Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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,

share|improve this question
5  
What is $db in this case? An instance of PDO? If so, you need to call fetch() at some point to get the results one by one or fetchAll() to get an array with all results. – Till Helge Jan 15 at 8:31
can u provied the result of print_r($instance); – DBK Jan 15 at 8:31
@Till Helge Helwig, thanks I will try this, makes sense. I will revert. – Smudger Jan 15 at 8:33

2 Answers

up vote 2 down vote accepted

Try

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

You don't need $i when using foreach

share|improve this answer
Perfect, works great thanks Peter. – Smudger Jan 15 at 8:37

Try this code.

$final=array();
foreach($outstandingchecking as $instance){ 

$final[]=$instance;

$i=$i+1;

//syntax email relevent haulier

}

print_r($final);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.