php - BackboneJS + Codeigniter 将 API 结果转换为字符串

标签 php mysql codeigniter backbone.js

我有一个 Backbone 应用程序,我在其中使用 Codeigniters Restful API。我有这个功能,可以从 MySQL 数据库获取Artistnames。现在,我的 API 给出的结果如下:

{"artist_name" : "Queens of the stone age"}

我想要实现的是我得到像 /queensofthestoneage 这样的名称。我怎样才能实现这一目标?

我的主干 View 如下所示:

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;
}

我的 Codeigniter/API MySQL 查询如下所示:

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() 来删除任何空格。带有正则表达式的空格。然后使用 strtolower() 将数据转换为小写,然后再将数据转换为 JSON:

echo strtolower(preg_replace("/\s+/", '', "Queens of the Stone Age"));
echo strtolower(str_replace(" ", '', "Queens of the Stone Age"));

如果您可以保证 MySQL 的数据中全部出现空格(例如没有回车符),那么 str_replace() 就是最佳选择,因为它更快。否则,我可能会使用正则表达式方法,因为它将删除所有空格。

关于php - BackboneJS + Codeigniter 将 API 结果转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22459934/

相关文章:

php - 大型 SQL 结果集占用太多内存 - CodeIgniter

php - 爆炸功能不起作用

php - 带有连接和计数的 mySQL 查询

php - MySQL + PHP 查询日期导致问题

PHP OOP 插入数据

php - 如何使用两个日期选择器从我的数据库获取数据。代码点火器

php - 在一个查询中组合两个不同的过滤器 - Elastica

php - Yii:根据下拉列表选择更改输入文本字段值

php - 在 PHP 中创建 P 值

php - 从 PHP 访问 apache errordocument 指令