ngreenwood6 Posted December 3, 2008 Share Posted December 3, 2008 I am trying to get my database to populate a select option. I have the following code: while($row = mysql_fetch_array($results)){ $dates = $row['dates']; foreach($dates as $key => $values){ $key = $values; echo "<form>"; echo "<select>"; echo "<option values='$values'>$values</option"; echo "</select>"; echo "</form>"; echo $values; } } I know that this has to be wrong. Can someone please help me with the foreach and the database values. Quote Link to comment https://forums.phpfreaks.com/topic/135400-mysql-populate/ Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 <?php $output = "<form><select name=dates>"; while($row = mysql_fetch_array($results)){ $dates = $row['dates']; foreach($dates as $key => $values){ // $key = $values; not sure what that is for $output .= "<option values='$values'>$values</option>"; //echo $values; } } $output .= "</select></form>"; echo $output; ?> Should work, assuming $dates is an array. Quote Link to comment https://forums.phpfreaks.com/topic/135400-mysql-populate/#findComment-705242 Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Author Share Posted December 3, 2008 I have the select box but it is not populated with any data. I know that the data is in the database. Oh yean and I am still getting this error: Warning: Invalid argument supplied for foreach() in C:\wamp\www\CMS\test.php on line 24 Line 24 is blank but line 25 is this: foreach($dates as $key => $values){ Quote Link to comment https://forums.phpfreaks.com/topic/135400-mysql-populate/#findComment-705249 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 <?php $output = "<form><select name=dates>"; while($row = mysql_fetch_array($results)){ $output .= "<option values='" . $row['dates'] . "'>" . $row['dates'] . "</option>"; } $output .= "</select></form>"; echo $output; ?> Maybe that is what you are after? Quote Link to comment https://forums.phpfreaks.com/topic/135400-mysql-populate/#findComment-705251 Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Author Share Posted December 3, 2008 Yeah the last post that you made worked like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/135400-mysql-populate/#findComment-705252 Share on other sites More sharing options...
ngreenwood6 Posted December 3, 2008 Author Share Posted December 3, 2008 When is the best time to use a foreach? Quote Link to comment https://forums.phpfreaks.com/topic/135400-mysql-populate/#findComment-705253 Share on other sites More sharing options...
premiso Posted December 3, 2008 Share Posted December 3, 2008 When is the best time to use a foreach? When you need to iterate through all elements of an array. foreach Quote Link to comment https://forums.phpfreaks.com/topic/135400-mysql-populate/#findComment-705256 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.