Php BackboneJS+;Codeigniter将API结果转换为字符串
我有一个主干应用程序,我使用Php BackboneJS+;Codeigniter将API结果转换为字符串,php,mysql,codeigniter,backbone.js,Php,Mysql,Codeigniter,Backbone.js,我有一个主干应用程序,我使用Codeignitersrestfulapi。我有这个功能,可以从MySQL数据库中获取艺人姓名。现在,我的API给出如下结果: {"artist_name" : "Queens of the stone age"} function (App, Backbone) { var Artistname = App.module(); Artistname.View = Backbone.View.extend({ template:
Codeigniters
restfulapi。我有这个功能,可以从MySQL数据库中获取艺人姓名。现在,我的API给出如下结果:
{"artist_name" : "Queens of the stone age"}
function (App, Backbone) {
var Artistname = App.module();
Artistname.View = Backbone.View.extend({
template: 'artistname',
initialize: function() {
this.listenTo(this.collection, 'all', this.render)
},
serialize: function() {
return this.collection ? this.collection.toJSON() : [];
}
});
Artistname.ArtistnameCollection = Backbone.Collection.extend({
url: function() {
return '/project/ci/index.php/api/artistchannel/artistname/' + this.artist_id;
}
});
return Artistname;
}
public function a_artists_get()
{
$this->load->database();
$sql = "SELECT formated_name FROM `artists` WHERE formated_name LIKE 'A%'";
$query = $this->db->query($sql);
$data = $query->result();
if($data) {
$this->response($data, 200);
} else {
$this->response(array('error' => 'Couldn\'t find any artists with letter a!'), 404);
}
}
我想要实现的是,我得到了一个像/queensofstoneage
这样的名字。我怎样才能做到这一点
我的主干视图如下所示:
{"artist_name" : "Queens of the stone age"}
function (App, Backbone) {
var Artistname = App.module();
Artistname.View = Backbone.View.extend({
template: 'artistname',
initialize: function() {
this.listenTo(this.collection, 'all', this.render)
},
serialize: function() {
return this.collection ? this.collection.toJSON() : [];
}
});
Artistname.ArtistnameCollection = Backbone.Collection.extend({
url: function() {
return '/project/ci/index.php/api/artistchannel/artistname/' + this.artist_id;
}
});
return Artistname;
}
public function a_artists_get()
{
$this->load->database();
$sql = "SELECT formated_name FROM `artists` WHERE formated_name LIKE 'A%'";
$query = $this->db->query($sql);
$data = $query->result();
if($data) {
$this->response($data, 200);
} else {
$this->response(array('error' => 'Couldn\'t find any artists with letter a!'), 404);
}
}
我的Codeigniter/API MySQL查询如下所示:
{"artist_name" : "Queens of the stone age"}
function (App, Backbone) {
var Artistname = App.module();
Artistname.View = Backbone.View.extend({
template: 'artistname',
initialize: function() {
this.listenTo(this.collection, 'all', this.render)
},
serialize: function() {
return this.collection ? this.collection.toJSON() : [];
}
});
Artistname.ArtistnameCollection = Backbone.Collection.extend({
url: function() {
return '/project/ci/index.php/api/artistchannel/artistname/' + this.artist_id;
}
});
return Artistname;
}
public function a_artists_get()
{
$this->load->database();
$sql = "SELECT formated_name FROM `artists` WHERE formated_name LIKE 'A%'";
$query = $this->db->query($sql);
$data = $query->result();
if($data) {
$this->response($data, 200);
} else {
$this->response(array('error' => 'Couldn\'t find any artists with letter a!'), 404);
}
}
欢迎任何帮助
提前感谢…假设您正在谈论服务器端,在PHP中,您可以使用str_replace()
去除空格,或者使用preg_replace()
去除带有正则表达式的空白。然后在将数据转换为JSON之前,使用strtolower()
转换为小写:
echo strtolower(preg_replace("/\s+/", '', "Queens of the Stone Age"));
echo strtolower(str_replace(" ", '', "Queens of the Stone Age"));
如果您可以保证MySQL中的数据中只显示空格(例如没有回车符),那么str_replace()
是一种更快的方法。否则,我可能会使用regex方法,因为它将删除所有空白。您是说在客户端还是服务器端?啊,好的!我更新了我的问题,所以我可以把你的建议包括进去吗?是的$data
将是一个关联数组,因此您需要输入类似于$data['artist_name']=strtolower(preg_replace(“/\s+/”,'',$data['artist_name'])的内容代码>在查询之后和IF语句之前。按照您的示例,它会显示未定义索引:artist_name
,这是因为该键在关联数组中不存在。您需要使用数据库中该字段的任何名称。表的字段实际上是格式化的\u name
,但我收到相同的错误消息未定义索引:格式化的\u name