/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Populate drop down menu from database

Hi all,

i have a drop down menu on my form and i would like it to display all values from a field in my database. This is what i have so far…


————————————————————-

<select name=”event”>”);
$db = connect();
$sql = “SELECT Event_Name FROM tblevent”;

foreach($db->query($sql) as $selected_event) {
$page->addToBody(“<option value=””.$selected_event.””>”.$selected_event.”</option>”);
}

$page->addToBody(“</select><br />
————————————————————-

The problem is, the values in the drop down menu are all “Array”.

I’d like them to be all the different events.

Any help will be greatly appreciated.

Cheers
Lewis

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@skywalker2208Apr 08.2010 — Try doing a print_r on the value. You probably have to do something like $selected_event[0] or $selected_event['Event_Name'], but I can't be 100&#37; sure since it looks like you are using a custom database class.
Copy linkTweet thisAlerts:
@ibanezsickoauthorApr 08.2010 — Thanks for the help.

I've ended up rewriting it. Thought i'd post this for anyone who has a similar problem.

Instead of going directly for the event_name using a foreach loop, i used this...


----------------------------------------------------------------
<select name="event">");

$db = connect();

$sql = "SELECT Event_ID, Event_Name FROM tblevent";

$stmt = $db->query($sql);

while ($obj = $stmt->fetchObject()) {

$value = $obj->Event_ID; // whatever that id field is

$desc = $obj->Event_Name; // whatver

$page->addToBody("<option value="".$value."">".$desc."</option>");

}

$page->addToBody("</select><br />
----------------------------------------------------------------



Works perfectly now.

Cheers

Lewis
×

Success!

Help @ibanezsicko spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...