PHP-使用implode,explode和array_unique从两个SQL列中以逗号分隔的关键字自动填充下拉菜单 [英] PHP - Using implode, explode, and array_unique to autopopulate a dropdown menu from two SQL columns of comma-separated keywords

查看:156
本文介绍了PHP-使用implode,explode和array_unique从两个SQL列中以逗号分隔的关键字自动填充下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL数据库,其中带有类别"关键字(仅允许一个关键字)和问题"关键字(多个逗号分隔的单词).我试图通过从类别"和问题"列中选择所有关键字,然后将两个返回的数组都转换为带爆破逗号分隔的字符串,然后组合字符串并展开,来制作一个自动填充的下拉关键字选择菜单逗号分隔的字符串放入数组,同时使用array_unique删除重复的条目.

I have an SQL database with a "category" keyword (only one allowed) and "issues" keywords (multiple comma-separated words). I am trying to make a auto-populating drop-down keyword select menu by selecting all the keywords from the "category" and "issues" columns, turning both returned arrays into comma-separated strings with implode, then combining the strings and exploding the comma-separated strings into an array, while removing duplicate entries with array_unique.

但是它不起作用.我尝试了几种方法.这是我的最新消息.它从列中返回一些值,但不是全部,我不知道为什么.也许array_unique不能按照我想要的方式工作,或者我弄乱了转换为字符串然后返回数组的转换?有没有更简单的方法可以做到这一点?我到处搜索,在任何地方都找不到很好的例子.

But it's not working. I've tried several approaches. Here is my latest. It is returning SOME values from the column but not all, and I can't figure out why. Perhaps array_unique isn't working the way I want it to work, or I am messing up the conversion to strings and back into an array? Is there a simpler way to do this? I have searched all over and can't find a good example anywhere.

这是我现在正在使用的代码...

Here is the code I have working now...

<?
$dropdownsql = "SELECT DISTINCT category FROM database";
$keywords = mysql_query($dropdownsql);
while($row = mysql_fetch_array($keywords))
{ 
  echo "<option value=\"".$row['category']."\">".$row['category']."</option>\n  ";
}
?>

虽然这适用于一词类别关键字,但显然不能处理多个SQL列或这些列中的逗号分隔的关键字.这是我尝试以最直接的方式进行的操作:

While this works for the one-word category keywords, it obviously can't handle multiple SQL columns or comma-separated keywords within those columns. Here's my attempt to do that in the most straightforward way:

<?
$dropdownsql = "SELECT DISTINCT category FROM database";
$dropdownsql2 = "SELECT DISTINCT issues FROM database";

//run sql queries separately.  Ideally they would be combined into one right?
$rs = mysql_query($dropdownsql);
$rs2 = mysql_query($dropdownsql2);
$row = mysql_fetch_array($rs);
$raw = mysql_fetch_array($rs2);

//then implode the resulting arrays, placing commas & spaces so they'll match
$rows = implode(", ", $row);
$raws = implode(", ", $raw);

//try to concatenate the strings of comma-separated keywords
$keywordvaluesstring = $rows.$raws;

//then explode the concatenated string back into array
$keywordvalue = explode(", ",$keywordvaluesstring);

//then keep only one copy of duplicated keywords
$values = array_unique($keywordvalue, SORT_REGULAR);

//and finally echo the keywords into a dropdown 
foreach($values as $value){ 
  echo "<option value=\"".$value."\">".$value."</option>\n  ";
}
?>

我在做什么错!!!! ?????

WHAT AM I DOING WRONG!!!!????

推荐答案

<?
$dropdownsql = "SELECT DISTINCT category FROM database";
$dropdownsql2 = "SELECT DISTINCT issues FROM database";

//run sql queries separately.  Ideally they would be combined into one right?
$rs = mysql_query($dropdownsql);
$rs2 = mysql_query($dropdownsql2);
$keywords = array();
while ($row = mysql_fetch_array($rs)) {
    $keywords[] = $row[0];
}
while($raw = mysql_fetch_array($rs2)) {
    $keywords = array_merge($keywords, explode(', ', $raw[0]));
}

$values = array_unique($keywords, SORT_STRING);

//and finally echo the keywords into a dropdown 
foreach($values as $value){ 
  echo "<option value=\"".$value."\">".$value."</option>\n  ";
}
?>

这篇关于PHP-使用implode,explode和array_unique从两个SQL列中以逗号分隔的关键字自动填充下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆