Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have to loop through an object called $logs in order to display queried data. I would also like to loop through an array of numbers I’ve created in order to have them line up next to each row of queried data. From my code I get an array to string conversion error. Does anyone know why this is happening?

$sql1 = "SELECT * FROM client_table GROUP BY client_name";
$query = $this->db->prepare($sql1);
$query->execute();
$logs = $query->fetchAll();

$totals = array($darty, $doug, $eliott, $henry, $leo, $neo, $rforgo, $sample,         
$susanne, $tim);

foreach ($logs as $log) {

    echo date("Y  m-d  ");
    echo "2nd half  ";
    echo $log->client_name . " ";
    $totals . "\n" ;

}
share|improve this question
    
What does var_dump($logs) give you? –  Dan Jun 2 at 14:34
    
Check your penultimate line.. ` $totals . "\n" ;` –  enapupe Jun 2 at 14:36
    
The array has nothing to do with the query, FYI. –  Anthony Jun 2 at 14:40

2 Answers 2

up vote 2 down vote accepted

On the last line: $totals is an array, and you're concatenating it with a string. So it tries to treat it as a string. Either use implode or loop through it. And echo it if that's what you want to do.

echo implode(', ', $totals) . "\n";
share|improve this answer
    
Although, this will help, it's more as a comment, not an actual answer. –  Dainis Abols Jun 2 at 14:41
    
@DainisAbols The alternative is to have a question with no answer, and a bunch of comments, which is really irritating on subsequence searches. This is a Q&A site, and I provided an answer. –  Stephen O'Flynn Jun 2 at 15:45
    
Thank you, it's outputting data now. Unfortunately it prints out one row followed by entire array contents; another row, entire array contents, etc. Is there a way to have it so row 1 outputs only the first array item; array two the second, etc.? –  Aaron Turecki Jun 2 at 15:56

Regarding your comment:

Unfortunately it prints out one row followed by entire array contents; another row, entire array contents, etc. Is there a way to have it so row 1 outputs only the first array item; array two the second, etc.?

You can do it like this, but there is something really wrong with your logic.

$k=0;
foreach ($logs as $log) {
    echo date("Y  m-d  ");
    echo "2nd half  ";
    echo $log->client_name . " ";
    if ( isset( $totals[$k] ) )
        echo $totals[$k] , "\n" ;
    $k++;
}   
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.