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.
$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
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 :-)

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.