简体   繁体   中英

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. 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. 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. I know you can usually use that in the view but it does not work in the 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.

$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. And I will suggest you to use transactions for this type of db events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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