Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old Feb 24, 2005, 11:03   #1
Teeej
SitePoint Enthusiast
 
Join Date: Feb 2005
Location: Glasgow, Scotland
Posts: 97
Multiple select boxes containing data from MySQL db help

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?
Teeej is offline   Reply With Quote
 

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 23:42.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved