PHP-使用内爆、爆炸和array_unique从逗号分隔的两列SQL关键字中自动填充下拉菜单



我有一个SQL数据库,其中包含"category"关键字(只允许一个)和"issues"关键字(多个逗号分隔的单词)。我正在尝试制作一个自动填充下拉关键字选择菜单,方法是从"类别"one_answers"问题"列中选择所有关键字,用内爆将两个返回的数组都变成逗号分隔的字符串,然后将这些字符串组合并将逗号分隔的串串分解成一个数组,同时用array_unique删除重复条目。

但它不起作用。我试过几种方法。这是我的最新作品。它从列中返回了一些值,但不是全部,我不知道为什么。也许array_unique没有按照我希望的方式工作,或者我把转换成字符串并返回到数组的过程搞砸了?有更简单的方法吗?我到处找了一遍,找不到一个好的例子。

这是我现在正在处理的代码。。。

<?
$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列或这些列中逗号分隔的关键字。以下是我尝试以最直接的方式做到这一点:

<?
$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  ";
}
?>

我做错了什么!!!!????

<?
$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  ";
}
?>

尝试替换此

//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;

使用此

$keywordvalue = array_merge($rows, $raws);

对于其他面临相同问题的世界卫生组织,这里是最终工作代码:在数据库中,"类别"是一个单独的关键字,"问题"one_answers"相关问题"是通用的单独关键字。感谢Ksimpson

<form method="GET" action="#">
<select name="keywords"> 
<OPTION selected><? echo $keyword1; ?></OPTION> 
<?
//define the query (the database connection is accomplished elsewhere btw)
$dropdownsql2 = "SELECT DISTINCT category FROM database";
$dropdownsql = "SELECT DISTINCT issue, relatedissues FROM database";
//runthequery
$rs = mysql_query($dropdownsql);
$rs2 = mysql_query($dropdownsql2);
//create an array to hold the keywords
$allvalues = array();    

//take the values from category and append to the array
while ($row = mysql_fetch_row($rs2)) {
    array_push($allvalues, $row[0]);
}

//loop again -- explode creates an array of arrays so we handle in the loop
while ($row = mysql_fetch_array($rs)) {
//for each comma separated string, explode it and append the results
    foreach($row as $str) {
    $exploded = explode(', ', $str);
    array_push($allvalues, $exploded[0]);      
       }
}
//then keep only one copy of duplicated keywords
$values = array_unique($allvalues, SORT_REGULAR);
//and finally echo the keywords into a dropdown 
foreach($values as $value){ 
  echo "<option value="".$value."">".$value."</option>n  ";
}
?> 

我相信这就是您想要的。

<?
$dropdownsql = "SELECT DISTINCT category FROM database";
$dropdownsql2 = "SELECT DISTINCT issues FROM database";
$allvalues = array();
while ($row = mysql_fetch_row($rs)) {
    array_push($allvalues, $row[0]);
}
while ($row = mysql_fetch_row($rs2)) {
    $keywords = explode(',', $row[0]);
    foreach($keywords as $word) {
        array_push($allvalues, $word);
    }
}
//then keep only one copy of duplicated keywords
$values = array_unique($allvalues, SORT_REGULAR);
//and finally echo the keywords into a dropdown 
foreach($values as $value){ 
    echo "<option value="".$value."">".$value."</option>n  ";
}
?>

相关内容

  • 没有找到相关文章

最新更新