生成具有不同选项的产品库存水平

时间:2012-07-16 11:38:27

标签: php mysql codeigniter attributes content-management-system

我正在为网上商店建立一个cms,产品可以有多达5种不同的选择,包括尺寸,颜色,味道,材料和款式。我需要创建代码来为库存控制记录生成字段,这些字段必须为每个可能的选项保留单独的记录,例如红色大小8,蓝色大小8,红色大小9条纹等。这是我当前的数据库结构:< / p>

  

产品

     

产品ID

     

价格,名称和描述等常用价值

     

产品属性

     

属性ID

     

属性组ID

     

属性名称

     

产品库存控制

     

股票ID

     

产品ID

     

Attibute 1 ID

     

Attibute 2 ID

     

Attibute 3 ID

     

Attibute 4 ID

     

Attibute 5 ID

     

库存水平

使用codeigniter和php,对此进行编码的最佳方法是什么?

修改

这是我目前正在尝试的代码,但肯定有比1233行代码更好的方法吗?

public function genStockRecords($id)
{

    $this->db->select("ps_name");
    $query = $this->db->get("product_selects");
    $selectList = $query->result();

    foreach($selectList as $select)
    {

        $selectName = $select->ps_name;
        echo $selectName." - ";
        $$selectName = array();

    }
    unset($selectList);

    foreach($_SESSION['options'] as $option)
    {

        $option = explode("|", $option);
        ${$option[0]}[] = $option[1];

    }

    if(!empty($Size))
    {

        foreach($Size as $sizeValue)
        {

            $data['psc_opt_1'] = $sizeValue;

            if(!empty($Colour))
            {

                foreach($Colour as $colourValue)
                {

                    $data['psc_opt_2'] = $colourValue;

                    if(!empty($Flavour))
                    {

                        foreach($Flavour as $flavourValue)
                        {

                            $data['psc_opt_3'] = $flavourValue;

                            if(!empty($Material))
                            {

                                foreach($Material as $materialValue)
                                {

                                    $data['psc_opt_4'] = $materialValue;

                                    if(!empty($Style))
                                    {

                                        foreach($style as $styleValue)
                                        {

                                            $data['psc_opt_5'] = $styleValue;

                                            $this->db->select("psc_id");
                                            $this->db->where("psc_prod_id", $id);

                                            foreach($data as $field => $value)
                                            {

                                                $this->db->where($field, $value);

                                            }

                                            $query = $this->db->get("product_stock_control", 1);

                                            if($query->num_rows == 0)
                                            {

                                                $data["psc_prod_id"] = $id;
                                                $this->db->insert("product_stock_control", $data);

                                            }

                                        }

                                    }
                                    else
                                    {

                                        $data['psc_opt_5'] = null;

                                        $this->db->select("psc_id");
                                        $this->db->where("psc_prod_id", $id);

                                        foreach($data as $field => $value)
                                        {

                                            $this->db->where($field, $value);

                                        }

                                        $query = $this->db->get("product_stock_control", 1);

                                        if($query->num_rows == 0)
                                        {

                                            $data["psc_prod_id"] = $id;
                                            $this->db->insert("product_stock_control", $data);

                                        }                                               

                                    }


                                }

                            }
                            else
                            {

                                $data['psc_opt_4'] = null;

                                if(!empty($Style))
                                {

                                    foreach($style as $styleValue)
                                    {

                                        $data['psc_opt_5'] = $styleValue;

                                        $this->db->select("psc_id");
                                        $this->db->where("psc_prod_id", $id);

                                        foreach($data as $field => $value)
                                        {

                                            $this->db->where($field, $value);

                                        }

                                        $query = $this->db->get("product_stock_control", 1);

                                        if($query->num_rows == 0)
                                        {

                                            $data["psc_prod_id"] = $id;
                                            $this->db->insert("product_stock_control", $data);

                                        }

                                    }

                                }
                                else
                                {

                                    $data['psc_opt_5'] = null;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }                                               

                                }                                   

                            }

                        }

                    }
                    else
                    {

                        $data['psc_opt_3'] = null;

                        if(!empty($Material))
                        {

                            foreach($Material as $materialValue)
                            {

                                $data['psc_opt_4'] = $materialValue;

                                if(!empty($Style))
                                {

                                    foreach($style as $styleValue)
                                    {

                                        $data['psc_opt_5'] = $styleValue;

                                        $this->db->select("psc_id");
                                        $this->db->where("psc_prod_id", $id);

                                        foreach($data as $field => $value)
                                        {

                                            $this->db->where($field, $value);

                                        }

                                        $query = $this->db->get("product_stock_control", 1);

                                        if($query->num_rows == 0)
                                        {

                                            $data["psc_prod_id"] = $id;
                                            $this->db->insert("product_stock_control", $data);

                                        }

                                    }

                                }
                                else
                                {

                                    $data['psc_opt_5'] = null;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }                                               

                                }


                            }

                        }
                        else
                        {

                            $data['psc_opt_4'] = null;

                            if(!empty($Style))
                            {

                                foreach($style as $styleValue)
                                {

                                    $data['psc_opt_5'] = $styleValue;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }

                                }

                            }
                            else
                            {

                                $data['psc_opt_5'] = null;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }                                               

                            }                                   

                        }

                    }

                }

            }
            else
            {

                $data['psc_opt_2'] = null;

                if(!empty($Flavour))
                {

                    foreach($Flavour as $flavourValue)
                    {

                        $data['psc_opt_3'] = $flavourValue;

                        if(!empty($Material))
                        {

                            foreach($Material as $materialValue)
                            {

                                $data['psc_opt_4'] = $materialValue;

                                if(!empty($Style))
                                {

                                    foreach($style as $styleValue)
                                    {

                                        $data['psc_opt_5'] = $styleValue;

                                        $this->db->select("psc_id");
                                        $this->db->where("psc_prod_id", $id);

                                        foreach($data as $field => $value)
                                        {

                                            $this->db->where($field, $value);

                                        }

                                        $query = $this->db->get("product_stock_control", 1);

                                        if($query->num_rows == 0)
                                        {

                                            $data["psc_prod_id"] = $id;
                                            $this->db->insert("product_stock_control", $data);

                                        }

                                    }

                                }
                                else
                                {

                                    $data['psc_opt_5'] = null;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }                                               

                                }


                            }

                        }
                        else
                        {

                            $data['psc_opt_4'] = null;

                            if(!empty($Style))
                            {

                                foreach($style as $styleValue)
                                {

                                    $data['psc_opt_5'] = $styleValue;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }

                                }

                            }
                            else
                            {

                                $data['psc_opt_5'] = null;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }                                               

                            }                                   

                        }

                    }

                }
                else
                {

                    $data['psc_opt_3'] = null;

                    if(!empty($Material))
                    {

                        foreach($Material as $materialValue)
                        {

                            $data['psc_opt_4'] = $materialValue;

                            if(!empty($Style))
                            {

                                foreach($style as $styleValue)
                                {

                                    $data['psc_opt_5'] = $styleValue;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }

                                }

                            }
                            else
                            {

                                $data['psc_opt_5'] = null;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }                                               

                            }


                        }

                    }
                    else
                    {

                        $data['psc_opt_4'] = null;

                        if(!empty($Style))
                        {

                            foreach($style as $styleValue)
                            {

                                $data['psc_opt_5'] = $styleValue;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }

                            }

                        }
                        else
                        {

                            $data['psc_opt_5'] = null;

                            $this->db->select("psc_id");
                            $this->db->where("psc_prod_id", $id);

                            foreach($data as $field => $value)
                            {

                                $this->db->where($field, $value);

                            }

                            $query = $this->db->get("product_stock_control", 1);

                            if($query->num_rows == 0)
                            {

                                $data["psc_prod_id"] = $id;
                                $this->db->insert("product_stock_control", $data);

                            }                                               

                        }                                   

                    }

                }



            }

        }

    }
    else
    {

        $data['psc_opt_1'] = null;

        if(!empty($Colour))
        {

            foreach($Colour as $colourValue)
            {

                $data['psc_opt_2'] = $colourValue;

                if(!empty($Flavour))
                {

                    foreach($Flavour as $flavourValue)
                    {

                        $data['psc_opt_3'] = $flavourValue;

                        if(!empty($Material))
                        {

                            foreach($Material as $materialValue)
                            {

                                $data['psc_opt_4'] = $materialValue;

                                if(!empty($Style))
                                {

                                    foreach($style as $styleValue)
                                    {

                                        $data['psc_opt_5'] = $styleValue;

                                        $this->db->select("psc_id");
                                        $this->db->where("psc_prod_id", $id);

                                        foreach($data as $field => $value)
                                        {

                                            $this->db->where($field, $value);

                                        }

                                        $query = $this->db->get("product_stock_control", 1);

                                        if($query->num_rows == 0)
                                        {

                                            $data["psc_prod_id"] = $id;
                                            $this->db->insert("product_stock_control", $data);

                                        }

                                    }

                                }
                                else
                                {

                                    $data['psc_opt_5'] = null;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }                                               

                                }


                            }

                        }
                        else
                        {

                            $data['psc_opt_4'] = null;

                            if(!empty($Style))
                            {

                                foreach($style as $styleValue)
                                {

                                    $data['psc_opt_5'] = $styleValue;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }

                                }

                            }
                            else
                            {

                                $data['psc_opt_5'] = null;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }                                               

                            }                                   

                        }

                    }

                }
                else
                {

                    $data['psc_opt_3'] = null;

                    if(!empty($Material))
                    {

                        foreach($Material as $materialValue)
                        {

                            $data['psc_opt_4'] = $materialValue;

                            if(!empty($Style))
                            {

                                foreach($style as $styleValue)
                                {

                                    $data['psc_opt_5'] = $styleValue;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }

                                }

                            }
                            else
                            {

                                $data['psc_opt_5'] = null;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }                                               

                            }


                        }

                    }
                    else
                    {

                        $data['psc_opt_4'] = null;

                        if(!empty($Style))
                        {

                            foreach($style as $styleValue)
                            {

                                $data['psc_opt_5'] = $styleValue;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }

                            }

                        }
                        else
                        {

                            $data['psc_opt_5'] = null;

                            $this->db->select("psc_id");
                            $this->db->where("psc_prod_id", $id);

                            foreach($data as $field => $value)
                            {

                                $this->db->where($field, $value);

                            }

                            $query = $this->db->get("product_stock_control", 1);

                            if($query->num_rows == 0)
                            {

                                $data["psc_prod_id"] = $id;
                                $this->db->insert("product_stock_control", $data);

                            }                                               

                        }                                   

                    }

                }

            }

        }
        else
        {

            $data['psc_opt_2'] = null;

            if(!empty($Flavour))
            {

                foreach($Flavour as $flavourValue)
                {

                    $data['psc_opt_3'] = $flavourValue;

                    if(!empty($Material))
                    {

                        foreach($Material as $materialValue)
                        {

                            $data['psc_opt_4'] = $materialValue;

                            if(!empty($Style))
                            {

                                foreach($style as $styleValue)
                                {

                                    $data['psc_opt_5'] = $styleValue;

                                    $this->db->select("psc_id");
                                    $this->db->where("psc_prod_id", $id);

                                    foreach($data as $field => $value)
                                    {

                                        $this->db->where($field, $value);

                                    }

                                    $query = $this->db->get("product_stock_control", 1);

                                    if($query->num_rows == 0)
                                    {

                                        $data["psc_prod_id"] = $id;
                                        $this->db->insert("product_stock_control", $data);

                                    }

                                }

                            }
                            else
                            {

                                $data['psc_opt_5'] = null;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }                                               

                            }


                        }

                    }
                    else
                    {

                        $data['psc_opt_4'] = null;

                        if(!empty($Style))
                        {

                            foreach($style as $styleValue)
                            {

                                $data['psc_opt_5'] = $styleValue;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }

                            }

                        }
                        else
                        {

                            $data['psc_opt_5'] = null;

                            $this->db->select("psc_id");
                            $this->db->where("psc_prod_id", $id);

                            foreach($data as $field => $value)
                            {

                                $this->db->where($field, $value);

                            }

                            $query = $this->db->get("product_stock_control", 1);

                            if($query->num_rows == 0)
                            {

                                $data["psc_prod_id"] = $id;
                                $this->db->insert("product_stock_control", $data);

                            }                                               

                        }                                   

                    }

                }

            }
            else
            {

                $data['psc_opt_3'] = null;

                if(!empty($Material))
                {

                    foreach($Material as $materialValue)
                    {

                        $data['psc_opt_4'] = $materialValue;

                        if(!empty($Style))
                        {

                            foreach($style as $styleValue)
                            {

                                $data['psc_opt_5'] = $styleValue;

                                $this->db->select("psc_id");
                                $this->db->where("psc_prod_id", $id);

                                foreach($data as $field => $value)
                                {

                                    $this->db->where($field, $value);

                                }

                                $query = $this->db->get("product_stock_control", 1);

                                if($query->num_rows == 0)
                                {

                                    $data["psc_prod_id"] = $id;
                                    $this->db->insert("product_stock_control", $data);

                                }

                            }

                        }
                        else
                        {

                            $data['psc_opt_5'] = null;

                            $this->db->select("psc_id");
                            $this->db->where("psc_prod_id", $id);

                            foreach($data as $field => $value)
                            {

                                $this->db->where($field, $value);

                            }

                            $query = $this->db->get("product_stock_control", 1);

                            if($query->num_rows == 0)
                            {

                                $data["psc_prod_id"] = $id;
                                $this->db->insert("product_stock_control", $data);

                            }                                               

                        }


                    }

                }
                else
                {

                    $data['psc_opt_4'] = null;

                    if(!empty($Style))
                    {

                        foreach($style as $styleValue)
                        {

                            $data['psc_opt_5'] = $styleValue;

                            $this->db->select("psc_id");
                            $this->db->where("psc_prod_id", $id);

                            foreach($data as $field => $value)
                            {

                                $this->db->where($field, $value);

                            }

                            $query = $this->db->get("product_stock_control", 1);

                            if($query->num_rows == 0)
                            {

                                $data["psc_prod_id"] = $id;
                                $this->db->insert("product_stock_control", $data);

                            }

                        }

                    }
                    else
                    {

                        $data['psc_opt_5'] = null;

                        $this->db->select("psc_id");
                        $this->db->where("psc_prod_id", $id);

                        foreach($data as $field => $value)
                        {

                            $this->db->where($field, $value);

                        }

                        $query = $this->db->get("product_stock_control", 1);

                        if($query->num_rows == 0)
                        {

                            $data["psc_prod_id"] = $id;
                            $this->db->insert("product_stock_control", $data);

                        }                                               

                    }                                   

                }

            }   

        }   

    }

}

1 个答案:

答案 0 :(得分:1)

这个问题更通用。您可以使用formigniter.comthephpcode.com等网站生成CRUD代码,然后从那里开始..