Jump to content

Fill a dropdownlist with PHP from a MySQL DB


Devil_Banner

Recommended Posts

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:

[color=green]$conn[/color] = [color=blue]mysql_connect[/color]([color=green]$dbHost[/color], [color=green]$dbUser[/color], $dbPassword) or die([color=orange]'DB connexion error'[/color]);
[color=blue]mysql_select_db[/color]($dbName) or die ([color=orange]"DB not reachable"[/color]);

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

[color=green]$row[/color] = [color=blue]mysql_fetch_array[/color]([color=green]$query[/color]);
[color=green]$item[/color] = [color=green]$row[/color][[color=orange]'ModeleArticle'[/color]];
echo "<option value=\"[color=limegreen]$item[/color]\">[color=green]$item[/color]</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.
Link to comment
Share on other sites

$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
Link to comment
Share on other sites

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 :-)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.