简体   繁体   中英

Separate comma separated values from mysql table

I have a table naming related_products,

Where products_id is the main product.

and related_products_ids consist of product ids related to the main product.

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

I have checkboxes,

<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

The checkboxes are generated by 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>';
    ?>

How to add checked to these checkboxes depending on the related_products_ids values. For example im in the product id 1.

Another thing: I also want to display those related products in the product page. How to do that? Example, Im in the product page where products_id is 1, i want to display the products from a table like table_name1 by related_products_ids .

UPDATE: I have used this code for displaying data,

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

However, it displays nothing..

Is my code wrong?

Please help as i don't know how to do it. Thank you.

Your method is not particularly scalable, with the overhead for your related products data growing as you hit each power of 10. What I would recommend is that you instead have multiple rows per product_id. You can then use in_array() to find if the products related key is in the results you now have.

If you insist on using your current method, have a look at explode(), which will seperate your values perfectly into an array, with which you can then use with in_array() as described above.

First of all, your checkboxes should not have the same ids.

Do something like this:

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 . ">
}

Obviously, assuming you have $lst_product_ids = array(1,2,3,4,5,6) and $arr_related_product_ids = array(1 => '1,2,3,4,6,', 2 => etc.)

UPDATE: Change your code to this (EDIT: after also selecting '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>";
}

But remember also to use different ids for each checkbox.

EDIT: You should also select 'related_products_ids':

$rp_sql = "select products_id, products_name, related_products_ids from ".TABLE_PRODUCTS_DESCRIPTION." order by products_id";

UPDATE: To display values for the comma separated ids, and with your current (probably inflexible) method of storing comma-separated relations, you will have to use an extra foreach($lst_rp as $rp_id) loop inside the while loop, and inside the foreach loop do a SQL select for each related product, something like

$query = "SELECT products_id, products_name FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE products_id=" . $rp_id;

UPDATE: If you have new issues, you should probably post a new question and link to this one as reference, otherwise this could go on forever.

Try this code. If your product id is a SQL int(), then you must not use quotes in the query:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM