我正在为一家在线商店建立一个 cms,其中的产品最多可以有 5 种不同的选择,分别是尺寸、颜色、风味、材料和款式。我需要创建代码来为库存控制记录生成字段,它必须为每个可能的选项保留单独的记录,例如红色尺寸 8、蓝色尺寸 8、红色尺寸 9 条纹等。这是我当前的数据库结构:
产品
产品编号
价格、名称和描述等常用值
产品属性
属性 ID
属性组 ID
属性名称
产品库存控制
股票编号
产品编号
属性 1 ID
属性 2 ID
属性 3 ID
属性 4 ID
属性 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);
}
}
}
}
}
}
}