Categories > TinyButStrong general >

Problem with sub block

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: ls_knx
Date: 2007-03-08
Time: 12:07

Problem with sub block

I have 2 tables: tbl_categories and tbl_products. I want to get all categories which have product and show result by sub block (category with product). Ex:

Mainboard
- Intel
- Asus
- Asrock

HDD
- Seagate
- Maxtor

CPU
- Intel

Can you help me, I don't want to use query when merge block. I wrote a function to get all categories (only categories which has product) and products (with category_id is parameter). Both functions get_categories and get_products return a array!
By: TomH
Date: 2007-03-08
Time: 16:25

Re: Problem with sub block

Just thoughts to get you started...

* use the "ondata" method to call a function thatg uses the get_category array
* for each category test the get_products(some_category) element to see of not null
* if not null then populate an output record with the  value

In psuedo code... UNTESTED of course
the template
     [blk1;block=tr;ondata=prepare_output]

* the sql for the block needs to order the get_products "ORDER BY category_id" then we know that if we have "ondata" situation we are processing a category that is not null
*
* sort the get_category array same as the get_products sql result
function prepare_output($BlockName,&$CurrRec,$RecNum) {

global $get_categories; // might need this

      $CurrRec['output']="<tr><td>". $get_categories[category_id] ;

           foreach( get_products(category_id) as $key->$val){
               $CurrRec['output'] .= " - ". $get_product[category_id] ;
                // notice the .=
                       }

        $CurrRec['output'] . = "</td></tr>"
}
... I think you can get the idea from that ;)

It would be interesting to compare how this type of solution compares to the more (IMO) usual use of the TBS sub-block with sub-query technique.

Of course in either event - if the page takes too long to render - the TBS cache should be used definitely.

Hope that helps,