Reputation: 1349
I am sending preformatted HTMl with AJAX JSON, JSON have below code,
I am trying pull data array from DB and echoing array data, I am not able to put foreach loop in json_encode, because seems my code is wrong at foreach loop,
How can i achieve that?
echo json_encode(array('returnnews' => '<div class="news-item-page">
<h3 class="text-info" style="margin-top:0">'.$latestnews->news_subject.'</h3>
'.$latestnews->news_content.'
</div>
<div class="row">
<div class="col-md-6">
<ul class="list-inline blog-tags">
<li>
<i class="fa fa-tags"></i>'.
foreach($news_tag_array as $tag){
<a href="javascript:;">
echo $tag </a>
}
</li>
</ul>
</div>
</div>'));
Upvotes: 0
Views: 1681
Reputation: 799
$tags = '';
foreach($news_tag_array as $tag){
$tags .= '<a href="javascript:;">'.$tag.' </a>';
}
echo json_encode(array('returnnews' => '<div class="news-item-page">
<h3 class="text-info" style="margin-top:0">'.$latestnews->news_subject.'</h3>
'.$latestnews->news_content.'
</div>
<div class="row">
<div class="col-md-6">
<ul class="list-inline blog-tags">
<li>
<i class="fa fa-tags"></i>'.$tags.'</li>
</ul>
</div>
</div>'));
Upvotes: 1
Reputation: 1521
Prepare the string first. With all loops which you want.
Then put it into array and send it into json_encode()
. Get the result.
$str = '';
foreach($news_tag_array as $tag){
$str .= '<a href="javascript:;">';
}
echo json_encode(array(
'returnnews' => '<div ...'.$latestnews.'</div ... '.$str.' ... ',
));
Upvotes: 0