为 Restfull api 显示来自数据库的 link 图片
Display link image from database for Restfull api
我使用 codeigniter 来管理 restfull api 并使用 Rest Server Library,所有数据都可以显示,但我想知道如何为从数据库加载的图像添加 link 目标?
例如,我有来自 api 的结果:
[
{
"id_news": "1",
"cat_news": "1",
"title": "news title",
"content": "this is content",
"media": "5350_13.jpg"
},
{
"id_news": "2",
"cat_news": "1",
"title": "news title 2",
"content": "this is content 2",
"media": "5350_16.jpg"
}
]
但我想在 "media" 中添加 link,例如 http://domain.com/media/"media_result"
在我的控制器中
function contents_get()
{
$data_news = $this->query->getall();
$this->response($data_news);
}
型号
function getall()
{
$query = $this->db->get("news");
return $query->result();
}
我该怎么做?
谢谢
如果我没记错的话,您想为每个结果添加一个 URL 到您的媒体值。在那种情况下,我会遍历结果,根据需要修改它们,然后 return 它们。尝试这样的事情:
function getall() {
$query = $this->db->get("news");
$result = $query->result();
foreach ($result as $res){
$media = $res->media;
$res->media = "http://something.com/media/$media";
}
return $result;
}
我使用 codeigniter 来管理 restfull api 并使用 Rest Server Library,所有数据都可以显示,但我想知道如何为从数据库加载的图像添加 link 目标?
例如,我有来自 api 的结果:
[
{
"id_news": "1",
"cat_news": "1",
"title": "news title",
"content": "this is content",
"media": "5350_13.jpg"
},
{
"id_news": "2",
"cat_news": "1",
"title": "news title 2",
"content": "this is content 2",
"media": "5350_16.jpg"
}
]
但我想在 "media" 中添加 link,例如 http://domain.com/media/"media_result"
在我的控制器中
function contents_get()
{
$data_news = $this->query->getall();
$this->response($data_news);
}
型号
function getall()
{
$query = $this->db->get("news");
return $query->result();
}
我该怎么做?
谢谢
如果我没记错的话,您想为每个结果添加一个 URL 到您的媒体值。在那种情况下,我会遍历结果,根据需要修改它们,然后 return 它们。尝试这样的事情:
function getall() {
$query = $this->db->get("news");
$result = $query->result();
foreach ($result as $res){
$media = $res->media;
$res->media = "http://something.com/media/$media";
}
return $result;
}