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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置