Henry Aspden
Henry Aspden

Reputation: 1945

Fill <select> using mySQL and PHP

I have the following drop down list

<select name="SelP">
    <option value="0">Please Select...</option>
    <option value="35">LS1</option>
    <option value="35">LS2</option>
    <option value="35">LS3</option>
    <option value="35">LS4</option>
    <option value="35">Postcode not Listed</option>
</select>

and I have written the PHP to work like so

<?php
//Sample Database Connection Syntax for PHP and MySQL.
//Connect To Database
$hostname="localhost";
$username="alloytes_achenry";
$password="**PASSWORD HERE**";
$dbname="alloytes_acquote";
$usertable="LocationTable";
$postcode="postcode";
mysql_connect($hostname,$username, $password) or die ("    <html>%MINIFYHTML4333ddb1f6ba50276851b9f9854a5c817%</html>");
mysql_select_db($dbname);
# Check If Record Exists
$query = "SELECT * FROM $usertable WHERE minimumprice = 35";
$result = mysql_query($query);
if($result)
{
while($row = mysql_fetch_array($result))
{
$postcode = $row["$postcode"];
echo "$postcode";
}
}
?>

This works well and returns the postcode value. I now need to edit the PHP to return both $postcode and $minimumprice , and then to populate the table with the postcode going as the text and the $minimumprice as the value (value="$minimumprice").

Any advice where to start would be greatly appreciated

Thanks

Henry

TEST CODE 1

<?php
//Sample Database Connection Syntax for PHP and MySQL.
//Connect To Database
$hostname="localhost";
$username="alloytes_achenry";
$password="f(6@g0^O#2kT";
$dbname="alloytes_acquote";
$usertable="LocationTable";
$postcode="postcode";
$minimumprice="minimumprice";
mysql_connect($hostname,$username, $password) or die (" <html>%MINIFYHTML4333ddb1f6ba50276851b9f9854a5c817%</html>");
mysql_select_db($dbname);
# Check If Record Exists
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if($result)
{
while ($row = mysql_fetch_assoc($result)) {
$postcode = $row['postcode'];
$minimumprice = $row['minimumprice'];
echo <<<EOL
<option value="$minimumprice">$postcode</option>

EOL;
}
}
?>
</form>

Returns Just the postcode, any thoughts?

Thanks

Upvotes: 0

Views: 2723

Answers (3)

Ruan Mendes
Ruan Mendes

Reputation: 92274

$select = '<select name="SelP"> <option value="0">Please Select...</option>';
$result = mysql_query($query);
if($result){
    while($row = mysql_fetch_array($result)) {
        $postcode = $row["postcode"];
        $minimumprice = $row["minimumprice"];
        $select .= "<option value=\"$postcode\">$minimumprice</option>";
    }
}
$select .= '</select>';

Upvotes: 1

Landon
Landon

Reputation: 4108

The only thing sent to the server is what you have in the value="" part of the option. Nothing else gets sent. If you really need to send both of those values, you might want to change the value to be something like 35,LS4. Have both values in there separated by a comma. Then on the serverside when you catch it, just explode() that value into an array and use the 0th and 1st element accordingly.

Upvotes: 0

Marc B
Marc B

Reputation: 360572

Something like this?

while ($row = mysql_fetch_assoc($result)) {
   $postcode = $row['postcode'];
   $minimumprice = $row['minimumprice'];
   echo <<<EOL
<option value="$minimumprice">$postcode</option>

EOL;
}

note the use of a heredoc

Upvotes: 2

Related Questions