使用DB数据预先填充多维表单 - PHP / SQL

时间:2018-04-05 08:39:53

标签: php sql forms

我正在使用PHP创建一个电子商务网站。它被设置为每个产品可以具有多个属性(大小,颜色等),并且这些不同的选项可以具有不同的价格。我们也在多个国家销售,由于运费,每个国家的价格也不同。我设置了一个表单来收集这些信息:

$params = [$attribute];
$sql = "SELECT * FROM attributes WHERE id=?";
$stmt = DB::run($sql,$params);
$attributeCount = $stmt->rowCount();
if ($attributeCount > 0) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $attribute_name = $row["name"];
    }
}
$price_form .= "<table class='table'>";
$price_form .= "<thead>";
$price_form .= "<td>$attribute_name</td>";
$sql = "SELECT * FROM countries";
$stmt = DB::run($sql);
$countryCount = $stmt->rowCount();
if ($countryCount > 0) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $country_name = $row["country"];
        $price_form .= "<td>$country_name</td>";
    }
}
$price_form .= "</thead>";
$params = [$attribute];
$sql = "SELECT * FROM attributes WHERE id=?";
$attributeResult = DB::run($sql,$params);
foreach ($attributeResult as $value) {
    for ($i = 1; $i <= 15; $i++) {
        $attributeColumn = "attribute".$i;
        $checkbox = "checkbox".$i;
        $priceValue = "priceValue".$i;
        $priceCurrency = "priceCurrency".$i;
        $priceAttribute = "priceAttribute".$i;
        if($value[$attributeColumn] != ""){
            $price_form .= "<tr><td><input type='checkbox' name='$checkbox' id='$checkbox' value='$checkbox'>$value[$attributeColumn]</td>";
            $sql = "SELECT * FROM countries";
            $countryResult = DB::run($sql);
            while ($row = $countryResult->fetch(PDO::FETCH_ASSOC)){
                $country_id = $row["id"];
                $country_currency = $row["currency"];
                $price_form .= '<td>';
                $price_form .= '<input type="text" placeholder='.$country_currency.' name='.$priceValue.'_'.$country_id.' id='.$priceValue.'_'.$country_id.' size="10">';
                $price_form .= '<input type="hidden" value='.$country_id.' name='.$priceCurrency.'_'.$country_id.' id='.$priceCurrency.'_'.$country_id.'>';
                $price_form .= '<input type="hidden" value='.$i.' name='.$priceAttribute.'_'.$country_id.' id='.$priceAttribute.'_'.$country_id.'>';
                $price_form .= '</td>';
            }
        }
        $price_form .= '</tr>';
    }
}
$price_form .= "</table>";

所以目前只有两个国家,但每个产品最多可以有15个属性,因此我们可以得到这样的表格:

<div id="price_form_div">
<table class="table"><thead><tr><td>Colour</td><td>Ireland</td><td>United Kingdom</td></tr></thead><tbody><tr><td><input type="checkbox" name="checkbox1" id="checkbox1" value="checkbox1">Black</td><td><input type="text" placeholder="€" name="priceValue1_1" id="priceValue1_1" size="10"><input type="hidden" value="1" name="priceCurrency1_1" id="priceCurrency1_1"><input type="hidden" value="1" name="priceAttribute1_1" id="priceAttribute1_1"></td><td><input type="text" placeholder="£" name="priceValue1_2" id="priceValue1_2" size="10"><input type="hidden" value="2" name="priceCurrency1_2" id="priceCurrency1_2"><input type="hidden" value="1" name="priceAttribute1_2" id="priceAttribute1_2"></td></tr><tr><td><input type="checkbox" name="checkbox2" id="checkbox2" value="checkbox2">White</td><td><input type="text" placeholder="€" name="priceValue2_1" id="priceValue2_1" size="10"><input type="hidden" value="1" name="priceCurrency2_1" id="priceCurrency2_1"><input type="hidden" value="2" name="priceAttribute2_1" id="priceAttribute2_1"></td><td><input type="text" placeholder="£" name="priceValue2_2" id="priceValue2_2" size="10"><input type="hidden" value="2" name="priceCurrency2_2" id="priceCurrency2_2"><input type="hidden" value="2" name="priceAttribute2_2" id="priceAttribute2_2"></td></tr><tr><td><input type="checkbox" name="checkbox3" id="checkbox3" value="checkbox3">Purple</td><td><input type="text" placeholder="€" name="priceValue3_1" id="priceValue3_1" size="10"><input type="hidden" value="1" name="priceCurrency3_1" id="priceCurrency3_1"><input type="hidden" value="3" name="priceAttribute3_1" id="priceAttribute3_1"></td><td><input type="text" placeholder="£" name="priceValue3_2" id="priceValue3_2" size="10"><input type="hidden" value="2" name="priceCurrency3_2" id="priceCurrency3_2"><input type="hidden" value="3" name="priceAttribute3_2" id="priceAttribute3_2"></td></tr><tr><td><input type="checkbox" name="checkbox4" id="checkbox4" value="checkbox4">Yellow</td><td><input type="text" placeholder="€" name="priceValue4_1" id="priceValue4_1" size="10"><input type="hidden" value="1" name="priceCurrency4_1" id="priceCurrency4_1"><input type="hidden" value="4" name="priceAttribute4_1" id="priceAttribute4_1"></td><td><input type="text" placeholder="£" name="priceValue4_2" id="priceValue4_2" size="10"><input type="hidden" value="2" name="priceCurrency4_2" id="priceCurrency4_2"><input type="hidden" value="4" name="priceAttribute4_2" id="priceAttribute4_2"></td></tr><tr><td><input type="checkbox" name="checkbox5" id="checkbox5" value="checkbox5">Red</td><td><input type="text" placeholder="€" name="priceValue5_1" id="priceValue5_1" size="10"><input type="hidden" value="1" name="priceCurrency5_1" id="priceCurrency5_1"><input type="hidden" value="5" name="priceAttribute5_1" id="priceAttribute5_1"></td><td><input type="text" placeholder="£" name="priceValue5_2" id="priceValue5_2" size="10"><input type="hidden" value="2" name="priceCurrency5_2" id="priceCurrency5_2"><input type="hidden" value="5" name="priceAttribute5_2" id="priceAttribute5_2"></td></tr><tr><td><input type="checkbox" name="checkbox6" id="checkbox6" value="checkbox6">Orange</td><td><input type="text" placeholder="€" name="priceValue6_1" id="priceValue6_1" size="10"><input type="hidden" value="1" name="priceCurrency6_1" id="priceCurrency6_1"><input type="hidden" value="6" name="priceAttribute6_1" id="priceAttribute6_1"></td><td><input type="text" placeholder="£" name="priceValue6_2" id="priceValue6_2" size="10"><input type="hidden" value="2" name="priceCurrency6_2" id="priceCurrency6_2"><input type="hidden" value="6" name="priceAttribute6_2" id="priceAttribute6_2"></td></tr><tr><td><input type="checkbox" name="checkbox7" id="checkbox7" value="checkbox7">Blue</td><td><input type="text" placeholder="€" name="priceValue7_1" id="priceValue7_1" size="10"><input type="hidden" value="1" name="priceCurrency7_1" id="priceCurrency7_1"><input type="hidden" value="7" name="priceAttribute7_1" id="priceAttribute7_1"></td><td><input type="text" placeholder="£" name="priceValue7_2" id="priceValue7_2" size="10"><input type="hidden" value="2" name="priceCurrency7_2" id="priceCurrency7_2"><input type="hidden" value="7" name="priceAttribute7_2" id="priceAttribute7_2"></td></tr><tr><td><input type="checkbox" name="checkbox8" id="checkbox8" value="checkbox8">Green</td><td><input type="text" placeholder="€" name="priceValue8_1" id="priceValue8_1" size="10"><input type="hidden" value="1" name="priceCurrency8_1" id="priceCurrency8_1"><input type="hidden" value="8" name="priceAttribute8_1" id="priceAttribute8_1"></td><td><input type="text" placeholder="£" name="priceValue8_2" id="priceValue8_2" size="10"><input type="hidden" value="2" name="priceCurrency8_2" id="priceCurrency8_2"><input type="hidden" value="8" name="priceAttribute8_2" id="priceAttribute8_2"></td></tr><tr><td><input type="checkbox" name="checkbox9" id="checkbox9" value="checkbox9">Pink</td><td><input type="text" placeholder="€" name="priceValue9_1" id="priceValue9_1" size="10"><input type="hidden" value="1" name="priceCurrency9_1" id="priceCurrency9_1"><input type="hidden" value="9" name="priceAttribute9_1" id="priceAttribute9_1"></td><td><input type="text" placeholder="£" name="priceValue9_2" id="priceValue9_2" size="10"><input type="hidden" value="2" name="priceCurrency9_2" id="priceCurrency9_2"><input type="hidden" value="9" name="priceAttribute9_2" id="priceAttribute9_2"></td></tr><tr><td><input type="checkbox" name="checkbox10" id="checkbox10" value="checkbox10">Grey</td><td><input type="text" placeholder="€" name="priceValue10_1" id="priceValue10_1" size="10"><input type="hidden" value="1" name="priceCurrency10_1" id="priceCurrency10_1"><input type="hidden" value="10" name="priceAttribute10_1" id="priceAttribute10_1"></td><td><input type="text" placeholder="£" name="priceValue10_2" id="priceValue10_2" size="10"><input type="hidden" value="2" name="priceCurrency10_2" id="priceCurrency10_2"><input type="hidden" value="10" name="priceAttribute10_2" id="priceAttribute10_2"></td></tr><tr><td><input type="checkbox" name="checkbox11" id="checkbox11" value="checkbox11">Brown</td><td><input type="text" placeholder="€" name="priceValue11_1" id="priceValue11_1" size="10"><input type="hidden" value="1" name="priceCurrency11_1" id="priceCurrency11_1"><input type="hidden" value="11" name="priceAttribute11_1" id="priceAttribute11_1"></td><td><input type="text" placeholder="£" name="priceValue11_2" id="priceValue11_2" size="10"><input type="hidden" value="2" name="priceCurrency11_2" id="priceCurrency11_2"><input type="hidden" value="11" name="priceAttribute11_2" id="priceAttribute11_2"></td></tr><tr><td><input type="checkbox" name="checkbox12" id="checkbox12" value="checkbox12">Spearmint</td><td><input type="text" placeholder="€" name="priceValue12_1" id="priceValue12_1" size="10"><input type="hidden" value="1" name="priceCurrency12_1" id="priceCurrency12_1"><input type="hidden" value="12" name="priceAttribute12_1" id="priceAttribute12_1"></td><td><input type="text" placeholder="£" name="priceValue12_2" id="priceValue12_2" size="10"><input type="hidden" value="2" name="priceCurrency12_2" id="priceCurrency12_2"><input type="hidden" value="12" name="priceAttribute12_2" id="priceAttribute12_2"></td></tr><tr><td><input type="checkbox" name="checkbox13" id="checkbox13" value="checkbox13">Lime Green</td><td><input type="text" placeholder="€" name="priceValue13_1" id="priceValue13_1" size="10"><input type="hidden" value="1" name="priceCurrency13_1" id="priceCurrency13_1"><input type="hidden" value="13" name="priceAttribute13_1" id="priceAttribute13_1"></td><td><input type="text" placeholder="£" name="priceValue13_2" id="priceValue13_2" size="10"><input type="hidden" value="2" name="priceCurrency13_2" id="priceCurrency13_2"><input type="hidden" value="13" name="priceAttribute13_2" id="priceAttribute13_2"></td></tr></tbody></table><br></div>

请注意,每个单元格都有两个隐藏的输入,用于国家/地区和属性值。此外,有些产品只使用某些属性(有些东西可能只有两种颜色),因此每个集合旁边都有一个复选框,因此只有被检查的属性才会上传到数据库。

所以我最终得到了这样的数据库结构,有3个产品(1,2,3),它们都只有一个属性(1),但它们在两个国家(1,2)出售,价格为每个:

id  product country attribute   value
6       3       1       1       562
5       3       2       1       443
4       2       1       1       416
3       2       2       1       318
2       1       1       1       196
1       1       2       1       142

我的问题是编辑产品。我希望能够使用此表中的数据重新填写此表单。我希望选择相应的复选框并填充值字段。

我尝试了一些事情,但我对此感到沮丧。我设法让它选中了这个复选框:

$sql = "SELECT * FROM prices WHERE product=?";
$stmt = DB::run($sql,$params);
$priceCount = $stmt->rowCount();
if ($priceCount > 0) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $country = $row["country"];
        $price_attribute = $row["attribute"];
        $value = $row["value"];
    }
}
$price_form .= "<tr><td><input type='checkbox' name='$checkbox' id='$checkbox' value='$checkbox'";
if($attribute == $i){ $price_form .= " checked";}
$price_form .= ">$value[$attributeColumn]</td>";

非常感谢任何有关此的帮助或建议。

0 个答案:

没有答案