SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    select box query and current value

    Hi all,
    I want to echo the current db value in a html select box, and show other selections from another table also, so the admin can change the value if required.
    I have 2 tables - one contains all the client data, the other contains the areas.
    CLIENT TABLE:
    ----------------------
    |clientID|client|area|
    ----------------------
    | 39|swaugh| nth|
    | 40|billyg| sth|
    ----------------------
    AREAS TABLE:
    -----------------
    | areaID| area|
    -----------------
    | 1| nth|
    | 2| sth|
    | 3| wst|
    | 4| est|
    -----------------
    The code below will echo all the area values to the select box, but I want to echo the current value from the CLIENT table into the select box AND all the values from the AREAS table as well.

    PHP Code:
    <select name="area">
    <?
    $sql
    =mysql_query("SELECT * FROM areas");
    while (
    $row mysql_fetch_object($sql)) { 
    echo  
    "<option value= \" $row->area\">$row->area</option>" ;
    }
    ?>                   
    </select>
    Any help or direction appreciated!
    Mike
    "You know what you know - but that's all you know!"

  2. #2
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    assuming that your current value is stored in $currentrow->area, modify your output like so

    PHP Code:
    echo  "<option value= \"$row->area;
    if (
    $row->area == $currentrow->area) {
        echo ' selected';
    }
    echo "
    \">$row->area</option>" 
    that should work (it also keeps the currently selected option in the right order inside the select...

    [edit]
    and, if you want to be xhtml compliant, change the above code to

    PHP Code:
    echo  "<option value= \"$row->area;
    if (
    $row->area == $currentrow->area) {
        echo ' selected="
    selected"'; // no attribute minimisation
    }
    echo "
    \">$row->area</option>" 
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  3. #3
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Or even:

    PHP Code:
    echo '<option value="' $row->area '"';
    if (
    $row->area == $currentrow->area) {
        echo 
    ' selected="selected"'// no attribute minimisation
    }
    echo 
    '>' $row->area '</option>'
    Mike
    It's not who I am underneath, but what I do that defines me.

  4. #4
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    doh !

    ok...i'll stick to the "design your site" section from now on then
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  5. #5
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Nah, we need you here
    Mike
    It's not who I am underneath, but what I do that defines me.

  6. #6
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies Have a problem though...

    The select box is still only displaying all the area values and not the current value, which i have assigned. (I think!) This is what i've got...
    PHP Code:
    $query = "SELECT * FROM $table WHERE clientID = $clientID";
    $query_result_handle = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($query_result_handle);
        $clientID = $row["clientID"];
        $clientName = $row["clientName"];
        $currentrow = $row["area"];
        
    ........

    <TR><TD>Client Name:</TD>
    <TD><input type="text" name="clientName" size="40" maxlength="50" value="<? echo $clientName?>">
    </TD></TR>

    <TD><select name="area">
    <?
    $sql
    =mysql_query("SELECT * FROM areas");
    while (
    $row mysql_fetch_object($sql)) { 
    echo 
    '<option value="' $row->area '"';
    if (
    $row->area == $currentrow->area) {
        echo 
    ' selected="selected"';
    }
    echo 
    '>' $row->area '</option>';
    }
    ?>                   
    </select>
    </TD></TR>
    What am I missing?
    Mike
    "You know what you know - but that's all you know!"

  7. #7
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Change it to this (I think)

    PHP Code:
    <?
    $area_query 
    mysql_query("SELECT area FROM areas");
    while (
    $areas mysql_fetch_array($area_query)) { 
    echo 
    '<option value="' $areas["area"] . '"';
    if (
    $areas["area"] == $currentrow) {
        echo 
    ' selected="selected"';
    }
    echo 
    '>' $areas["area"] . '</option>';
    }
    ?>
    .
    Last edited by naramation; Jan 28, 2003 at 14:13.
    Mike
    It's not who I am underneath, but what I do that defines me.

  8. #8
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    <edit>
    Mike
    It's not who I am underneath, but what I do that defines me.

  9. #9
    SitePoint Zealot vbthanks's Avatar
    Join Date
    May 2001
    Location
    Sydney
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey naramation - I played hockey with a guy from Penticton... I digress!
    Still not working, just displaying all the values from the areas table, and not the current value from the client table as the selected option.
    edit
    I just echoed $currentrow and it displayed 'Object', so I assigned $currentrow incorrectly - fixed now!
    Thankyou naramation and redux!
    Mike
    "You know what you know - but that's all you know!"

  10. #10
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Mike
    It's not who I am underneath, but what I do that defines me.

  11. #11
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You could put it back to the object way aswell:

    PHP Code:
    <?
    $area_query 
    mysql_query("SELECT area FROM areas");
    while (
    $areas mysql_fetch_object($area_query)) { 
    echo 
    '<option value="' $areas->area '"';
    if (
    $areas->area == $currentrow) {
        echo 
    ' selected="selected"';
    }
    echo 
    '>' $areas->area '</option>';
    }
    ?>
    Mike
    It's not who I am underneath, but what I do that defines me.


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
  •