Results 1 to 6 of 6

Thread: [RESOLVED] Multiple Select List Box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    London
    Posts
    107

    Resolved [RESOLVED] Multiple Select List Box

    I have a function that works correctly, by dynamically assigning a dataset to a drop down box and selecting a specific value based on the specific query row.

    However now I need to do a similar thing for a multi select list box and I can't quite get my head round it.

    Here is my code:



    function drop_down($array_name,$field_id, $query_field_id, $field_name,$server_name,$user_name,$password,$database_name,$query_string,$row,$multiple) {
    //-------------------------------------------------------------------------------------------------------------------------
    $stat_array=get_dataset($server_name,$user_name,$password,$database_name,$query_string);

    //echo "<select name=".$array_name." width='100%'>";

    if(empty($multiple)) {

    echo "<select name=".$array_name." width='100%'>";
    if ($row[$field_id] == $thiscat[$query_field_id]) {}{echo "<option>None Selected</option>";}

    }

    else {echo "<select multiple size =".$multiple." name=".$array_name." width='100%'>";

    }

    foreach ($stat_array as $thiscat)
    {
    echo '<option value="' . $thiscat[$query_field_id] . '"';
    if ($row[$field_id] == $thiscat[$query_field_id]) {echo ' selected';}
    echo '>' . $thiscat[$field_name] . '</option>';
    }

    echo "</select>";

    $db1->close;

    }
    //-------------------------------------------------------------------------------------------------------------------------

    It renders the list box correctly, but I feel I need an additional loop possibly around this line:

    if ($row[$field_id] == $thiscat[$query_field_id]) {echo ' selected';}

    Will that then loop through all the selected values or do I need a different approach?

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Multiple Select List Box

    How is this used in practice? I see that you're looping through the 'rows' in $thiscat. I think your $row variable needs to be a $rowS and do an inner loop to compare each one instead of the same one to all the $thiscat[$query_field_id] values.
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    London
    Posts
    107

    Re: Multiple Select List Box

    Yeah, I think thats what I meant, was really struggling to get my head around it last night. If it helps, the real world example is as follows (its for a bond trading system):

    There is a list of bonds, all with various characteristics, one of the 'sections' refers to the exchanges each particular bond is listed on, so each bond can be listed on multiple exchanges, these are selected from a multi select listbox on my insert screen, then on this update screen I'm building, I'd like each selected exchange to be highlight from the list of those available, for that particular bond.

    Does that help?

  4. #4
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Multiple Select List Box

    So you have a Bonds table, Exchanges table and another relational table (we'll call it BondToExchanges where you can have 0 to many Exchanges to one bond.
    I can't really tell what the code is trying to due, sorry.

    But you basically need to query like so:

    Select ExchangeName, ExchangeID From Exchanges;

    another result set like so:

    Select BtE.ExchangeID From Bonds B Join BondToExchanges BtE ON B.ID = BtE.BondID

    So now you have all your Exchanges and a list of the ones for the current bond in info '$row'.

    Finally, you would just loop through your list of all Exchanges
    and then for each one check it against the ones in the list that the bond contains.
    if the id exists, selected else not selected.

    :Pseudo-code:

    $bondId = 1;

    $bondInfo = mysql_query(sprintf("Select * From Bonds Where ID=%d",intval($bondId)));
    $allExchanges = mysql_query("Select * From Exchanges");
    $bondExchanges = mysql_query(sprintf("Select BtE.ExchangeID From Bonds B Join BondToExchanges BtE ON B.ID = BtE.BondID Where B.ID=%d",intval($bondId)));

    echo "<select >";
    while($exchangeInfo = mysql_fetch_assoc($allExchanges))
    {
    while($bondExchangeInfo = mysql_fetch_assoc($bondExchanges))
    {
    $exchangeFound = false;
    if(in_array($echangeInfo["ID"],$bondExchanges))
    {
    echo "<option selected>".$echangeInfo["ExchangeName"]."</option>";
    $exchangeFound = true;
    break;
    }
    }
    if(!$exchangeFound)
    echo "<option>".$exchangeInfo["ExchangeName"]."</option>";
    }
    echo "</select >";

    :/Pseudo-code:



    Hope that helps you out a bit.
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Location
    London
    Posts
    107

    Re: Multiple Select List Box

    Ok, solved it!

    I created one new function, get_sub_dataset (which is for the inner loop), then I adapted my existing function for drop down/list boxes, here is the code:

    function get_sub_dataset($server, $user, $password, $database, $query7,$sub_field_id) {
    //---------------------------------------------------------------------
    $db1 = new mysqli($server, $user, $password, $database);
    $query1 = $query7;
    $result1 = $db1->query($query1);
    $num_results = $result1->num_rows;

    $result8 = db_result_to_array($result1);
    return $result8;}
    //---------------------------------------------------------------------



    function drop_down($array_name,$field_id, $query_field_id, $field_name,$server_name,$user_name,$password,$database_name,$query_string,$row,$multiple,$query_str ing2,$sub_field_id) {
    //-------------------------------------------------------------------------------------------------------------------------
    $stat_array=get_dataset($server_name,$user_name,$password,$database_name,$query_string);

    //echo "<select name=".$array_name." width='100%'>";

    if(empty($multiple)) {

    echo "<select name=".$array_name." width='100%'>";
    if ($row[$field_id] == $thiscat[$query_field_id]) {}{echo "<option>None Selected</option>";}

    foreach ($stat_array as $thiscat)
    {
    echo '<option value="' . $thiscat[$query_field_id] . '"';

    if ($row[$field_id] == $thiscat[$query_field_id]) {echo ' selected';}

    echo '>' . $thiscat[$field_name] . '</option>';
    }

    }

    else {echo "<select multiple size =".$multiple." name=".$array_name." width='100%'>";

    foreach ($stat_array as $thiscat)
    {
    echo '<option value="' . $thiscat[$query_field_id] . '"';

    $list_box_array=get_sub_dataset($server_name,$user_name,$password,$database_name,$query_string2,$sub _field_id);

    foreach ($list_box_array as $list_box_row)
    {
    if ($list_box_row[$sub_field_id] == $thiscat[$query_field_id]) {echo ' selected';}
    }

    echo '>' . $thiscat[$field_name] . '</option>';
    }

    }

    echo "</select>";

    $db1->close;

    }
    //-------------------------------------------------------------------------------------------------------------------------

  6. #6
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Multiple Select List Box

    Awesome! Glad you got it going : ).
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

Posting Permissions

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



Click Here to Expand Forum to Full Width