dreamy6301 2014-03-19 12:23
浏览 49
已采纳

Backbone JS + Codeigniter Restful API和MYSQL子串函数无法正常工作

I have this BackboneJS Application where I use Codeigniter as Backend. I use the RESTful API to return data from my MySQL Database to the Frontend. As part of my application, I want to display Music artists Albums and the relevant tracks and the tracks duration. Right now, I get the track duration like 00:03:26 but I want it to display 03:26, so I made this MySQL-query where i use the SUBSTRING-method:

public function artist_tracks_get($artist_id)  
{ 
    $this->load->database();
    $sql = "SELECT products.title AS album_title, products.genre AS album_genre, products.ccws_pro_id AS product_upc, track_title, SUBSTRING(track_duration, 4) AS track_duration FROM products
                INNER JOIN track_albumn_information ON products.ccws_pro_upc = track_albumn_information.product_upc 
                AND track_albumn_information.artist_id =  '".$artist_id."'  LIMIT 0 , 30";
    $query = $this->db->query($sql);
    $data = $query->result();

    if($data) {
        $this->response($data, 200); 
    } else {
        $this->response(array('error' => 'Couldn\'t find any artist albums!'), 404);
    }
}

And inside my Backbone View I have:

serialize: function() {
var grouped = _.groupBy(this.collection.toJSON(), function(item) {
    return item.album_title;
}), spanned = [];
_.each(grouped, function(item) {
    var firstItem = item[0],
        spannedItem = {
            'album_genre': firstItem.album_genre,
            'album_id': firstItem.album_id,
            'album_title': firstItem.album_title,
            'cover_image': firstItem.cover_image,
            'digitalReleaseDate': firstItem.digitalReleaseDate,
            'physicalReleaseDate': firstItem.physicalReleaseDate,
            'pro_id': firstItem.pro_id,
            'product_upc': firstItem.product_upc,
            'tracks': []
        };
    _.each(item, function(albumItem) {
        spannedItem.tracks.push({
            'track_duration': albumItem.track_duration,
            'track_title': albumItem.track_title
        })
    })
    spanned.push(spannedItem);
});

return spanned;
}

When I run this query on my local phpmyadmin, I get the right result like 03:26 but when I test it online on my webserver, I get 00:03:26... What is the issue here? Is it the SQL version? Is BackboneJs unable to handle the SUBSTRING-method?

Please help! Thanks in advance...

  • 写回答

1条回答 默认 最新

  • douxuan0698 2014-03-19 13:34
    关注

    Backbone has no idea what you are doing in MySQL so it's not Backbone having a problem with MySQL SUBSTRING().

    If you wanted to avoid dealing with MYSQL, you could simply do

    albumItem.track_duration.substr(3);
    

    in the view to display it as "03:26".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Markdown在VScode编辑器下插入视频
  • ¥15 stm32c8t6工程,使用hal库
  • ¥100 有偿求易语言word文档取doc和docx页数方法或模块
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡