I’m making this function where a part of an array should be read, and each value holds a number with which I want to perform a PDO query. This is my following code:
function get_topics($array) {
$top = 20; $base = 0;
foreach ($array as $key => $value) {
$getData = $dbc->prepare('SELECT * FROM topics WHERE id = :id LIMIT 1');
$getData->execute(array(':id' => $value));
while($row = $getData->fetch()) {
$potential_topic_img = 'members/topic_' . $value . '.jpg';
if (file_exists($potential_topic_img)) { $topic_img = $potential_topic_img; } else {
$topic_img = 'members/0.jpg'; }
$name = $row['name'];
echo '<div class="topic_div"><img src="' . $topic_img . '" width="80"><br /><span
style="font-size:10pt;">' . $name . '</span></div>';
} if (++$base == $top) break;
}
}
echo get_topics($some_array);
But all I get is an error telling this: “Parse error: syntax error, unexpected T_VARIABLE in /home/……“, and it says that the problem is on this line:
$getData->execute(array(':id' => $value));
What can I be doing wrong?
EDIT
I deleted some code and the code is running fine when this is remaining:
function get_topics($array) {
foreach ($array as $key => $value) {
echo $value;
}
}
echo get_topics($user_likes_array);
So it’s not that $value is empty, the problem seems to be in the line I mentioned in the beginning, since when I move everything below that line, the error message does not change, but it does change when I move that specific line.
foreach ($array as $key => $value)assigns the current element’s key to the$valuevariable on each iteration.Try