Javascript Json foreach循环正在输出word对象而不是其内容

Javascript Json foreach循环正在输出word对象而不是其内容,javascript,php,arrays,json,ajax,Javascript,Php,Arrays,Json,Ajax,我试图通过json发送foreach循环,然后用innerHTML显示。没有任何错误。我遇到的问题是输出是显示: [对象] 而不是foreach循环中的内容 如何使输出成为foreach循环中的$html行 try { $sql_recentProjects = " SELECT * FROM project_gallery ORDER BY date_added ASC LIMIT 5 "; if ($re

我试图通过json发送
foreach
循环,然后用
innerHTML
显示。没有任何错误。我遇到的问题是输出是显示:

[对象]

而不是foreach循环中的内容

如何使输出成为
foreach
循环中的
$html

try {
    $sql_recentProjects = "
        SELECT *
        FROM project_gallery
        ORDER BY date_added ASC
        LIMIT 5
    ";
    if ($recentProjects_stmt = $con->prepare($sql_recentProjects)) {
        $recentProjects_stmt->execute();
        $recentProjects_rows = $recentProjects_stmt->fetchAll(PDO::FETCH_ASSOC);
        $recProj_arr = array();
        foreach ($recentProjects_rows as $recentProjects_row) {
            $precProjName = $recentProjects_row['p_name'];
            $recProjImg = $recentProjects_row['p_img'];
            //$project_img = substr($project_img, 2);
            $displayRecProjImg = '<img src="/php'.$recProjImg.'" alt="'. $precProjAlt .'" class="projectDisplayImg">';
            $html = '';
            $html .= '<div class="recentProjectCont">';
            $html .= '<div class="recentProjectImg">';
            $html .= $displayRecProjImg;
            $html .= '</div>';
            $html .= '<div class="recProjInfoCont">';
            $html .= '<div class="">';
            $html .= $precProjName;
            $html .= '</div>';
            $html .= '</div>';
            $html .= '</div>';
            $recentProjData = array('html' => $html);
            //$proj_arr[] = $data;
        }
    }
}
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
echo json_encode(['recentProjectData' => $recentProjData]);
(阅读OP的评论,了解我发布此答案的原因)

你有两个选择。。。最简单但更具限制性的方法是:

try {
    $sql_recentProjects = "
        SELECT *
        FROM project_gallery
        ORDER BY date_added ASC
        LIMIT 5
    ";
    if ($recentProjects_stmt = $con->prepare($sql_recentProjects)) {
        $recentProjects_stmt->execute();
        $recentProjects_rows = $recentProjects_stmt->fetchAll(PDO::FETCH_ASSOC);
        $recProj_arr = array();
        $html = '';  // MOVE THIS OUTSIDE THE LOOP
        foreach ($recentProjects_rows as $recentProjects_row) {
            $precProjName = $recentProjects_row['p_name'];
            $recProjImg = $recentProjects_row['p_img'];
            //$project_img = substr($project_img, 2);
            $displayRecProjImg = '<img src="/php'.$recProjImg.'" alt="'. $precProjAlt .'" class="projectDisplayImg">';
            $html .= '<div class="recentProjectCont">';
            $html .= '<div class="recentProjectImg">';
            $html .= $displayRecProjImg;
            $html .= '</div>';
            $html .= '<div class="recProjInfoCont">';
            $html .= '<div class="">';
            $html .= $precProjName;
            $html .= '</div>';
            $html .= '</div>';
            $html .= '</div>';
            //$proj_arr[] = $data;
        }
        // SET THIS AFTER YOU GET ALL ROWS INTO THE STRING
        $recentProjData = array('html' => $html);
    }
}
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
echo json_encode(['recentProjectData' => $recentProjData]);
试试看{
$sql\u最近的项目=”
挑选*
来自project_画廊
按日期订购\u添加ASC
限制5
";
if($recentProjects\u stmt=$con->prepare($sql\u recentProjects)){
$recentProjects\u stmt->execute();
$recentProjects\u rows=$recentProjects\u stmt->fetchAll(PDO::FETCH\u ASSOC);
$recProj_arr=array();
$html='';//将其移出循环
foreach($recentProjects\u行作为$recentProjects\u行){
$precProjName=$recentProjects_行['p_name'];
$recProjImg=$recentProjects_行['p_img'];
//$project\u img=substr($project\u img,2);
$displayRecProjImg='';
$html.='';
$html.='';
$html.=$displayRecProjImg;
$html.='';
$html.='';
$html.='';
$html.=$precProjName;
$html.='';
$html.='';
$html.='';
//$proj_arr[]=$data;
}
//在将所有行放入字符串后设置此选项
$recentProjData=array('html'=>$html);
}
}
捕获(PDO$e){
echo“连接失败:”..e->getMessage();
}
echo json_encode(['recentProjectData'=>$recentProjData]);
或者,您可以这样做,这将真正将行分隔为更多可消费的数据,但这意味着您需要重新编码JS以正确输出(通过JS循环数据):

试试看{
$sql\u最近的项目=”
挑选*
来自project_画廊
按日期订购\u添加ASC
限制5
";
if($recentProjects\u stmt=$con->prepare($sql\u recentProjects)){
$recentProjects\u stmt->execute();
$recentProjects\u rows=$recentProjects\u stmt->fetchAll(PDO::FETCH\u ASSOC);
$recProj_arr=array('html'=>array());//设置数组
foreach($recentProjects\u行作为$recentProjects\u行){
$precProjName=$recentProjects_行['p_name'];
$recProjImg=$recentProjects_行['p_img'];
//$project\u img=substr($project\u img,2);
$displayRecProjImg='';
$html='';
$html.='';
$html.='';
$html.=$displayRecProjImg;
$html.='';
$html.='';
$html.='';
$html.=$precProjName;
$html.='';
$html.='';
$html.='';
$recentProjData['html'][]=$html;//添加到数组中,而不是覆盖它
//$proj_arr[]=$data;
}
}
}
捕获(PDO$e){
echo“连接失败:”..e->getMessage();
}
echo json_encode(['recentProjectData'=>$recentProjData]);

这是因为您有
$recentProjData=array('html'=>$html)
。因此,
recentProjectData
是一个带有
html
元素的对象,为什么不发送html字符串而不是尝试以json的形式发送呢?如果你去掉了注释,看看你接收的数据是如何构造的://console.log(data)<代码>文档.getElementById('recentProjectWrap')。innerHTML=recentProjectData.html或
document.getElementById('recentProjectWrap')。innerHTML=recentProjectData['html']将其作为答案发布…谢谢!为什么通过JS循环的方法会更好?通过JS循环的方法会更好,因为它可以让您更好地控制如何显示行,因为每行的代码都被分割成一个数组,而不是一个长字符串。比如说,出于某种原因,您只需要在一页中显示两行,但在另一页中显示所有5行。。。或者你需要重新排序,因为你的老板告诉你。你可以通过JS循环来控制它。否则,您将需要在字符串中进行更复杂的搜索/替换。如果您想获得真正的技术性,您正在使用一种RESTful API,因此,根据您希望实现的可重用程度,最好将数据与标记分离。实际上,您应该只返回所有数据的对象。这样,您可以在不同的页面/网站上使用数据,但可以更改其外观,因为您可以为每个用例独立创建标记。通过添加标记,您将强制任何人使用您指定的布局和类。如果只是为了你,那也没什么大不了的,但是却破坏了RESTful API的优点。那么,通过为JS方法添加数组,我该怎么做呢?我定义每个循环行是
$html
@Paul我不确定我是否理解你的问题。“定义每个循环行是$html”是什么意思?
try {
    $sql_recentProjects = "
        SELECT *
        FROM project_gallery
        ORDER BY date_added ASC
        LIMIT 5
    ";
    if ($recentProjects_stmt = $con->prepare($sql_recentProjects)) {
        $recentProjects_stmt->execute();
        $recentProjects_rows = $recentProjects_stmt->fetchAll(PDO::FETCH_ASSOC);
        $recProj_arr = array();
        $html = '';  // MOVE THIS OUTSIDE THE LOOP
        foreach ($recentProjects_rows as $recentProjects_row) {
            $precProjName = $recentProjects_row['p_name'];
            $recProjImg = $recentProjects_row['p_img'];
            //$project_img = substr($project_img, 2);
            $displayRecProjImg = '<img src="/php'.$recProjImg.'" alt="'. $precProjAlt .'" class="projectDisplayImg">';
            $html .= '<div class="recentProjectCont">';
            $html .= '<div class="recentProjectImg">';
            $html .= $displayRecProjImg;
            $html .= '</div>';
            $html .= '<div class="recProjInfoCont">';
            $html .= '<div class="">';
            $html .= $precProjName;
            $html .= '</div>';
            $html .= '</div>';
            $html .= '</div>';
            //$proj_arr[] = $data;
        }
        // SET THIS AFTER YOU GET ALL ROWS INTO THE STRING
        $recentProjData = array('html' => $html);
    }
}
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
echo json_encode(['recentProjectData' => $recentProjData]);
try {
    $sql_recentProjects = "
        SELECT *
        FROM project_gallery
        ORDER BY date_added ASC
        LIMIT 5
    ";
    if ($recentProjects_stmt = $con->prepare($sql_recentProjects)) {
        $recentProjects_stmt->execute();
        $recentProjects_rows = $recentProjects_stmt->fetchAll(PDO::FETCH_ASSOC);
        $recProj_arr = array( 'html' => array() );  // SET UP THE ARRAY
        foreach ($recentProjects_rows as $recentProjects_row) {
            $precProjName = $recentProjects_row['p_name'];
            $recProjImg = $recentProjects_row['p_img'];
            //$project_img = substr($project_img, 2);
            $displayRecProjImg = '<img src="/php'.$recProjImg.'" alt="'. $precProjAlt .'" class="projectDisplayImg">';
            $html = '';
            $html .= '<div class="recentProjectCont">';
            $html .= '<div class="recentProjectImg">';
            $html .= $displayRecProjImg;
            $html .= '</div>';
            $html .= '<div class="recProjInfoCont">';
            $html .= '<div class="">';
            $html .= $precProjName;
            $html .= '</div>';
            $html .= '</div>';
            $html .= '</div>';
            $recentProjData['html'][] = $html; //ADD TO THE ARRAY, INSTEAD OF OVERWRITING IT
            //$proj_arr[] = $data;
        }
    }
}
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
echo json_encode(['recentProjectData' => $recentProjData]);