【问题标题】:codeigniter shopping cart and multiple tablescodeigniter 购物车和多张桌子
【发布时间】:2012-02-20 03:37:31
【问题描述】:

我对 codeigniter 还很陌生,我正在努力更好地了解数据库,所以我决定关注 this guide

但是我想更高级一点,并在具有多个表的多个页面上尝试它,我已经完成了一半的工作,但是当我单击添加到购物车时,它总是从数据库 1 中选择项目。它使用

$this->db->where('id', $id);  
$query = $this->db->get('fruit', 1);  

将商品添加到购物车。但我希望它能够从多个表中进行选择,例如。水果和蔬菜它们都在不同的表中但具有相同的列,无论如何使用连接或任何其他方法可以轻松地做到这一点?

【问题讨论】:

    标签: mysql database codeigniter shopping-cart


    【解决方案1】:

    是的,你调查过codeigniter active record吗?

    Something like?
    $this->db->select('*');
    $this->db->from('fruit');
    $this->db->join('veg', 'fruit.id = veg.id');
    
    $query = $this->db->get();
    
    // Produces:
    // SELECT * FROM fruit
    // JOIN veg ON veg.id = fruit.id
    

    编辑: 根据您在评论中的描述,我构建了这两个表。

       mysql> desc fruit;
       +-------+-------------+------+-----+---------+-------+
       | Field | Type        | Null | Key | Default | Extra |
       +-------+-------------+------+-----+---------+-------+
       | id    | int(11)     | NO   |     | NULL    |       |
       | name  | varchar(29) | NO   |     | NULL    |       |
       | price | int(11)     | YES  |     | NULL    |       |
       +-------+-------------+------+-----+---------+-------+
       3 rows in set (0.00 sec)
    
       mysql> desc veg;
       +-------+-------------+------+-----+---------+-------+
       | Field | Type        | Null | Key | Default | Extra |
       +-------+-------------+------+-----+---------+-------+
       | id    | int(11)     | NO   |     | NULL    |       |
       | name  | varchar(20) | NO   |     | NULL    |       |
       | price | int(11)     | YES  |     | NULL    |       |
       +-------+-------------+------+-----+---------+-------+
       3 rows in set (0.00 sec)
    

    这是一个使用多重和连接的函数测试示例。我想这是你最后的 sql 查询?

    SELECT *
    FROM (`fruit`)
    JOIN `veg` ON `fruit`.`id` = `veg`.`id` and fruit.name = veg.name and fruit.price = veg.price
    WHERE `fruit`.`id` =  1
    

    这是对应的codeigniter查询。

    public function testJoins() {
        $id = 1;
        $this->db->select('*');
        $this->db->from('fruit');
        $this->db->join('veg', 'fruit.id = veg.id and fruit.name = veg.name and fruit.price = veg.price');
        $this->db->where('fruit.id', $id);
    
        $query = $this->db->get();
        return $query->result();
    }
    

    【讨论】:

    • 感谢您的回复。我已经尝试过了,但现在每个 ID 都与 veg 上的商品具有相同的名称和价格,当我使用 'fruit.id = veg.id AND fruit.name = veg.name AND fruit.price = veg.price' 加入更多表格时,它给出的产品不存在。再次感谢您的回复。
    • 告诉我你的最终sql查询是什么,我将使用ci活动记录语法构造查询。
    • $id = $this->input->post('product_id'); $qty = $this->input->post('quantity'); $this->db->select('*'); $this->db->from('fruit'); $this->db->join('veg', 'fruit.id = veg.id AND fruit.name = veg.name AND fruit.price = veg.price'); $query = $this->db->get(); 将删除行$this->db->where('id', $id); // Select where id matches the posted id 有什么关系吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多