I started to try to build an array from a select distinct query. The purpose is to make a drop down with the distinct categories. But things did not work so I tried to build the drop down with the results of the query. Still I am beating my head and my eyes are tired from reading.
Here is some code that is not working. The config file is in the same dir as the function file.
<?PHP
function get_categories()
{
include("/config.php");
$host = $db;
$pwd = $db_pwd;
$user = $db_user;
$sql = "SELECT DISTINCT(category) FROM `table`";
$connection = mysql_connect($host, $user, $pwd)or die("Could not connect");
$dbs = mysql_select_db ($user,$connection);
$result = mysql_query($sql, $connection) or die(mysql_error());
if ($result)
{
$categories = "<select name = \"category\">";
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
$cat = row['0'];
$categories .= "<option value = \"$cat\">$cat</option>";
}
$categories .= "</select>";
}
@mysql_free_result ($result);
return $categories;
}
?>
After returning the $categories variable I should be able to echo it and bam, have my drop down. I was going to have an array that I would use for the drop down data and use code like this. I know it works with an array because I have used it several times.
<?PHP
echo '<select name = "ccategory">';
foreach ($categories as $key => $value)
{
echo "<option value = \"$key\"";
if($value == $day)
{
echo ' selected = "selected"';
}
echo ">$value</option>\n";
}
echo '</select>';
?>