Jump to content

Archived

This topic is now archived and is closed to further replies.

156418

Populate Drop Down Menu from Query

Recommended Posts

This is probably a simple request, but I cant seem to find an answer - all I can find are answers that are doing more sophisticated things.

I'm running this query:

[code]<?php
//we need to connect to the database so
require('db/db_connect.php');
?>
<?php
$query = "SELECT * FROM EventDates WHERE EventID = '22'";
$results = mysql_query($query)
or die(mysql_error());

?>[/code]

Which works no problem, as I can get the results to display as Text.

However I'd like to take the results and put them into a Drop Down Menu to pass on to the next part of my form.

Could anyone adivse how I take the results and put the into the drop down, I cant seem to get the code right, and just get the php partly displayed in the box!

Share this post


Link to post
Share on other sites
A simple example...

[code]<?php

//we need to connect to the database so

require ( 'db/db_connect.php' );

$result = mysql_query ( 'SELECT * FROM EventDates WHERE EventID = 22;' ) or die ( 'Query Error: ' . mysql_error () );

if ( mysql_num_rows ( $result ) > 0 )
{
echo "<select name='some_select_name'>";

while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) )
{
echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</option>";
}

echo '</select>';
}
else
{
echo 'can not create select box, no results found';
}

?>[/code]

fix the closing </option>, thanks HuggieBear


me!

Share this post


Link to post
Share on other sites
There's a slight error there...

Change this:
[code]echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</a>";[/code]

To this:
[code]echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</option>";[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
[quote author=HuggieBear link=topic=110696.msg448020#msg448020 date=1160144424]
There's a slight error there...

Change this:
[code]echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</a>";[/code]

To this:
[code]echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</option>";[/code]

Regards
Huggie
[/quote]

hehehe, did I do that, that what I get for refusing a link loop, LoL!


Thanks lookout, [b]HuggieBear[/b]

me!

Share this post


Link to post
Share on other sites
Many thanks, I hadn't come across the error as my first page I'm using that on only has one option in the table, but the next one will need multiple options

Share this post


Link to post
Share on other sites
So I've got the first one working with Event Description

But the next one is event date, there are 5 items listed, and the drop down populates ok, however it doesn't pass it on in the POST. I.e the EventDescription, Password and EmailAddress are passing on find, but EventDate which is being seleted at this point isnt.


[code] <?php

//we need to connect to the database so

require ( 'db/db_connect.php' );

$result = mysql_query ( 'SELECT * FROM AvailableDates WHERE EventID = 22;' ) or die ( 'Query Error: ' . mysql_error () );

if ( mysql_num_rows ( $result ) > 0 )
{
echo "<select name='AvailableDates'>";

while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) )
{
echo "<option value='" . $row['EventDate'] . "'>" . $row['EventDate'] . "</a>";
}

echo '</select>';
}
else
{
echo 'can not create select box, no results found';
}

?>

      <input type="hidden" name="EmailAddress" value="<?php echo $_POST['EmailAddress']; ?>">
        <input type="hidden" name="Password" value="<?php echo $_POST['Password']; ?>">
  <input type="hidden" name="EventDescription" value="<?php echo $_POST['EventDescription']; ?>">
 
 
  <input type="submit" name="Submit" value="Submit"></form>[/code]

Share this post


Link to post
Share on other sites
Once the page with the date dropdown appears, view the source and paste it here, that way we can see what values are appearing... Also, post the code for the page that's processing the values.

You need to change the line I pointed out earlier again...

change:
[code=php:0]echo "<option value='" . $row['EventDate'] . "'>" . $row['EventDate'] . "</a>";
[/code]
to:
[code=php:0]echo "<option value='" . $row['EventDate'] . "'>" . $row['EventDate'] . "</option>";[/code]


Regards
Huggie

Share this post


Link to post
Share on other sites

×

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.