dongshubang7816 2018-08-07 10:38
浏览 48
已采纳

PHP / SQL - 交叉引用表,仅选择出现在其他表中的列

I am creating an ecommerce site and am trying to figure out how to get the users to be able to select the appropriate options for a product.

At present I have a products table, this includes a column for the ID's of which attributes apply.

My attributes table then has a 'name' column and columns 'attribute1' - 'attribute15'. So if the name is 'Size' the columns 'attribute1' - 'attribute15' would have values like 'small','medium','large',etc,. with some blank columns depending on how many attributes that product has (max 15).

So then on my backend I have a form for the pricing. So for instance if a product has two attributes (size and colour), it will have each combination in the form. For example: Black - Small, Black - Medium, Black - Large, Blue - Small, Blue - Medium, Blue - Large, etc,.

Each of these combinations has a checkbox and a price input. If I check the checkbox it means that combination is availbale and I store the price value. So my pricing table has a product ID, possibly multiple attribute ID's, and a column 1-15 to say which option for that attribute this price is for.

At the moment my product page first queries my products table to see what attributes apply. It creates a dropdown for each of these but not all of them apply as that combination mightn't be available. For instance we could have a 'small black item' and a 'large black item' but no 'medium black item'. So I'm looking for a way that if they select black in the colour dropdown there is no option for medium in the other dropdown.

This is very complicated to explain what is happening so I hope it makes sense.

Inside a foreach loop I am getting the ID and column with a price from my pricing table, and then, searchng the attributes table to display the dropdown for that attribute like so:

$attribute_id = $attribute[0];
$attribute_column = $attribute[1];
$params = [$attribute_id];
$sql = "SELECT * FROM attributes WHERE id=?";
$stmt = DB::run($sql,$params);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    $attribute_title = $row["name"];
}
$attribute_list .= $attribute_title.'<br />';
$attribute_list .= "<select name='attribute_price' class='attribute_price'>";
$attribute_list .= "<option value='0'>Choose an option...</option>";
$params = [$attribute_id];
$sql = "SELECT * FROM attributes WHERE id=?";
$stmt = DB::run($sql,$params);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
    for($i=1; $i<=15; $i++){
        $column = 'attribute'.$i;
        $product_attribute = $row["$column"];
        if($product_attribute != ""){   
            $attribute_list .= "<option value='";
            $attribute_list .= $attribute_id.'-'.$column;
            $attribute_list .= "'>$product_attribute</option>";
        }
    }   
}
$attribute_list .= "</select>";

At present they can select any of the options from that set of attributes even though it mightn't apply for that product. If they do it just says that the combination isn't available.

Is there a better way to only show the attributes that apply to this product. Any help or suggestions with this would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duancan1732 2019-07-31 09:09
    关注

    As suggested I changed my DB format to include a cross referencing table. This wasn't necessary when it was first set up but as it grew was definitely needed

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable