I have the following code to create multiple select form elements based on a number of artists user input. All the select elements are filled with the exact same array (every artist from the artist table in the db).
Code:
// If they have posted data about number of artists and that value is a number
if(isset($_POST['noartists']) && is_numeric($_POST['noartists']))
{
// For the number of artists the item has, create a drop down menu
// for the artists to be selected
$noartists = $_POST['noartists'];
for($i = 0; $i < $noartists; $i++)
{
$sql_query = "SELECT * FROM distro_artist ORDER BY artist ASC";
$result = mysql_query($sql_query);
echo "<tr>
<td class=tablealt1>Artist1</td>
<td class=tablealt2>
<select name=\"artist[]\">";
while($row = mysql_fetch_array($result))
{
$artist = $row['artist'];
$artistid = $row['artistid'];
echo "<option value=\"$artistid\">$artist</option>";
}
echo"
</select>
</td>
</tr>
";
}
}
As you can see, I have to query for the artists each time I make the select box. I'm sure this is a stupid newbie mistake and I should be able to query once and repeatedly use the result... but I'm not sure how. I tried making the query outside the for loop and then just processing the result but as soon as you do it the first time you just get empty select boxes there after. Can anyone help?