【发布时间】:2018-03-12 14:38:24
【问题描述】:
我正在尝试使用 php 在我的页面上设置一个表单,并循环通过数据库查询结果来生成它。
稍微理解一下,这是针对在不同国家/地区以不同价格销售具有不同属性的产品的电子商务网站。例如,我们可以拥有像 T 恤这样的产品。这可以有多达 15 种不同的属性(在本例中为颜色)。不同的颜色有不同的价格,我们需要根据我们销售的国家/地区调整价格(取决于运费等)。对于此示例,假设我们只向两个国家/地区销售产品(尽管这可能会增加)。
所以我正在寻找的是这样的:
<table>
<tr><td>Colour 1</td><td>Price country 1</td><td>Price country 2</td></tr>
<tr><td>Colour 2</td><td>Price country 1</td><td>Price country 2</td></tr>
<tr><td>Colour 3</td><td>Price country 1</td><td>Price country 2</td></tr>
</table>
我的php如下:
$attributeResult = DB::run("SELECT * FROM attributes WHERE id='$new_product_attribute_id'");
$countryResult = DB::run("SELECT * FROM countries");
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>';
foreach ($countryResult as $countryVal) {
$price_form .= '<td>';
$price_form .= '<input type="text" placeholder='.$countryVal['currency'].' name='.$priceValue.'_'.$countryVal['id'].' size="10">';
$price_form .= '<input type="hidden" value='.$countryVal['id'].' name='.$priceCurrency.'_'.$countryVal['id'].'>';
$price_form .= '<input type="hidden" value='.$i.' name='.$priceAttribute.'_'.$countryVal['id'].'>';
$price_form .= '</td>';
}
$price_form .= '</tr>';
}
}
}
这是屏幕上的输出:
<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>
</tr>
<tr>
<td><input type="checkbox" name="checkbox3" id="checkbox3" value="checkbox3">Purple</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox4" id="checkbox4" value="checkbox4">Yellow</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox5" id="checkbox5" value="checkbox5">Red</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox6" id="checkbox6" value="checkbox6">Orange</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox7" id="checkbox7" value="checkbox7">Blue</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox8" id="checkbox8" value="checkbox8">Green</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox9" id="checkbox9" value="checkbox9">Pink</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox10" id="checkbox10" value="checkbox10">Grey</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox11" id="checkbox11" value="checkbox11">Brown</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox12" id="checkbox12" value="checkbox12">Spearmint</td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox13" id="checkbox13" value="checkbox13">Lime Green</td>
</tr>
</tbody>
如您所见,它第一次正确地为每个循环输入了第二个,但不是再次。
我已经玩了一段时间了,不知道为什么会这样。 (但现在是星期一,我想弄清楚它有点脑残)有什么我想念的吗?对此的任何帮助将不胜感激。
【问题讨论】:
-
foreach 中的 for 循环是怎么回事?
-
我认为这可能会有所帮助。 php.net/manual/en/function.reset.php需要重置数组上的指针
-
@ThisGuyHasTwoThumbs 我从一个最多可以有 15 列的数据库中获取每个属性,在这种情况下只有 13 种颜色,因此我有
if($value[$attributeColumn] != "")语句,另外我需要将 $i 添加到引用每个字段的表单元素的名称 -
@PaddyHallihan 是用于 CMS 系统还是什么?
-
@RichardHousham 感谢您的评论。我不熟悉 reset() 函数。我在 foreach 循环内部和之后都尝试了
reset ($countryResult);,但没有区别