public function getSpecInput($goods_id, $spec_arr)
{
// <input name="item[2_4_7][price]" value="100" /><input name="item[2_4_7][name]" value="藍色_S_長袖" />
// $goods_id = 18;
// $spec_arr = array(
// 20 => array('7','8','9'),
// 10=>array('1','2'),
// 1 => array('3','4'),
// );
// 排序
foreach ($spec_arr as $k => $v)
{
$spec_arr_sort[$k] = count($v);
}
asort($spec_arr_sort);
foreach ($spec_arr_sort as $key =>$val)
{
$spec_arr2[$key] = $spec_arr[$key];
}
$clo_name = array_keys($spec_arr2);
$spec_arr2 = $this->combineDika($spec_arr2,3); // 擷取 規格的 笛卡爾積
$spec = $this->asArray(DB::table('spec')->select(['id','spec_name'])->get()); // 規格表id,name
$spec = $this->array_change($spec);
$specItem = $this->asArray(DB::table('spec_item')->select(['id','spec_id','item'])->get());//規格項id,item,spec_id
$specItem = $this->array_change($specItem);
$keySpecGoodsPrice = $this->asArray(DB::table('products')->select(['product_sn','price','sku','key_name','key','member_price'])->where('goods_id',$goods_id)->get());//規格項
$str = "<table class='table table-bordered' id='spec_input_tab'>";
$str .="<tr>";
// 顯示第一行的資料
foreach ($clo_name as $k => $v)
{
$str .=" <td><b>{$spec[$v]['spec_name']}</b></td>";
}
$str .="<td><b>價格</b></td>
<td><b>會員價格</b></td>
<td><b>團購價格</b></td>
<td><b>庫存</b></td>
</tr>";
// 顯示第二行開始
foreach ($spec_arr2 as $k => $v)
{
$str .="<tr>";
$item_key_name = array();
foreach($v as $k2 => $v2)
{
$str .="<td>{$specItem[$v2]['item']}</td>";
$item_key_name[$v2] = $spec[$specItem[$v2]['spec_id']]['spec_name'].':'.$specItem[$v2]['item'];
}
ksort($item_key_name);
$item_key = implode('_', array_keys($item_key_name));
$item_name = implode(' ', $item_key_name);
$product_sn = 'no_'.time().rand(11111,99999);
$str .="<input type='hidden' name='item[$item_key][product_sn]' value='$product_sn'>";
$str .="<td><input name='item[$item_key][price]' οnkeyup='this.value=this.value.replace(/[^\d.]/g,\"\")' οnpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' /></td>";
$str .="<td><input name='item[$item_key][member_price]' οnkeyup='this.value=this.value.replace(/[^\d.]/g,\"\")' οnpaste='this.value=this.value.replace(/[^\d.]/g,\"\")'/></td>";
$str .="<td><input name='item[$item_key][group_price]' οnkeyup='this.value=this.value.replace(/[^\d.]/g,\"\")' οnpaste='this.value=this.value.replace(/[^\d.]/g,\"\")'/></td>";
$str .="<td><input name='item[$item_key][sku]' />
<input type='hidden' name='item[$item_key][key_name]' value='$item_name' /></td>";
$str .="</tr>";
}
$str .= "</table>";
return $str;
}
public function array_change($arr,$id = 'id')
{
$newArr = [];
foreach($arr as $k => $v){
$newArr[$v[$id]] = $v;
}
return $newArr;
}
function combineDika() {
$data = func_get_args();
$data = current($data);
$cnt = count($data);
$result = array();
$arr1 = array_shift($data);
foreach($arr1 as $key=>$item)
{
$result[] = array($item);
}
foreach($data as $key=>$item)
{
$result = $this->combineArray($result,$item);
}
return $result;
}
function combineArray($arr1,$arr2) {
$result = array();
foreach ($arr1 as $item1)
{
foreach ($arr2 as $item2)
{
$temp = $item1;
$temp[] = $item2;
$result[] = $temp;
}
}
return $result;
}
最終的效果圖如下: