Reputation: 34175
Consider the following PHP array:
$config= array(
"Plan" => array( "Type" => "dropdown", "Options" => "value1, value2, value3"),
);
Now, Instead of hardcoding these values(value1, value2 etc); I want these values(value1,value2 etc) to be fetched from a database.
$rs = mysql_query('SELECT value from tbloptions');
$optvalue = mysql_fetch_array($rs);
I have been thinking about this from past half hours but I can't figure out on How to proceed. Can anyone help?
Upvotes: 1
Views: 2043
Reputation: 21659
mysql_fetch_assoc and mysql_fetch_array return one row at a time, so you have to call the function until there are no more rows. For each row, grab the value and insert it into a numerically indexed array. The join function takes each value of that array, and concatenates them into a string, with the specified delimiter:
while ($row = mysql_fetch_assoc($rs)) {
$opt[] = $row['value'];
}
$config['Options'] = implode(', ', $opt);
Upvotes: 2
Reputation: 3694
Look into mysql_fetch_assoc.
$result = mysql_query('SELECT value from tbloptions');
$rows = "";
while( $row = mysql_fetch_assoc( $result ) )
{
$rows[] = $row;
}
mysql_free_result( $rows );
Upvotes: 0
Reputation: 7337
Looks like you just need to build an array of options and implode it...
foreach ($optvalue as $option)
{
$options[] = $option['value']
}
$config['Options'] = implode(', ', $options);
Upvotes: 1