0

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

}
3
  • What does var_dump($logs) give you? Commented Jun 2, 2014 at 14:34
  • Check your penultimate line.. ` $totals . "\n" ;` Commented Jun 2, 2014 at 14:36
  • The array has nothing to do with the query, FYI. Commented Jun 2, 2014 at 14:40

2 Answers 2

2

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";
Sign up to request clarification or add additional context in comments.

3 Comments

Although, this will help, it's more as a comment, not an actual answer.
@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.
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.?
0

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++;
}   

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.