Jump to content


Photo

Fill a dropdownlist with PHP from a MySQL DB


  • Please log in to reply
4 replies to this topic

#1 Devil_Banner

Devil_Banner
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 18 September 2006 - 07:14 PM

Hi there,

I have this really dumb problem with my query. I have to admit i'm a newbie, hence my stupid problem.
I Use MySQL 5. (Easyphp 1.8 )
I have a table which has a column named ArticleModel.
I Have four entries in this table.
My goal is to select the entries in this table and output them into a dropdown list.
For example, I want the list to contain
LaserJet 4000
Deskjet 1200
Latitude D515
Cisco 2950 series

I can't get further than having 4 times 'array' written in my dropdown list. I have managed to get the first one (Laserjet 4000) to get into the list, but I can't figure out how to loop so it continues to fill my list...

Here's my code:

$conn = mysql_connect($dbHost, $dbUser, $dbPassword) or die('DB connexion error');
mysql_select_db($dbName) or die ("DB not reachable");

//query
$sql = "SELECT * FROM article ";
$query = mysql_query($sql);
if (!$query) {
              // error handler
              echo 'Query failed. SQL: ', $sql, '<br />Error # ', mysql_errno(), ' Error msg: ', mysql_error();
              exit;
             }
//end of query

$row = mysql_fetch_array($query);
$item = $row['ModeleArticle'];
echo "<option value=\"$item\">$item</option>";

Now, I suppose that before the next to last line of code, there should be a loop, but like I said : can't figure out how...

Thanks alot for helping me out.

#2 gterre

gterre
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 18 September 2006 - 09:06 PM

$conn = mysql_connect($dbHost, $dbUser, $dbPassword) or die('DB connexion error');
mysql_select_db($dbName) or die ("DB not reachable");

//query
$sql = "SELECT * FROM article ";
$query = mysql_query($sql);
if (!$query) {
              // error handler
              echo 'Query failed. SQL: ', $sql, '
Error # ', mysql_errno(), ' Error msg: ', mysql_error();
              exit;
             }
//end of query

while ($row = mysql_fetch_array($query)) {

$item = $row['ModeleArticle'];
echo '<option value='.$item.'>'.$item.'</option>';

} //end loop


#3 Devil_Banner

Devil_Banner
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 September 2006 - 11:56 AM

Great !!!
I guess that was an easy one, wasn't it ? :)
Lotsa thanks, gterre. It got me stuck for a while.


#4 Devil_Banner

Devil_Banner
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 September 2006 - 02:54 PM

Thanks alot for helping me out on that one.
Say I wanted to improve and have the user choose in a first dorpdownlist, and by doing so have it populate the second dropdownlist with stuff relevant to the first choice?
Lemme explain :
Say the user chooses "printer" in the first dropdownlist (populated by "ArticleType" column (Article.ArticleType)), then I would want the second dropdownlist (Article.ArticleBrand) to display choices like HP, Epson, and so on...
Is this easy to do ? I mean, dynamically  ?
Thanks very much for you guys' time :-)

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 20 September 2006 - 09:44 AM

That's more of a JavaScript question (assuming you pull down all the brands first).
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users