2

I have this table,

--------------------------------------------
|   products_id   |   related_products_ids |
| ------------------------------------------
|    1            |   2                    |
|    1            |   3                    |
|    1            |   4                    |
--------------------------------------------

Problem 1

I have these checkboxes,

<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

I used this code,

<?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>';
?>

But my php code is not working, only 1 checkbox is checked. How can i make those checkboxes checked if their value exist on the database table?

Problem 2

I also wanted to output the related products by products ids,

I used this code,

$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>';

Only one related product is displayed. I know there is something wrong with my code, I just can't figure it out.

Please Help.

2
  • This is off topic, but I think you could strongly benefit from the use of sprintf / printf for building strings. It would make the above code much easier to write, read, and maintain. Commented Apr 15, 2012 at 1:46
  • http_get_vars is deprecated, and you shouldn't be using it at all in any new code. These days it's $_GET. You're also running your queries multiple times, which is inefficient, and assuming they've succeeded, which is bad. Commented Apr 15, 2012 at 3:46

2 Answers 2

1

The easiest way would be to do this:

function isChecked() {
    # perform SQL query
    # if value exists, set $exists to true
    if ($exists) {
        return "checked";
    } else {
        return "";
    }
}

<input type="checkbox" <?php echo isChecked() ?> />

Obviously, you would need to adjust your code, maybe put the whole input tag in a loop in php and write 'checked' or not depending on your query. But the key is:

<input type="checkbox" checked /> Checked by default
Sign up to request clarification or add additional context in comments.

Comments

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>

1 Comment

Mind to add some words of explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.