Categories > TinyButStrong general >

Linked selects

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Martin
Date: 2005-03-31
Time: 19:10

Linked selects

Hi,

I'm exploring PHPand TBS together with ezsql. Now my question is what would be the way to go to create two linked select lists populated from mySQL? Any hint is appreciated.

By: Skrol29
Date: 2005-04-01
Time: 11:40

Re: Linked selects

Hi,

What do you call "two linked lists"?

If it's populating the second list using a SQL statement that depends on a value get from the first list, then it's simple.
By: Martin
Date: 2005-04-01
Time: 12:25

Re: Linked selects

Hello,

The first select (category) list has a number of categories that it retrieves from mySQL the select returns the id field. The second select (product) list has to show products based on the selected category.

I also looked around for a solution with javascript to fill the selects without refreshing the pages. But there are numerous examples from small to big/complicated and I'm not sure which one to pick or were to start to this with tbs/ezsql.
By: Skrol29
Date: 2005-04-01
Time: 14:32

Re: Linked selects

Hi,

It seems that you problem is quite the same as the previous post named "2 selects dependents (combo) Javascript".
I've just posted a solution for it.
By: Martin
Date: 2005-04-01
Time: 17:07

Re: Linked selects

Thank you, I take a look.
By: Martin
Date: 2005-04-04
Time: 14:51

Re: Linked selects

Hi Skrol29,

I adapted your code from the other thread but the selected entry jumps to the last item in the select list? Perhaps I made a mistake. This is what I made out of it.

html
<div id="categorySelect">
    <form action="[var..script_name]" method="get" name="formCategorySelect" >
        Category &nbsp;
        <select name="blk_list_category" id="blk_list_category" onchange="submit(this)">
            <option value="0">&lt; Select one... &gt;</option>
            <option value="[blk_list_category.Id;block=option]" selected>[blk_list_category.Name]</option>
        </select>
    </form>
</div>

<div id="productSelect">
    <form action="[var..script_name]" method="get" name="formProductSelect" >
        Product &nbsp;
        <select name="blk_list_product" id="blk_list_product" onchange="submit(this)">
            <option value="[blk_list_product.Id;block=option]" selected>[blk_list_product.Name]</option>           
        </select>
    </form>
</div>
php
<?php
    require_once('includes/config.inc.php');
    require_once('classes/ez/ez_sql.php');
    require_once('classes/tbs/tbs_ez_sql.php');
    require_once('classes/tbs/tbs_class.php');
    require_once('classes/es_query.php');

    $image = '';
   
    $db = new db($db_user,$db_password,$db_name,$db_server);
   
    $TBS = new clsTinyButStrong;
    $TBS->LoadTemplate('templates/tbs_us_productchart.htm') ;
   
    // Build variables from selected items
    $categoryId = (isset($_GET['blk_list_category'])) ? intval($_GET['blk_list_category']) : 0;
    $productId = (isset($_GET['blk_list_product'])) ? intval($_GET['blk_list_product']) : 0;
   
    // Check selection
    //if ($categoryId==0) $productId = 0;
   
    $sql = new EsQuery;
    // Merge category list
    $TBS->MergeBlock('blk_list_category',$db,$sql->queryAllCategories());
   
    // Merge product list
    if ($categoryId > 0) {
        $TBS->MergeBlock('blk_list_product',$db,$sql->queryProductByCategoryId($categoryId));       
    } else {
        //$TBS->MergeBlock('blk_list_product','clear');
        $TBS->MergeBlock('blk_list_product',$db,$sql->queryAllProducts()) ;
    }
    $image = 'chart.php' . '?productId=' . $productId;
    $TBS->Show() ;
?>
By: Skrol29
Date: 2005-04-05
Time: 00:34

Re: Linked selects

Hello,

This is because of the 'selected' attribute. All merged items have the attribute 'selected' so the last one is displayed. I makde the mistake in the snippet I've posted. That was an untested snipped just of illustate how to make the solution with refreshing the page after each sub-select.

I've posted an example (tested) with the solution that hold all the data into the page, so the page hasn't to be reloaded after each sub-select.