如何将$ arr ['news']更改为echo

时间:2018-07-23 14:56:43

标签: php arrays json pdo

请帮助,我正在学习json,但是我发现很难插入代码回显。如果您知道答案,请帮助我

我的代码

if ($result) {
    $arr['news'] = '';
    $idterakhir = '';
    $query = $pdo->query($sql);
    while ($row = $query->fetch(PDO::FETCH_OBJ)) {
        // change this into echo
        $arr['news'] .= '<div class="card" news_id="'.$row->news_id.'">'.$row->news_id.'<br>'.$row->news.'</div>';
        // change this into echo
        $idterakhir = $row->news_id;
    }
    $arr['idterakhir'] = $idterakhir;
    $arr['end'] = false;
} else {
    $arr['end'] = true;
}
echo json_encode($arr);

进入:

echo "
     <div class='card' news_id='$id'> $id  <br> $title  </div>
    ";

2 个答案:

答案 0 :(得分:1)

<div class="header_wrapper">
  <div class="header_section">
    <div class="header_bg banner_bg mob_header_bg"></div>
    <div class="container">
      <div class="header_box_wrapper">
        <div class="header_box">
          <h2 id="header_text_round">Lorem ipsum dolor sit amet</h2>
        </div>
      </div>
    </div>
  </div>
</div>




<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://circletype.labwire.ca/dist/circletype.min.js"></script>

这应在每个圆圈中打印while ($row = $query->fetch(PDO::FETCH_OBJ)) { $id2 = $row['news_id']; $id = <div class="card" news_id="'.$id2.'"> $news = $row['news']; $title = $id.$id2.<br>'.$news.'</div> $arr['news'] .= title; echo $arr['news']; $idterakhir = $row->news_id; }

答案 1 :(得分:0)

如果您需要回显$arr['news']的内容,请执行以下操作:

echo $arr['news'];