0

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>'));
2
  • Its really unclear. You need to post your array or json Commented May 29, 2015 at 11:48
  • create s string at first, afterwards do the json_encode. Commented May 29, 2015 at 11:48

2 Answers 2

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>'));
Sign up to request clarification or add additional context in comments.

Comments

0

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.' ... ',
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.