Darkmatter5 Posted April 23, 2008 Share Posted April 23, 2008 I have a table with local counties listed. EXAMPLE: 1 Hays 2 Travis 3 Blanco 4 Bastrop With the following code why do I get a list with the entry of only Bastrop and not Bastrop, Blanco, Hays, Travis? <?php include 'dbconfig.php'; include 'opendb.php'; $query=mysql_query(sprintf("SELECT County FROM byrnjobdb.counties ORDER BY County ASC")); $result=mysql_fetch_assoc($query); #echo "{$result['County']}"; echo "<select name='SurvCounty'>"; foreach ($result as $r) { echo "<option value='$r'>$r</option>"; } echo "</select>"; include 'closedb.php'; ?> Is there an easier way to do this and as I don't really understand the usage of foreach, did I implement it correctly? Link to comment https://forums.phpfreaks.com/topic/102591-help-populating-a-list-with-a-mysql-query/ Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 echo "<select name='SurvCounty'>"; while ($result=mysql_fetch_assoc($query)) { #echo "{$result['County']}"; $r = $result['County']; echo "<option value='$r'>$r</option>"; } echo "</select>"; Use that in place of: $result=mysql_fetch_assoc($query); #echo "{$result['County']}"; echo "<select name='SurvCounty'>"; foreach ($result as $r) { echo "<option value='$r'>$r</option>"; } echo "</select>"; =) Link to comment https://forums.phpfreaks.com/topic/102591-help-populating-a-list-with-a-mysql-query/#findComment-525372 Share on other sites More sharing options...
p2grace Posted April 23, 2008 Share Posted April 23, 2008 You could also use extract: <?php include 'dbconfig.php'; include 'opendb.php'; $query=mysql_query(sprintf("SELECT County FROM byrnjobdb.counties ORDER BY County ASC")); echo "<select name='SurvCounty'>"; while($result=mysql_fetch_assoc($query)); extract($result); echo "<option value='$County'>$County</option>"; } echo "</select>"; include 'closedb.php'; ?> Link to comment https://forums.phpfreaks.com/topic/102591-help-populating-a-list-with-a-mysql-query/#findComment-525375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.