【问题标题】:how to change $arr['news'] into echo如何将 $arr['news'] 更改为 echo
【发布时间】:2023-03-30 11:19:01
【问题描述】:

请帮忙,我正在学习 json,但我发现很难插入代码 echo。如果你知道答案,请帮助我

我的代码

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>
    ";

【问题讨论】:

  • 如果您显然不需要/想要 json 输出,请不要使用 json_encode。您已经在编写您似乎需要的 HTML 输出。只需回显echo $arr['news'];

标签: php arrays json pdo


【解决方案1】:

如果您需要回显$arr['news'] 的内容,只需:

echo $arr['news'];

【讨论】:

  • 不能这样:include("data.php"); echo "
    $id
    $title
    ";
  • 真的很难理解你想做什么
【解决方案2】:
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;
}

这应该为每个循环打印$arr['news']

【讨论】:

  • 或者这个,如何在我的代码中插入:$id2 = $row['news_id']; $title2 = $row['news'];
  • 你想做什么我不明白
  • 我想让 $ 在
    ' 上更容易写。 $ row-> news_id。'
    $title2'. $ 行-> 新闻。
  • 对不起,我没注意 :D
猜你喜欢
  • 2021-11-18
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 2021-01-02
  • 2019-12-19
  • 2022-11-30
  • 1970-01-01
相关资源
最近更新 更多