I new to php and mysql.
I wrote the function below:
function catOption() {
$maincatfunc_query = mysql_query("SELECT * FROM mainCats ORDER BY id ") or die(mysql_error());
$funcCat = array();
while ($mainCatFunc = mysql_fetch_array($maincatfunc_query)) {
for ($i = 0; $i < count($maincatfunc_query); $i++) {
$funcCat[$i] = '<option value="'.$maincatfunc_sorgu['mainCatID'].'">' . $maincatfunc_sorgu['name'] . '</option>';
}
}
for ($i = 0; $i < count($maincatfunc_query); $i++) {
return $funcCat[$i];
}
}
I want to fetch "all" value from mysql database and fill it in a dropdown. So i wrote a function like this. But it does not work.
And i don't think count() function doesn't really work in this conditions. How can i get the max count of mysql array ?.
or besides can i do this without using a function ?. I've googled it for a long time but i can't find any usefull info.
Thanks !
You should use like below: [returns array]
function catOption()
{
$query = mysql_query("SELECT * FROM mainCats ORDER BY id ") or die(mysql_error());
$arrCat = array();
while ($row = mysql_fetch_array($query)) {
$arrCat[] = '<option value="'. $row['mainCatID'].'">'. $row['name'].'</option>';
}
return $arrCat;
}
Or this one [returns string]
function catOption()
{
$query = mysql_query("SELECT * FROM mainCats ORDER BY id ") or die(mysql_error());
$arrCat = "";
while ($row = mysql_fetch_array($query)) {
$arrCat .= '<option value="'. $row['mainCatID'].'">'. $row['name'].'</option>';
}
return $arrCat;
}
You should try this,
function catOption() {
$maincatfunc_query = mysql_query("SELECT * FROM mainCats ORDER BY id ") or die(mysql_error());
while ($mainCatFunc = mysql_fetch_array($maincatfunc_query)) {
$funcCat .= '<option value="'.$mainCatFunc['mainCatID'].'">' . $mainCatFunc['name'] . '</option>';
}
return $funcCat;
}
Use like this :
$dropDownData = catOption();
put it into HTML
<select><?php echo $dropDownData; ?></select>