Devil_Banner Posted September 18, 2006 Share Posted September 18, 2006 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 4000Deskjet 1200Latitude D515Cisco 2950 seriesI 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 https://forums.phpfreaks.com/topic/21177-fill-a-dropdownlist-with-php-from-a-mysql-db/ Share on other sites More sharing options...
gterre Posted September 18, 2006 Share Posted September 18, 2006 $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 https://forums.phpfreaks.com/topic/21177-fill-a-dropdownlist-with-php-from-a-mysql-db/#findComment-94222 Share on other sites More sharing options...
Devil_Banner Posted September 19, 2006 Author Share Posted September 19, 2006 Great !!!I guess that was an easy one, wasn't it ? :)Lotsa thanks, gterre. It got me stuck for a while. Link to comment https://forums.phpfreaks.com/topic/21177-fill-a-dropdownlist-with-php-from-a-mysql-db/#findComment-94562 Share on other sites More sharing options...
Devil_Banner Posted September 19, 2006 Author Share Posted September 19, 2006 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 https://forums.phpfreaks.com/topic/21177-fill-a-dropdownlist-with-php-from-a-mysql-db/#findComment-94671 Share on other sites More sharing options...
fenway Posted September 20, 2006 Share Posted September 20, 2006 That's more of a JavaScript question (assuming you pull down all the brands first). Link to comment https://forums.phpfreaks.com/topic/21177-fill-a-dropdownlist-with-php-from-a-mysql-db/#findComment-95172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.