Home php foreach loop in a string
Reply: 1

php foreach loop in a string

Kostja Wolf
Kostja Wolf Published in 2018-01-12 14:30:58Z

i have a Problem with my foreach:

Parse error: syntax error, unexpected 'foreach' (T_FOREACH)

$pdo = new PDO('mysql:host=localhost;dbname=center24', 'root', '');

$sql = "SELECT `Name`, `SP`, `NP`, `PP` FROM `mitarbeiter punkte`";

$string = '{
"cols": [
    {"id":"","label":"Start Punkte","pattern":"","type":"number"},
    {"id":"","label":"Negative Punkte","pattern":"","type":"number"},
    {"id":"","label":"Positive Punkte","pattern":"","type":"number"}
"rows": [
    '.foreach ($pdo->query($sql) as $row) {.'

echo $string;
Alex Howansky
Alex Howansky Reply to 2018-01-12 14:33:46Z

foreach doesn't return the output generated inside the loop, so you need to build up the string like this:

$string = '...';
foreach ($pdo->query($sql) as $row) {
    $string .= '...';

That said, don't do that, and use json_encode() instead.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322512 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO