我有这张桌子,
--------------------------------------------
| products_id | related_products_ids |
| ------------------------------------------
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
--------------------------------------------
问题1
我有这些复选框,
<input value="1" type="checkbox" name="rp_product[]" id="in-category1"> Microsoft IntelliMouse Pro 1
<input value="2" type="checkbox" name="rp_product[]" id="in-category2"> Microsoft IntelliMouse Pro 2
<input value="3" type="checkbox" name="rp_product[]" id="in-category3"> Microsoft IntelliMouse Pro 3
<input value="3" type="checkbox" name="rp_product[]" id="in-category3"> Microsoft IntelliMouse Pro 4
我使用了这段代码,
<?php
echo '<ul id="categorychecklist" class="list:category categorychecklist form-no-clear">';
global $wpdb;
$sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . (int)$HTTP_GET_VARS['pID']."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$rp_sql = "select products_id, products_name from ".TABLE_PRODUCTS_DESCRIPTION." where products_id !='" . (int)$HTTP_GET_VARS['pID']."' order by products_name";
$rp_1 = mysql_query($rp_sql);
while($rp_2 = mysql_fetch_array($rp_1)) {
$checked = '';
if ($row['related_products_ids'] == $rp_2['products_id']) $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[]\" class=\"rp_item\"" . $checked . "> <span>".$rp_2['products_name']."</span></label></li>";
}
mysql_free_result($rp_1);
echo '</ul></div></div>';
?>
但是我的php代码无效,只选中了1个复选框。如果它们的值存在于数据库表中,我如何制作这些复选框checked
?
问题2
我还希望按产品ID输出相关产品,
我使用了这段代码,
$sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . (int)$_GET["products_id"]."'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$lst_rp = explode(',', $row['related_products_ids']);
if(mysql_num_rows(mysql_query($sql))){
echo '<ul id="related-products-array">';
foreach($lst_rp as $rp_id) {
$res = "select p.products_id, products_type, pd.products_name, pd.products_description, p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price, p.products_tax_class_id, p.products_date_added, p.products_date_available, p.manufacturers_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = '" . $rp_id . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'";
$result1 = mysql_query($res);
$row1 = mysql_fetch_array($result1);
if(mysql_num_rows(mysql_query($res))){
if ($nw_price = tep_get_products_special_price($rp_id)) {
$products_price1 = '<del>' . $currencies->display_price($row1['products_price'], tep_get_tax_rate($row1['products_tax_class_id'])) . '</del><br><span class="productSpecialPrice">' . $currencies->display_price($nw_price, tep_get_tax_rate($row1['products_tax_class_id'])) . '</span>';
} else {
$products_price1 = $currencies->display_price($row1['products_price'], tep_get_tax_rate($row1['products_tax_class_id']));
}
echo '<li><a href="'.get_permalink($storepage).'?slug=product_info.php&products_id='.$rp_id.'" target="_blank">'.tep_image(DIR_WS_IMAGES . $row1['products_image'], addslashes($row1['products_name']), null, null, 'hspace="5" vspace="5" height="120" width="120"').'<br>'.$row1['products_name'].'</a><br>'.$products_price1.'</li>';
} else {
}
}
echo '</ul>';
} else {
echo 'No related products.';
}
echo '<div style="clear:both;"></div>';
仅显示一个相关产品。我知道我的代码有问题,我无法理解。
请帮助。
答案 0 :(得分:1)
最简单的方法是:
function isChecked() {
# perform SQL query
# if value exists, set $exists to true
if ($exists) {
return "checked";
} else {
return "";
}
}
<input type="checkbox" <?php echo isChecked() ?> />
显然,您需要调整代码,可能将整个输入标记放在php中的循环中,并根据您的查询写入“已检查”或不进行“检查”。但关键是:
<input type="checkbox" checked /> Checked by default
答案 1 :(得分:0)
<a href="http://notfound.stackexchange.com/">`enter code here`
<?php $row9=mysql_query("select * from project_cat where des!=''");?>
Please Select How Did They Do It: <br/>
<?php
$query=mysql_query("select * from addproject where page_name='HOW DID THEY DO IT' order by id desc limit 0,1");
$get=mysql_fetch_array($query);
$check=$get['add_project'];
while($sql9=mysql_fetch_assoc($row9)) {
if($check==$sql9['project']) {?>
<input type="radio" name="project" value="<?php echo $sql9['project']; ?>" checked="checked" /><?php echo $sql9['project']; ?><br/>
<?php } else {?>
<input type="radio" name="project" value="<?php echo $sql9['project']; ?>"/><?php echo $sql9['project']; ?><br/>
<?php } } ?></a>