【发布时间】:2018-03-26 17:10:27
【问题描述】:
我正在创建一个销售到多个国家/地区的电子商务网站。
由于运费等原因,我们必须在不同国家为同一产品收取不同的价格,
我们对不同的产品也有不同的属性(如尺寸、颜色等)。
我正在使用PHP 来生成这样的产品定价表单。
我的PHP如下:
$attribute = $_GET['attribute'];
$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'";
if($attribute == 1){ $price_form .= " checked";}
$price_form .= ">$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.' size="10">';
$price_form .= '<input type="hidden" value='.$country_id.' name='.$priceCurrency.'_'.$country_id.'>';
$price_form .= '<input type="hidden" value='.$i.' name='.$priceAttribute.'_'.$country_id.'>';
$price_form .= '</td>';
}
}
$price_form .= '</tr>';
}
}
$price_form .= "</table>";
$price_form .= "<br />";
$price_form .= "<button id='add_prices' name='add_prices' onClick='return false'>Continue</button>";
这取决于产品的属性,在这种情况下,颜色是这样的:
<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" size="10"><input type="hidden" value="1" name="priceCurrency1_1"><input type="hidden" value="1" name="priceAttribute1_1"></td><td><input type="text" placeholder="£" name="priceValue1_2" size="10"><input type="hidden" value="2" name="priceCurrency1_2"><input type="hidden" value="1" name="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" size="10"><input type="hidden" value="1" name="priceCurrency2_1"><input type="hidden" value="2" name="priceAttribute2_1"></td><td><input type="text" placeholder="£" name="priceValue2_2" size="10"><input type="hidden" value="2" name="priceCurrency2_2"><input type="hidden" value="2" name="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" size="10"><input type="hidden" value="1" name="priceCurrency3_1"><input type="hidden" value="3" name="priceAttribute3_1"></td><td><input type="text" placeholder="£" name="priceValue3_2" size="10"><input type="hidden" value="2" name="priceCurrency3_2"><input type="hidden" value="3" name="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" size="10"><input type="hidden" value="1" name="priceCurrency4_1"><input type="hidden" value="4" name="priceAttribute4_1"></td><td><input type="text" placeholder="£" name="priceValue4_2" size="10"><input type="hidden" value="2" name="priceCurrency4_2"><input type="hidden" value="4" name="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" size="10"><input type="hidden" value="1" name="priceCurrency5_1"><input type="hidden" value="5" name="priceAttribute5_1"></td><td><input type="text" placeholder="£" name="priceValue5_2" size="10"><input type="hidden" value="2" name="priceCurrency5_2"><input type="hidden" value="5" name="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" size="10"><input type="hidden" value="1" name="priceCurrency6_1"><input type="hidden" value="6" name="priceAttribute6_1"></td><td><input type="text" placeholder="£" name="priceValue6_2" size="10"><input type="hidden" value="2" name="priceCurrency6_2"><input type="hidden" value="6" name="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" size="10"><input type="hidden" value="1" name="priceCurrency7_1"><input type="hidden" value="7" name="priceAttribute7_1"></td><td><input type="text" placeholder="£" name="priceValue7_2" size="10"><input type="hidden" value="2" name="priceCurrency7_2"><input type="hidden" value="7" name="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" size="10"><input type="hidden" value="1" name="priceCurrency8_1"><input type="hidden" value="8" name="priceAttribute8_1"></td><td><input type="text" placeholder="£" name="priceValue8_2" size="10"><input type="hidden" value="2" name="priceCurrency8_2"><input type="hidden" value="8" name="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" size="10"><input type="hidden" value="1" name="priceCurrency9_1"><input type="hidden" value="9" name="priceAttribute9_1"></td><td><input type="text" placeholder="£" name="priceValue9_2" size="10"><input type="hidden" value="2" name="priceCurrency9_2"><input type="hidden" value="9" name="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" size="10"><input type="hidden" value="1" name="priceCurrency10_1"><input type="hidden" value="10" name="priceAttribute10_1"></td><td><input type="text" placeholder="£" name="priceValue10_2" size="10"><input type="hidden" value="2" name="priceCurrency10_2"><input type="hidden" value="10" name="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" size="10"><input type="hidden" value="1" name="priceCurrency11_1"><input type="hidden" value="11" name="priceAttribute11_1"></td><td><input type="text" placeholder="£" name="priceValue11_2" size="10"><input type="hidden" value="2" name="priceCurrency11_2"><input type="hidden" value="11" name="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" size="10"><input type="hidden" value="1" name="priceCurrency12_1"><input type="hidden" value="12" name="priceAttribute12_1"></td><td><input type="text" placeholder="£" name="priceValue12_2" size="10"><input type="hidden" value="2" name="priceCurrency12_2"><input type="hidden" value="12" name="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" size="10"><input type="hidden" value="1" name="priceCurrency13_1"><input type="hidden" value="13" name="priceAttribute13_1"></td><td><input type="text" placeholder="£" name="priceValue13_2" size="10"><input type="hidden" value="2" name="priceCurrency13_2"><input type="hidden" value="13" name="priceAttribute13_2"></td></tr></tbody></table>
我一直在通过 PHP 对其进行解析,但整个添加产品的部分变得非常复杂,而不仅仅是定价。 (正在提交不同的部分,将其保存在 $_SESSION 中,以便在提交其他部分时存储它,然后在每个部分完成时提交所有 $_SESSION 变量。
但是我已经切换到AJAX,所以页面不会一直刷新,我不需要继续存储$_SESSION 变量。
我的PHP如下:
for ($i = 0; $i <= 15; $i++) {
$checkbox = "checkbox".$i;
$priceValue = "priceValue".$i;
$priceCurrency = "priceCurrency".$i;
$priceAttribute = "priceAttribute".$i;
for ($j = 1; $j <= $countryCount; $j++) {
$priceValue1 = $priceValue.'_'.$j;
$priceCurrency1 = $priceCurrency.'_'.$j;
$priceAttribute1 = $priceAttribute.'_'.$j;
if(isset($_SESSION[''.$priceValue1.'']) && isset($_SESSION[''.$priceCurrency1.'']) && isset($_SESSION[''.$priceAttribute1.''])){
$value = $_SESSION[''.$priceValue1.''];
$currency = $_SESSION[''.$priceCurrency1.''];
$attribute = $_SESSION[''.$priceAttribute1.''];
$params = [$id,$currency,$attribute,$value];
$sql = "INSERT INTO prices (product, country, attribute, value) VALUES(?,?,?,?)";
$stmt = DB::run($sql,$params);
}
}
}
但是,当我想用 AJAX 解析这个时,我不确定如何在不知道来自 PHP 的 $countryCount 变量的情况下循环所有这些以确定存储了多少个国家(我们希望这个保持活力,以便我们可以扩展到更多国家/地区)。
我尝试过在循环中弄乱循环(一个循环用于属性,一个循环用于国家),但我无处可去,必须有一种更好的方法来做到这一点,而不必硬编码以获得价值每个国家/地区的每种价格。
如果有人可以就如何解决这个问题给我任何建议,将不胜感激。
//--------------------------------编辑--------\
到目前为止,我一直在尝试将它们全部推送到这样的多维数组中:
var pricing = [];
for (i = 0; i <15; i++) {
var checkbox = "checkbox"+i;
var priceValue = "priceValue"+i;
var priceCurrency = "priceCurrency"+i;
var priceAttribute = "priceAttribute"+i;
if ($("#"+checkbox).is(':checked')) {
for (j = 1; j <=2; j++) {
var priceValue1 = priceValue+'_'+j;
var priceCurrency1 = priceCurrency+'_'+j;
var priceAttribute1 = priceAttribute+'_'+j;
var value = $("#"+priceValue1).val();
var currency = $("#"+priceCurrency1).val();
var attribute = $("#"+priceAttribute1).val();
var item = [];
item.push(value);
item.push(currency);
item.push(attribute);
pricing.push(i);
}
}
}
但是,这是错误的并且给了我错误。
它也不够动态,在第二个 for() 循环中,我暂时只输入了“2”代表国家的数量。
【问题讨论】:
-
注意,上面sn-p中每个有输入的表格单元格也有两个隐藏字段。所以每个单元格有 3 个输入。一个用于引用属性 ID,一个用于国家/地区 ID,以及您看到的价格的实际输入。