简体   繁体   中英

Update table with foreach in CodeIgniter

I have a model with this function

function dummy_system(){
    $this->db->select('SKU, size, color')
             ->from('order_detail')
             ->where('order_id',"52F4CC00C32");

    $query = $this->db->get();

    // 2nd query
    foreach($query->result() as $row):
        $SKU = $row->SKU;
        $size = $row->size;
        $color = $row->color;

        $this->db->select('stock_quantity')
                 ->from('item_stock')
                 ->join('items','items.stock_id = item_stock.stock_id')
                 ->join('item','item.item_id = items.item_id')
                 ->where('item.SKU',$SKU)
                 ->where('item_stock.size',$size)
                 ->where('item_stock.colour',$color);

        $query = $this->db->get();
    endforeach;

    if($query->num_rows() > 0){
    return $query->result();
    }
    else{
        return array();
    }
}

this controller

public function index()
{
    $this->load->model('nekogear');
    $data['dummy'] = $this->nekogear->dummy_system();

    echo "<pre>";
    die(print_r($data, TRUE));
}

and this is the result which only showing the last query.

Array
(
    [dummy] => Array
        (
            [0] => stdClass Object
                (
                    [stock_quantity] => 5
                )

        )

)

my question is how to do the foreach loop in model to get all query result with one order_id? (the current order_id has two rows with same order_id) with this code (which I need the SKU, size & color values into the next queries)

$this->db->select('SKU, size, color')
             ->from('order_detail')
             ->where('order_id',"52F4CC00C32");

    $query = $this->db->get();

    if($query->num_rows() > 0){
        return $query->result();
    }
    else{
        return array();
    }

it can fetch the result

Array
(
    [dummy] => Array
        (
            [0] => stdClass Object
                (
                    [SKU] => Nisekoi-01
                    [size] => L
                    [color] => Biru
                )

            [1] => stdClass Object
                (
                    [SKU] => Wooser-01
                    [size] => XL
                    [color] => Kuning
                )

        )

)

with that in mind, I think the problem lies in the foreach loop inside my model and I don't know how to solve this.

thanks for your time and sorry for my bad english.

You can build an array and loop through it to get the stocks :

$this->db->select('SKU, size, color')
->from('order_detail')
->where('order_id',"52F4CC00C32");

$query = $this->db->get();
$skus = array();
foreach($query->result() as $row):
    $skus[$row->SKU] = array('sku' => $row->SKU, 'size' = $row->size, 'color' = $row->color);
endforeach;

$return = array();

foreach($skus as $sku => $data):
    $SKU = $data['sku'];
    $size = $data['size'];
    $color = $data['color'];

    // same query

    $return[] = $query->result();
endforeach:

return $return;

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