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

Link to comment
Share on other sites

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


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.