2016-07-04 3 views
-1

데이터베이스의 이미지가 json 페이지와 html 페이지에서 나오지 않지만 이미지 소스 이름이 출력됩니다. html 페이지에 이미지를 표시하려면 어떻게합니까?이미지가 json 페이지에 표시되지 않습니다

감사합니다.

linejson.php 페이지

<?php 
header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8"); 

$conn = new mysqli("localhost", "db_user", "db_pwd", "db_name"); 

$result = $conn->query("SELECT tbl_users.image, tbl_users.firstname, tbl_users.lastname, tbl_users.username, tbl_posts.post, tbl_posts.post_date 
        FROM tbl_posts INNER JOIN tbl_users ON tbl_users.id=tbl_posts.user_id 
        WHERE tbl_posts.user_id=3"); 

$outp = "["; 
while($rs = $result->fetch_array(MYSQLI_ASSOC)) { 
if ($outp != "[") {$outp .= ",";} 

$outp .= '{"Image":"' . $rs["image"] . '",'; 
$outp .= '"Firstname":"' . $rs["firstname"]  . '",'; 
$outp .= '"Lastname":"' . $rs["lastname"] . '",'; 
$outp .= '"Username":"' . $rs["username"]  . '",'; 
$outp .= '"Post":"' . $rs["post"]  . '"}'; 

} 
$outp .="]"; 

$conn->close(); 

echo($outp); 
?> 

linejson.html 페이지

<!DOCTYPE html> 
<html> 

<head> 
<style> 

table, th , td { 
border-top: 1px solid purple; 
border-collapse: collapse; 
padding: 15px; 

} 
table tr:nth-child(odd) { 
background-color: #f0f1f1; 

} 
table tr:nth-child(even) { 
background-color: #ffffff; 
} 
</style> 
</head> 

<body> 
<div id="timeline"></div> 

<script> 
var xmlhttp = new XMLHttpRequest(); 
var url = "http://www.outpaceng.com/linejson.php"; 

xmlhttp.onreadystatechange=function() { 
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
    myFunction(xmlhttp.responseText); 
    } 
    } 
xmlhttp.open("GET", url, true); 
xmlhttp.send(); 

function myFunction(response) { 
var arr = JSON.parse(response); 
var i; 
var out = "<table>"; 

for(i = 0; i < arr.length; i++) { 
    out += "<tr><td>" + 
    arr[i].Image + 
    "</td><td>" + 
    arr[i].Firstname + 
    "&nbsp;" + 
    arr[i].Lastname + 
    "&nbsp;" + 
    arr[i].Username + 
    "<br>" + 
    arr[i].Post + 
    "</td><tr>" ; 

    } 
    out += "</table>"; 
    document.getElementById("timeline").innerHTML = out; 
} 
</script> 

</body> 
</html> 
+1

당신은 downvote에 대해 유감스럽게도 img 태그 – Saty

+1

을 만들 [로 json_encode()을 (http://php.net/manual/en/function.json-encode.php)를 사용하지 왜

for(i = 0; i < arr.length; i++) { out += "<tr><td><img src='" + arr[i].image + "' alt="img"/></td><td>" + arr[i].firstname + "&nbsp;" + arr[i].lastname + "&nbsp;" + arr[i].username + "<br>" + arr[i].post + "</td><tr>" ; } 
Robert

답변

1

코드는 sipmlified 할 수 있으며 JSON에게

변경이

$outp = "["; 
while($rs = $result->fetch_array(MYSQLI_ASSOC)) { 
if ($outp != "[") {$outp .= ",";} 

$outp .= '{"Image":"' . $rs["image"] . '",'; 
$outp .= '"Firstname":"' . $rs["firstname"]  . '",'; 
$outp .= '"Lastname":"' . $rs["lastname"] . '",'; 
$outp .= '"Username":"' . $rs["username"]  . '",'; 
$outp .= '"Post":"' . $rs["post"]  . '"}'; 

} 
$outp .="]"; 

$conn->close(); 

echo($outp); 
를 만드는로 json_encode를 사용한다

echo json_encode($result->fetch_all()); 
$conn->close(); 

및 자바 스크립트

JSON
+0

을 사용해야 만한다. 그러나이 배열이나'json_encode ($ rs)'가'$ rs = interator_to_array ($ rs))'$ rs' 중개자 인 경우. – Naumov

+0

나는 그것을 사용, 무슨 뜻인지 이해가 안 돼 – Robert

+0

죄송합니다, 전 upvote 해요. – Naumov

관련 문제