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.
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 ";
}
?>
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!!!!????
I believe this is what you want.
Try replacing this
With This
FOR OTHERS WHO FACE THIS SAME PROBLEM, HERE IS THE FINAL WORKING CODE: IN THE DATABASE, 'CATEGORY' IS A SINGLE KEYWORD, AND 'ISSUE' AND 'RELATEDISSUES' ARE COMMA-SEPERATED KEYWORDS. Thanks to Ksimpson