Categories > TinyButStrong general >

Use parentgrp in place of sub-blocks

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: TomH
Date: 2007-06-22
Time: 00:28

Use parentgrp in place of sub-blocks

Is there a way to use the parentgrp technique as a replacement for sub-blocks?

I ask partly because of the repeated cautions that sub-blocks can be slow. Otherwise it is interesting to try different TBS tricks :)

When I do sub-blocks like
$TBS->MergeBlock('main', 'array', 'xmlobj' );
$TBS->MergeBlock('sub', 'array', 'xmlobj[%p1%][rss][channel][item]' );
with template
<tr>
<td width=170 height="4" valign=top>
<xxx><img src='[main.rss.channel.image.url;block=tr;magnet=xxx;noerr]' align=right /></xxx> <font class=feedlink><a href="[main.rss.channel.link;.]" target="_blank">[main.rss.channel.title;.]</a></font><br><font class=feeddescription>[main.rss.channel.description;noerr;.]</font> </td>

<td width="95%">

<table width=550 border=1 cellspacing=0 cellpadding=3>
<tr bgcolor="#ffffcc">
<td width=130 valign=top><font class=items><!-- [sub.$;p1=[main.$];block=tr] --><a href="[sub.link;p1=[main.$]]" target="_blank">[sub.title;p1=[main.$]]</a> </font></td>
<td valign=top> <font face="Arial" size="-1" color="navy">[sub.description;p1=[main.$]]</font></td>
</tr>
</table>
</td>
</tr>
This all works just fine at...
((http://tomhenry.us/tbs3/rss_object_read/TBS_objread_KD_multi.php))

SO now I try to convert the above to use parentgrp -- like so...
$TBS->MergeBlock('main', 'array', 'xmlobj' );
with template like this...
<tr>
<td width=170 height="4" valign=top>
<!-- [main.rss.channel.title;block=tr;.] -->
<xxx>  <img src='[main.rss.channel.image.url;magnet=xxx;noerr]' align=right /></xxx> <font class=feedlink><a href="[main.rss.channel.link;.]" target="_blank">[main.rss.channel.title;parentgrp=main.rss.channel.title;.]</a></font><br><font class=feeddescription>[main.rss.channel.description;.]</font> </td>
<td valign=top width="95%">

<table width=550 border=1 cellspacing=0 cellpadding=3>
<tr bgcolor="#ffffcc"> <!-- [main.rss.channel.item;block=table;.] -->
<td width=130 valign=top><font class=items><a href="[main.rss.channel.item.link;block=table;.]" target="_blank">[main.rss.channel.item.title;.]</a> </font></td>
<td valign=top> <font face="Arial" size="-1" color="navy">[main.rss.channel.item.description;.]</font></td>
</tr>
</table>

</td>
</tr>
This fails as the children (inside of main.rss.channel.item array) are not discovered :)  The errors can be seen here...
http://tomhenry.us/tbs3/rss_object_read/TBS_objread_KD_multi_parentgrp.php

I cannot figure out how to specify the block so that the children are found inside the array of "item"

The array looks like this
Array
(
    [0] => Array
        (
            [rss attr] => Array
                (
                    [version] => 2.0
                )

            [rss] => Array
                (
                    [channel] => Array
                        (
                            [title] => Cruising for sailors
                            [description] => All about cruising
                            [link] => http://cruisingsailor.org
                            [lastBuildDate] => Thu, 21 Jun 2007 17:33:50 +0100
                            [generator] => FeedCreator 1.7.2
                            [item] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Micro-cruisers
                                            [link] => http://cruisingsailor.org/content/view/26/30/
                                            [description] => Small boats are challenging for weeking cruising - because of low speed it's not easy to cover miles. An alternative - a micro multihull to expand your range.
                                            [category] => News - Latest
                                            [pubDate] => Mon, 07 May 2007 10:11:24 +0100
                                        )

                                    [1] => Array
                                        (
                                            [title] => Cruisers First Aid
                                            [link] => http://cruisingsailor.org/content/view/25/36/
                                            [description] => Aways a concern. A good discussion of first aid issues, especially for those who need to be prodded to even consider the subject.
                                            [category] => Cruising Stories - Cruising Stories
                                            [pubDate] => Wed, 28 Mar 2007 09:52:55 +0100
                                        )

                                    [2] => Array
                                        (
                                            [title] => Triad in the Bahamas
                                            [link] => http://cruisingsailor.org/content/view/18/36/
                                            [description] => Triad stops racing long enough to do a Bahamas (http://www.sailtriad.com/bahamas.html) cruise - and maybe just a little competitive sailing.
                                            [category] => Cruising Stories - Cruising Stories
                                            [pubDate] => Thu, 11 Aug 2005 19:13:11 +0100
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [rss attr] => Array
                (
                    [version] => 2.0
                )
...etc...

Can anyone understand what I'm missing here??

Any help/suggestions (even RTFM's) welcomed,
TomH
By: Skrol29
Date: 2007-06-22
Time: 01:18

Re: Use parentgrp in place of sub-blocks

Hi Tom,

Unfortunately parameter "parentgrp" supports only direct items names, and not sub items.
Thus, when you code "parentgrp=main.rss.channel.title" then TBS will search for an item $xmlobj[0]['main.rss.channel.title'] which does not exist.

If you want to display your data using parameter "parentgrp" then you data must be flat, not arranged in sub items. For example if you are merging:
$TBS->MergeBlock('main',$xmlobj);
Then this will merge only 3 records because this data have only 3 records (according to your online example). Even if the total of sub-items is 18.

What you can do is to create a new array containing the same informations but put in flat mode. PHP is working with reference since your are not modifying the item data. So this array will be very fast to create.

By: TomH
Date: 2007-06-22
Time: 05:10

Re: Use parentgrp in place of sub-blocks

Hi Skrol29

Thanks for the helpful explanation of the parentgrp feature - now I see why I had no success.

Maybe there's a generic way to flatten any array - but I haven't been able to find it, do you have a hint about that?

Also, just from your experience, if I did have a flat array for use with parentgrp --- what would you expect the comparison of speed for that as compared to sub-blocks approach?

Thanks a lot,
TomH
By: Skrol29
Date: 2007-06-22
Time: 10:50

Re: Use parentgrp in place of sub-blocks

I don't know if there a generic way to flat arrays. May be in the PHP Array functions, but I don't think so.

About speed, I think your way can be faster, because you're preparing data to have only one MergeBlock().

Here is how you can flat you array:
$flat = array();
foreach ($xmlobj as $row) {
    $chanel = $row['rss']['channel'];
    $ch_base = array();
    $ch_base['ch_title'] = $chanel['title'] ;
    $ch_base['ch_link']  = $chanel['link'] ;
    $ch_base['ch_image'] = $chanel['image'] ;
    foreach ($chanel['item'] as $item)
        $x = $ch_base;
        $x['it_title'] = $item['title'] ;
        $x['it_link'] = $item['link'] ;
        $x['it_descr'] = $item['description'];
        $flat[] = $x;
    }
}
By: TomH
Date: 2007-06-22
Time: 17:42

Re: Use parentgrp in place of sub-blocks

Hi Skrol29,
Thanks to you -- that code for flat array does work just right.

Now that I see the answer I will give a try at building a generic and recursive array-flattener. I will post in "Tips and Tricks" if I get it working.

Thanks again,
TomH