我发送带有AJAX JSON的预先格式化的HTMl, JSON有以下代码,
我正在尝试从数据库中提取数据并回显数组数据, 我无法在json_encode中放置foreach循环,因为看起来我的代码在foreach循环中是错误的,
我如何实现这一目标?
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>'));
答案 0 :(得分:1)
$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>'));
答案 1 :(得分:0)
首先准备字符串。随你所有的循环。
然后将其放入数组并将其发送到json_encode()
。得到结果。
$str = '';
foreach($news_tag_array as $tag){
$str .= '<a href="javascript:;">';
}
echo json_encode(array(
'returnnews' => '<div ...'.$latestnews.'</div ... '.$str.' ... ',
));