我在显示指向我的 showdb 的图像 url 链接时遇到问题.php
我目前的数据库记录:
|_id_____|____name_____|_image|
|____1_____|__banana____|香蕉.jpg|
|____2_____|__aple______|苹果.jpg |
我想做的是香蕉.jpg或图像列在记录中显示时如何 www.myhost.com/image/banana.jpg.php?
我现在得到的:
{"posts":[
{
"name":"banana",
"image":banana.jpg}, // FROM THIS
{
"name":"apple",
"image":apple.jpg} // FROM THIS
]}
我想要什么 :
{"posts":[
{
"name":"banana",<br>
"image":www.myhost.com/image/banana.jpg}, // BECOME THIS
{
"name":"apple",<br>
"image":www.myhost.com/image/apple.jpg} // BECOME THIS
]}
这是我当前的代码来显示我的数据库记录:
if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["name"] = $row["name"];
$post["image"] = $row["image"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}
是否可以更改这些代码?
或者如果有任何链接可以做到这一点,我很高兴:))
谢谢,你的回答:)))*如果我的英语不好,对不起:)
====
==================================================================解决者: @KhaledBentoumi
只需添加
'www.yourhost.com/folder/'(dot)$row["image"]; => 'www.yourhost.com'.$row["image"];
但是,它会/
更改为'/
:'www.yourhost.com'/folder'/file.jpg
添加JSON_UNESCAPED_SLASHES
,例如:
$url = 'http://www.example.com/';
echo json_encode($url), "'n";
echo json_encode($url, JSON_UNESCAPED_SLASHES), "'n";
祝大家编码愉快!:))
只需将
www.myhost.com/image/
附加到 post 数组中的图像条目中
if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["name"] = $row["name"];
$post["image"] = 'www.myhost.com/image/'.$row["image"]; // Append here
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}
使用简单的串联:
$post["image"] = "www.myhost.com/image/".$row["image"];