我有一个表命名related_products,
其中
products_id
是主要产品。
和related_products_ids
由与主要产品相关的产品ID组成。
--------------------------------------------
| products_id | related_products_ids |
| -----------------------------------------
| 1 | 1,2,3,4,6, |
| -----------------------------------------
| 2 | 1,2,3, |
| -----------------------------------------
| 3 | 1,2, |
-------------------------------------------
我有复选框,
<input value="1" type="checkbox" name="rp_product[]" id="in-category"> Microsoft IntelliMouse Pro 1
<input value="2" type="checkbox" name="rp_product[]" id="in-category"> Microsoft IntelliMouse Pro 2
<input value="3" type="checkbox" name="rp_product[]" id="in-category"> Microsoft IntelliMouse Pro 3
<input value="4" type="checkbox" name="rp_product[]" id="in-category"> Microsoft IntelliMouse Pro 4
<input value="5" type="checkbox" name="rp_product[]" id="in-category"> Microsoft IntelliMouse Pro 5
<input value="6" type="checkbox" name="rp_product[]" id="in-category"> Microsoft IntelliMouse Pro 6
复选框由php生成,
<?php
echo '<div class="categorydiv"><div id="category-all" class="tabs-panel"><ul id="categorychecklist" class="list:category categorychecklist form-no-clear">';
$rp_sql = "select products_id, products_name from ".TABLE_PRODUCTS_DESCRIPTION." order by products_id";
$rp_1 = mysql_query($rp_sql);
while($rp_2 = mysql_fetch_array($rp_1)) {
echo "<li id=\"category-".$rp_2['products_id']."\" class=\"popular-category\"><label class=\"selectit\"><input value=\"".$rp_2['products_id']."\" type=\"checkbox\" name=\"rp_product[]\" id=\"in-category-1\"> ".$rp_2['products_name']."</label></li>";
}
mysql_free_result($rp_1);
echo '</ul></div></div>';
?>
如何根据checked
值向这些复选框添加related_products_ids
。例如,我在产品ID 1中。
另一件事:我还想在产品页面中显示这些相关产品。怎么做?
例如,我在products_id
为1的产品页面中,我想显示table_name1
表格中的产品related_products_ids
。
更新 我已使用此代码显示数据
$sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . (int)$_GET["products_id"]."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$lst_rp = explode(',', $row['related_products_ids']);
foreach($lst_rp as $rp_id) {
$res = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id='" . $rp_id . "'";
}
$result1 = mysql_query($res);
while($row1 = mysql_fetch_array($result1)) {
echo $row1['products_name'];
}
然而,它什么也没显示..
我的代码错了吗?
请帮助,因为我不知道该怎么做。 谢谢。
答案 0 :(得分:1)
您的方法不是特别可扩展,当您达到每个10的幂时,相关产品数据的开销会增加。我建议您每个product_id有多行。然后,您可以使用in_array()来查找与产品相关的密钥是否在您现在拥有的结果中。
如果您坚持使用当前的方法,请查看explode(),它会将您的值完美地分离到一个数组中,然后您可以使用in_array(),如上所述。
答案 1 :(得分:0)
首先,您的复选框不应具有相同的ID。
做这样的事情:
foreach ($lst_product_ids as $product_id) {
$checked = '';
$lst_rel_prods = explode(',', $arr_related_product_ids[$product_id]);
if (in_array($product_id, $lst_rel_prods))
$checked = " checked";
echo "<input value='" . $product_id . "' type='checkbox' name='rp_product[]'" . $checked . ">
}
显然,假设你有$ lst_product_ids = array(1,2,3,4,5,6)和$ arr_related_product_ids = array(1 =&gt;'1,2,3,4,6,',2 = &gt;等。)
更新: 将您的代码更改为此(编辑:在选择'related_products_ids'之后):
while($rp_2 = mysql_fetch_array($rp_1)) {
$checked = '';
$lst_rp = explode(',', $rp_2['related_products_ids']);
if (in_array($rp_2['products_id'], $lst_rp)) $checked = " checked";
echo "<li id=\"category-".$rp_2['products_id']."\" class=\"popular-category\"><label class=\"selectit\"><input value=\"".$rp_2['products_id']."\" type=\"checkbox\" name=\"rp_product[]\" id=\"\"" . $checked . "> ".$rp_2['products_name']."</label></li>";
}
但请记住也要为每个复选框使用不同的ID。
编辑:你还应该选择'related_products_ids':
$rp_sql = "select products_id, products_name, related_products_ids from ".TABLE_PRODUCTS_DESCRIPTION." order by products_id";
更新:要显示逗号分隔的ID的值,并使用当前(可能不灵活)存储逗号分隔关系的方法,您必须在while循环内部使用额外的foreach($lst_rp as $rp_id)
循环,并且foreach循环为每个相关产品执行SQL选择,如
$query = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id=" . $rp_id;
更新:如果您有新问题,您应该发布一个新问题并链接到此问题作为参考,否则这可能会永远持续下去。
试试这段代码。如果您的产品ID是SQL int(),那么您不能在查询中使用引号:
$sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = " . $_GET["products_id"]; // I MADE CHANGES HERE
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result); // I MADE A CHANGE HERE
$lst_rp = explode(',', $row['related_products_ids']);
foreach($lst_rp as $rp_id) {
$query = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id=" . $rp_id;
$result1 = mysql_query($res); // THIS AND THE FOLLOWING MUST STAY INSIDE THE LOOP
$row1 = mysql_fetch_assoc($result1);
echo $row1['products_name'];
}