dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Populate drop down menu from database

  1. #1
    Join Date
    Mar 2010
    Posts
    7

    resolved [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

  2. #2
    Join Date
    Mar 2007
    Posts
    946
    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.

  3. #3
    Join Date
    Mar 2010
    Posts
    7
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles