从mysql表中分隔逗号分隔值

时间:2021-02-21 00:24:05

I have a table naming related_products,

我有一个表命名related_products,

Where products_id is the main product.

其中products_id是主要产品。

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

和related_products_ids由与主要产品相关的产品ID组成。

--------------------------------------------
|   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生成,

    <?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.

如何根据related_products_ids值向这些复选框添加选中。例如im在产品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.

另一件事:我还想在产品页面中显示这些相关产品。怎么做?例如,我在products_id为1的产品页面中,我希望通过related_products_ids显示table_name1之类的表中的产品。

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.

请帮忙,因为我不知道该怎么做。谢谢。

2 个解决方案

#1


0  

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

首先,您的复选框不应具有相同的ID。

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.)

显然,假设你有$ lst_product_ids = array(1,2,3,4,5,6)和$ arr_related_product_ids = array(1 =>'1,2,3,4,6,',2 =>等)

UPDATE: Change your code to this (EDIT: after also selecting 'related_products_ids'):

更新:将您的代码更改为此(编辑:在选择'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.

但请记住每个复选框也使用不同的ID。

EDIT: You should also select 'related_products_ids':

编辑:您还应该选择'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

更新:要显示逗号分隔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;

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:

试试这个代码。如果您的产品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'];
}

#2


1  

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.

您的方法不是特别可扩展,当您达到10的每个幂时,相关产品数据的开销会增加。我建议您每个product_id有多行。然后,您可以使用in_array()来查找与产品相关的密钥是否在您现在拥有的结果中。

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.

如果您坚持使用当前的方法,请查看explode(),它将您的值完美地分离到一个数组中,然后您可以使用in_array(),如上所述。

#1


0  

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

首先,您的复选框不应具有相同的ID。

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.)

显然,假设你有$ lst_product_ids = array(1,2,3,4,5,6)和$ arr_related_product_ids = array(1 =>'1,2,3,4,6,',2 =>等)

UPDATE: Change your code to this (EDIT: after also selecting 'related_products_ids'):

更新:将您的代码更改为此(编辑:在选择'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.

但请记住每个复选框也使用不同的ID。

EDIT: You should also select 'related_products_ids':

编辑:您还应该选择'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

更新:要显示逗号分隔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;

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:

试试这个代码。如果您的产品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'];
}

#2


1  

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.

您的方法不是特别可扩展,当您达到10的每个幂时,相关产品数据的开销会增加。我建议您每个product_id有多行。然后,您可以使用in_array()来查找与产品相关的密钥是否在您现在拥有的结果中。

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.

如果您坚持使用当前的方法,请查看explode(),它将您的值完美地分离到一个数组中,然后您可以使用in_array(),如上所述。