Jump to content

[SOLVED] PHP in a 'search form'


richard_PHP

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/86116-solved-php-in-a-search-form/
Share on other sites

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

<?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>";
                }
            ?>


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.