I'm trying to wrap the results from a query into HTML elements the json_encode it, but only one record is being returned even though the query returns several records. Below is the format I'm returning from json_encode. I want all the records be wrapped inside the div tag only but the h2 and the last a be the same for all:
My Results
Load more?Actually getting only one record.
$items = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$items = "<h2>My Results</h2>
<div class=\"search__results--display ">
<a href=\"/account/\"><img src=\"$image\"/></a>
</div>
<a href=\"/search/more/\" class=\"search__results--more\">Load more?</a>"
}
echo json_encode(array("success" => true, "html" => $items));
It's obvious I'm doing somthing wrong here.
Need advice and help.
source https://stackoverflow.com/questions/68550416/loop-through-records-from-data-base-in-php
Comments
Post a Comment