richard_PHP Posted January 15, 2008 Share Posted January 15, 2008 Hi, Part of my assignment using PHP is to create a search function within a car website. What I've done is a drop down list that gets the records from a database and displays them. However, because there are multiple car makes in the database they come up several times. Code: <?php // Start the connection to the database $conn = mysql_pconnect("**", "**", "**"); // Select the database to use mysql_select_db("**",$conn); // Select records from the database $result = mysql_query ("SELECT * FROM ** LIMIT 0 , 50"); ?> <form name='myform' action='searchresult.php' method='post'> <p><select name='chosen' tabindex='1'> <option value="" selected="selected">Select..</option> <?php while ($row=mysql_fetch_array($result)) //loops through results { echo "<option value=".$row['make'].">".$row['make']."</option>"; } ?> </select></p> <p><input type='submit' value='Submit' tabindex='2' /></p> </form> <p></p> I'm wanting the list to display 'Aston Martin, BMW, Ferrari" etc. But what it's really showing is "Aston Martin, BMW, BMW, Ferrari, Ferrari". (BMW and Ferrari have multiple cars). Please help, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/ Share on other sites More sharing options...
adam291086 Posted January 15, 2008 Share Posted January 15, 2008 I think that is more of an issue with your database structure. You have repeating data in your table which should of been removed in the first steps of normalisation Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439737 Share on other sites More sharing options...
richard_PHP Posted January 15, 2008 Author Share Posted January 15, 2008 So there is no way at all to get rid of the extra makes? ??? Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439739 Share on other sites More sharing options...
adam291086 Posted January 15, 2008 Share Posted January 15, 2008 Whats your database structure. In your make feild do you have Astin Martin DB9 or just Astin Martin Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439743 Share on other sites More sharing options...
richard_PHP Posted January 15, 2008 Author Share Posted January 15, 2008 Just Aston Martin. The fields I'm wanting to use for the search form is 'make' and 'model'. The search form above uses 'make' to list in the drop-down and the search result displays the whole car using 'make' and 'model'. Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439744 Share on other sites More sharing options...
adam291086 Posted January 15, 2008 Share Posted January 15, 2008 sorry there is away i forgot about Select DISTINCT Names FROM table; will return astin martin once Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439747 Share on other sites More sharing options...
richard_PHP Posted January 15, 2008 Author Share Posted January 15, 2008 Changed code: <?php // Start the connection to the database $conn = mysql_pconnect("**", "**", "**"); // Select the database to use mysql_select_db("**",$conn); // Select records from the database $result = mysql_query ("SELECT DISTINCT * FROM ** LIMIT 0 , 50"); ?> <form name='myform' action='searchresult.php' method='post'> <p><select name='chosen' tabindex='1'> <option value="" selected="selected">Select..</option> <?php while ($row=mysql_fetch_array($result)) //loops through results { echo "<option value=".$row['make'].">".$row['make']."</option>"; } ?> And it's still bringing up multiple results. I'll give you the list as it appears: "Aston Martin BMW BMW Ferrari Ferrari Mercedes-Benz Mercedes-Benz Mitsubishi Nissan Noble Noble Porsche Porsche Porsche Subaru Subaru TVR Vauxhall" Grrr.. Lol.. Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439753 Share on other sites More sharing options...
adam291086 Posted January 15, 2008 Share Posted January 15, 2008 <?php // Start the connection to the database $conn = mysql_pconnect("**", "**", "**"); // Select the database to use mysql_select_db("**",$conn); // Select records from the database $result = mysql_query ("SELECT DISTINCT * FROM ** LIMIT 0 , 50"); ?> <form name='myform' action='searchresult.php' method='post'> <p><select name='chosen' tabindex='1'> <option value="" selected="selected">Select..</option> <?php while ($row=mysql_fetch_array($result)) //loops through results { echo "<option value=".$row['make'].">".$row['make']."</option>"; } ?> should be <?php // Start the connection to the database $conn = mysql_pconnect("**", "**", "**"); // Select the database to use mysql_select_db("**",$conn); // Select records from the database $result = mysql_query ("SELECT DISTINCT make FROM table_name LIMIT 0 , 50"); ?> <form name='myform' action='searchresult.php' method='post'> <p><select name='chosen' tabindex='1'> <option value="" selected="selected">Select..</option> <?php while ($row=mysql_fetch_array($result)) //loops through results { echo "<option value=".$row['make'].">".$row['make']."</option>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439754 Share on other sites More sharing options...
richard_PHP Posted January 15, 2008 Author Share Posted January 15, 2008 Brilliant! That did the job! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439757 Share on other sites More sharing options...
adam291086 Posted January 15, 2008 Share Posted January 15, 2008 Hope you get an A in this project and the correct places of references marked down e.g phpfreak.com - adam291086 HAHAHAHA Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439760 Share on other sites More sharing options...
richard_PHP Posted January 15, 2008 Author Share Posted January 15, 2008 Lol.. Thanks. And I will with the reference. Quote Link to comment https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/#findComment-439763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.