简体   繁体   English

Codeigniter 使用for循环将数据从一个表传输到另一个表

[英]Codeigniter transferring data from one table to another using for loop

I am trying to move data from one table to another in my model.我试图在我的 model 中将数据从一个表移动到另一个表。 In the first two lines I am getting all of the rows in my 'cart' table that match the username.在前两行中,我得到了“购物车”表中与用户名匹配的所有行。 I am now trying to iterate through all of these and match the product ID to the product ID in my 'product' table.我现在正在尝试遍历所有这些并将产品 ID 与我的“产品”表中的产品 ID 匹配。 I am then trying to format the data to fit my new table called 'sold'.然后我尝试格式化数据以适合我的名为“已售”的新表。 However I am getting an error.但是我收到一个错误。 I think the syntax of $q->id, $q->product_name etc is wrong.我认为 $q->id、$q->product_name 等的语法是错误的。 I know you can usually use that in the view but it does not work in the model.我知道您通常可以在视图中使用它,但它在 model 中不起作用。 Do you know what the correct syntax would be to do this?你知道这样做的正确语法是什么吗?

 function checkout($username){
    $this->db->where('username', $username);
    $query = $this->db->get('cart');
    //$data = array();

    foreach ($query as $q){
        $product = $this->db->get_where('product', array('id' => $q->id));
        $arr['product_id'] = $product->id;
        $arr['product_brand'] = $product->item_brand;
        $arr['product_name'] = $product->item_name;
        $arr['product_image'] = $product->item_image_url;
        $arr['product_price'] = $product->item_price;
        $arr['product_size'] = $product->item_size;
        $arr['name_of_seller'] = $product->name_of_lister;
        $arr['name_of_buyer'] = $this->session->userdata('username');

        $this->db->insert('sold', $arr);
    }
    //Deletes the items out of the cart
   // $this->db->delete('cart');
}

This is the error message I am getting这是我收到的错误消息

在此处输入图像描述

You have to use result() to get the data in the the $query variable.您必须使用result()来获取$query变量中的数据。

$query = $this->db->get('cart')->result();

In the same way, also change the second query.同样的方法,也改变第二个查询。

$product = $this->db->get_where('product', array('id' => $q->id))->result();

See if this helps you.看看这是否对您有帮助。

function checkout($username){
$this->db->where('username', $username);
$query = $this->db->get('cart');

if ($query->num_rows()>0) {
    $qdata = $query->result_array();

    foreach ($qdata as $key => $qdvalue) {
        $this->db->where('id', $qdvalue['id']);
        $product = $this->db->get('product');
        $pdata = $product->row_array()
        $inarray = array(
            'product_id'        =>      $pdata['id'],
            'product_brand'     =>      $pdata['item_brand'],
            'product_name'      =>      $pdata['item_name'],
            'product_image'     =>      $pdata['item_image_url'],
            'product_price'     =>      $pdata['item_price'],
            'product_size'      =>      $pdata['item_size'],
            'name_of_seller'    =>      $pdata['name_of_lister'],
            'name_of_buyer'     =>      $this->session->userdata('username')
        );

        $this->db->insert('sold', $inarray);
    }//end of foreach
}//end of if query
}//end of checkout function

I rewrite your function if all the column values right then It will work.如果所有列值都正确,我会重写您的 function 它将起作用。 And I will suggest you to use transactions for this type of db events.我会建议你对这种类型的数据库事件使用事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM