如何使用foreach获取我的ids产品?

时间:2017-07-05 23:39:24

标签: php codeigniter-3

我希望有效的一个销售处理,但我的最后一个股票等于x股票然后它将被添加到通知表,但我怎么能得到我的产品ID只有ids foreach,之后发送他们哈希到我的get_product

获取购物车的功能

 $this->session->carrito = $this->sale->checar_existe_carrito();
 $array  = $this->sale->get_all_cart($this->session->carrito);

JSON

[{"id":"4165","qty":"1.00","price":"77.00","name":"hammer"},{"id":"4168","qty":"3.00","price":"7.00","name":"whey protein"},{"id":"4169","qty":"3.00","price":"500.00","name":"papas"}]

有效的功能然后库存产品等于x stock

public function concretar_venta(){
        if($this->sale->checa_carrito_vacio($this->session->carrito)){
            $total = $this->input->post("total", TRUE);
            $cantidad_pagada = $this->input->post("cantidad_pagada", TRUE);
            $cambio = $cantidad_pagada - $total;
            $producto = $this->products->get_product('861578d797aeb0634f77aff3f488cca2');
            if($this->sale->concretar_venta($this->session->carrito, $total, $cantidad_pagada, $cambio)){
                if($producto->stock <= 2){
                   $this->notification->addNotification('low stock', $product_id[$output], $user_id[$output_u], 'low stock',now());
                }else{
                    echo 'bien';
                }
            }
            else{
                echo "Ocurrio un error al concretar la venta, por favor intentelo de nuevo";
            }
        }
        else{
            "El carrito está vacío";
        }
    }

检索功能测试

  public function FunctionName() {
    $json = '[{"id":"4165","qty":"1.00","price":"77.00","name":"hammer"},{"id":"4168","qty":"3.00","price":"7.00","name":"whey protein"},{"id":"4169","qty":"3.00","price":"500.00","name":"papas"}]';

    $jsonArray = json_decode($json, true);

    $ids = array();

    foreach ($jsonArray as $value) {
      $ids[] = md5($value['id']);
    }
      print_r($ids);
      $producto = $this->products->get_product($ids);
    }

why_is_sending_array_as_a_column

1 个答案:

答案 0 :(得分:0)

此解决方案基于您提供的JSON

$json = '[{"id":"4165","qty":"1.00","price":"77.00","name":"hammer"},{"id":"4168","qty":"3.00","price":"7.00","name":"whey protein"},{"id":"4169","qty":"3.00","price":"500.00","name":"papas"}]';

$jsonArray = json_decode($json, true);
// echo '<pre>'; print_r($jsonArray); die; // check what is coming in array

$ids = array();
// extract ids using loop
foreach ($jsonArray as $value) {
  $ids[] = md5($value['id']);
}
// print_r($ids); die;

foreach ($ids as $id) {
  $producto = $this->products->get_product($id);
}

您可以在这里体验Link