0

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

6
  • also, this is WHERE minimumprice = 35"; but I need it to pull all values, and I am not sure how to :( sorry for me being such a noob... Commented Nov 14, 2012 at 19:27
  • just to clarify, i think it would need to echo echo "<option value="$minimumprice">"$postcode"</option>"; but all of the rows in the table, not just a single echo Commented Nov 14, 2012 at 19:32
  • Please don't use the mysql_* API anymore, the community is deprecating it. It's highly unsafe and inefficient. Use PDO or mysqli with prepared statements instead. Commented Nov 14, 2012 at 20:19
  • Wherever you found this terrible code, forget about it and start again here: phptherightway.com Commented Nov 14, 2012 at 20:20
  • no I am totally goosed, feel like I don't know anything about PHP again :( Commented Nov 14, 2012 at 20:32

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

how do i remove this line? $query = "SELECT * FROM $usertable WHERE minimumprice = 35"; as it only selects 1 value here
plus this is also returning a blank value, please could you edit my PHP and point out the errors I have made too? Regards
@HenryAspden It's not very polite to ask others to do stuff for you
@JuanMendes asking, I was, I am asking for help, I am trying code after code after code... and looping round and round and round... :( I am sorry, I didn't mean to be rude, i just thought it might help to work backwards from some successfull code
worked a charm once entered into the live version... Thanks for your help !!!!!!!!!!
1
$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>';

9 Comments

meh, I think my first bit of my PHP is wrong, can you please help by editing my PHP with the addition of yours and pointing out anywhere I have made a mistake? This code isn't erroring, but is returning a blank page :(
Turn on all error notification and show us some error messages
ok, one quick question, if my mySQL and my webhosting are both on the same server, can I use "localhost" or is there another way? regards
Yes, you can use localhost, you can also use the actual servername, but localhost will still work when you move it to a different server
Try changing your query to just be SELECT * FROM $usertable to see if you're just not getting any results. Add debugging statements to the else clause of the if ($resuls) and see if you ever go into your while loop
|
0

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.

1 Comment

no just the value need to be caried forward to the Javascript element, the postcode is just for refrence purposes to the user...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.