SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Nottingham England
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populate Dropdown with value from mysql

    Hi,

    I have built a form which uses three drop down boxes to allow a user to select a date which is then converted to a single date string which is then entered into a database.

    That is all fine and working but now I want to try and reverse engineer this to produce the same dropdown boxes but with a certain date already selected across the three boxes based on a date value pulled from a mysql field.

    I know how to split the date into the three constituent parts (y-m-d) after extracting it but can't for the life of me think how to produce the boxes now with these selected. Is it even possible?

    regards
    netkwake
    It was working when I left last night!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Of course it's possible.
    While populating the drop down boxes, you'll have to confront the value each time with the data you got from your database, and if it's equal then add selected="selected" inside the option tag.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like this?:
    PHP Code:
    <?php

    function selected($opt1,$opt2) {
        if (
    $opt1 === $opt2) echo 'selected="selected"';
    }

    $the_date '2009-05-27';

    ?> 
    <select name="x" id="x">
        <option value="2009-05-25" <?php selected($the_date,'2009-05-25');?> >2009-05-25</option>
        <option value="2009-05-26" <?php selected($the_date,'2009-05-26');?> >2009-05-26</option>
        <option value="2009-05-27" <?php selected($the_date,'2009-05-27');?> >2009-05-27</option>
        <option value="2009-05-28" <?php selected($the_date,'2009-05-28');?> >2009-05-28</option>
        <option value="2009-05-29" <?php selected($the_date,'2009-05-29');?> >2009-05-29</option>
    </select>

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Nottingham England
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why didn't I think of that?

    thanks a lot.

    Simples!!
    It was working when I left last night!


Bookmarks

Posting Permissions

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