Jump to content

help populating a list with a mysql query


Darkmatter5

Recommended Posts

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?

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>";

 

=)

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';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.