Jump to content

Populate Drop Down Menu from Query


156418

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!
Link to comment
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!
Link to comment
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
Link to comment
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!
Link to comment
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]
Link to comment
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
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.