Categories > TinyButStrong general >

MergeBlock() with array of arrays (with inner numeric indexes)

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

MergeBlock() with array of arrays (with inner numeric indexes)

This (single array with inner numeric indexes) works
My data array (slurped and converted into array from an RSS feed)
Array
(
    [rss attr] => Array
        (
            [version] => 2.0
        )

    [rss] => Array
        (
            [channel attr] => Array
                (
                    [version] => 2.0
                )

            [channel] => Array
                (
                    [title] => Daily Star Front Page
                    [link] => http://www.azstarnet.com/sn/dailystar/
                    [description] => Daily Star Front Page headlines from the Arizona Daily Star
                    [language] => en-us
                    [pubDate] => 2007-04-06
                    [ttl] => 60
                    [item] => Array
                        (
                            [0] => Array
                                (
                                    [title] => Union gets Pima workers' approval
                                    [link] => http://www.azstarnet.com/sn/dailystar/177192.php
                                    [description] => Around 4,500 Pima County employees will be represented by the Service Employees International Union after workers voted to approve the union by a 17-to-1 margin.
                                    [category] => 0x02000100
                                    [enclosure attr] => Array
                                        (
                                            [url] => http://www.azstarnet.com/ss/2007/04/06/177192-2.jpg
                                            [type] => image/jpeg
                                        )

                                    [enclosure] =>
                                )

                            [1] => Array
                                (
                                    [title] => Tram road up Sabino Canyon is set for rebuild to the top, Kyl says at site
                                    [link] => http://www.azstarnet.com/sn/dailystar/177172.php
                                    [description] => The road up Sabino Canyon will be repaired in time for winter visitors to ride the popular tram to the end of the line, but restrooms there, buried under a rock slide, won't be rebuilt.
                                    [category] => 0x07000000
                                    [enclosure attr] => Array
                                        (
                                            [url] => http://www.azstarnet.com/ss/2007/04/06/177172-2.jpg
                                            [type] => image/jpeg
                                        )

                                    [enclosure] =>
                                )

                            [2] => Array
                                (
                                    [title] => AZ House: Prove legal status to do business
                                    [link] => http://www.azstarnet.com/sn/dailystar/177169.php
                                    [description] => PHOENIX — The state House narrowly approved legislation Thursday to require all who want a state, county or city permit to do business to prove they are in this country legally.
                                    [category] => 0x07080000
                                )

                            [3] => Array
                                (
                                    [title] => City considers buying site of Broadway Volvo
                                    [link] => http://www.azstarnet.com/sn/dailystar/177162.php
                                    [description] => Tucson could spend up to $2.7 million to buy the Broadway Volvo car dealership, which is moving outside the city limits.
                                    [category] => 0x07080800
                                )
                        )
                )
        )
)

Using the following php (works)
global $xmlobj;
include ("./xml_parser_KD.php");

$url = "http://rss.azstarnet.com/index.php?section=s1a";
$xml = implode('', file($url));
$xmlobj = XML_unserialize(&$xml);

// blk1 gets me the top level info about the RSS feed...
$TBS->MergeBlock('blk1', 'array', array($xmlobj));
// blk2 drills down into the array to get the multiple items...
$TBS->MergeBlock('blk2', 'array', $xmlobj[rss][channel][item]);

$TBS->Show();

and the .html template for this (works)
<tr><th align=left colspan=2>&#160; &#160; &#160; &#160;
RSS Feed for &#160; &#160;"<a href="[blk1.rss.channel.link;block=tr;.]" target="_blank">[blk1.rss.channel.title;.]</a>"
</th></tr>

<tr bgcolor="#ffffcc">
<td valign=top><font size="-0" color="Blue">[blk2.title;block=tr;.]</font></td>
<td valign=top> <font face="Arial" size="-1" color="navy">[blk2.description;.]</font></td>
</tr>

Now the problrem... the "multiple" RSS feeds I generate an array of arrays (another array for each of multiple RSS feeds)
the php to generate the data array

Yes, I think main/sub blocks are the possible approach, and here is what I am trying for the PHP...
include ("./xml_parser_KD.php");
$list[] = "http://cruisingsailor.org/component/option,com_rss/feed,RSS2.0/no_html,1";
$list[] = "http://rss.azstarnet.com/index.php?section=s1a";

foreach ($list as $key=>$url){
$xml = implode('', file($url));
$xmlobj[] = XML_unserialize(&$xml);
unset($xml);
}

$TBS->MergeBlock('main', 'array', $xmlobj );
$TBS->MergeBlock('sub', 'array', 'xmlobj[%p1%][channel][item]' );

$TBS->Show();

The above php code gives array of arrays like this (example of just two feeds)
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] => Fri, 06 Apr 2007 12:07:21 +0100
                            [generator] => FeedCreator 1.7.2
                            [item] => Array
                                (
                                    [0] => 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
                                        )

                                    [1] => 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
                                        )

                                    [2] => Array
                                        (
                                            [title] => Crew Overboard Regatta
                                            [link] => http://cruisingsailor.org/content/view/23/30/
                                            [description] => Experience from in-the-water testing can help you to build a plan for your boat and the skills of your crew. Join in a Crew Over Board Regatta (http://crewoverboard.org/): compete, win stuff
                                            [category] => News - Latest
                                            [pubDate] => Fri, 07 Apr 2006 21:27:57 +0100
                                        )
                                )
                        )
                )
        )
    [1] => Array
        (
            [rss attr] => Array
                (
                    [version] => 2.0
                )
            [rss] => Array
                (
                    [channel attr] => Array
                        (
                            [version] => 2.0
                        )

                    [channel] => Array
                        (
                            [title] => Daily Star Front Page
                            [link] => http://www.azstarnet.com/sn/dailystar/
                            [description] => Daily Star Front Page headlines from the Arizona Daily Star
                            [language] => en-us
                            [pubDate] => 2007-04-06
                            [ttl] => 60
                            [item] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Union gets Pima workers' approval
                                            [link] => http://www.azstarnet.com/sn/dailystar/177192.php
                                            [description] => Around 4,500 Pima County employees will be represented by the Service Employees International Union after workers voted to approve the union by a 17-to-1 margin.
                                            [category] => 0x02000100
                                            [enclosure attr] => Array
                                                (
                                                    [url] => http://www.azstarnet.com/ss/2007/04/06/177192-2.jpg
                                                    [type] => image/jpeg
                                                )

                                            [enclosure] =>
                                        )

                                    [1] => Array
                                        (
                                            [title] => Tram road up Sabino Canyon is set for rebuild to the top, Kyl says at site
                                            [link] => http://www.azstarnet.com/sn/dailystar/177172.php
                                            [description] => The road up Sabino Canyon will be repaired in time for winter visitors to ride the popular tram to the end of the line, but restrooms there, buried under a rock slide, won't be rebuilt.
                                            [category] => 0x07000000
                                            [enclosure attr] => Array
                                                (
                                                    [url] => http://www.azstarnet.com/ss/2007/04/06/177172-2.jpg
                                                    [type] => image/jpeg
                                                )

                                            [enclosure] =>
                                        )

                                    [2] => Array
                                        (
                                            [title] => AZ House: Prove legal status to do business
                                            [link] => http://www.azstarnet.com/sn/dailystar/177169.php
                                            [description] => PHOENIX — The state House narrowly approved legislation Thursday to require all who want a state, county or city permit to do business to prove they are in this country legally.
                                            [category] => 0x07080000
                                        )

                                    [3] => Array
                                        (
                                            [title] => City considers buying site of Broadway Volvo
                                            [link] => http://www.azstarnet.com/sn/dailystar/177162.php
                                            [description] => Tucson could spend up to $2.7 million to buy the Broadway Volvo car dealership, which is moving outside the city limits.
                                            [category] => 0x07080800
                                        )
                                )
                        )
                )
        )
)

But now I'm stumped -- I can't see how to do recursion through each of the top level arrays and then also access the numeric indexes of "items" inside of each RSS array.

My template like this...
<tr><th align=left colspan=2>&#160; &#160; &#160; &#160;
RSS Feed for &#160; &#160;"<a href="[main.rss.channel.link;block=tr;.]" target="_blank">[main.rss.channel.title;.]</a>"
</th></tr>

<tr bgcolor="#ffffcc">
<td valign=top><font size="-0" color="Blue">[sub.title;block=tr;p1=[main.$];.]</font></td>
<td valign=top> <font face="Arial" size="-1" color="navy">[sub.description;.]</font></td>
</tr>



And I tried that and many other templates for a long time :( but could not get anything running.

Any examples or ideas would be very appreciated!

Thanks for TBS
TomH
By: Pirjo Posio
Date: 2007-04-07
Time: 23:44

Re: MergeBlock() with array of arrays (with inner numeric indexes)

Hi TomH,
I'm not sure if this helps any, and I have not time to test it. Besides, I'm not quite sure I understand your question well enough. But does this message on this same forum help?
http://www.tinybutstrong.com/forum.php?msg_id=7002
By: TomH
Date: 2007-04-08
Time: 00:19

sub-block indexing error? Re: MergeBlock() with array of arrays (with inner numeric indexes)

Hi Pirjo,
Yes, that is the exact model I am using now!

$TBS->MergeBlock('main', 'array', $xmlobj );
$TBS->MergeBlock('sub', 'array', 'xmlobj[%p1%][rss][channel][item]' );
and
<tr bgcolor="#ffffcc">
<td valign=top><font size="-0" color="Blue">
<a href="[sub.link;p1=[main.$];block=tr;.]" 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>

You can see my actual testing with results page, php source, and template source by looking here http://tomhenry.us/tbs3/rss_object_read/

There are two errors showing on the results page "multi"
Note the following errors in the results... 
* the sub-block items are rendered _below_ the three RSS feed parents when they should be just below the second RSS feed
# that the four 'items' that are displayed are the child 'items' that associate with the second RSS feed parent

Maybe from there and the source that is available there you might seee what I'm missing.



By: Pirjo Posio
Date: 2007-04-08
Time: 03:37

Re: sub-block indexing error? Re: MergeBlock() with array of arrays (with inner numeric indexes)

Sorry, I can't help you test this, too busy, too sleepy, 4:25 a.m...
But here's a very old 3-level TBS example I found, originally by Skrol29, I believe:
<table border="1" cellspacing="0" bordercolor="#0000FF">

  <tr>

    <td>[b1.$;block=tr]</td>

    <td>

      <table border="1" cellspacing="0" bordercolor="#FF0000">

        <tr>

          <td>[b2.$;block=tr;p1=[b1.$]]</td>

          <td>

            <table border="1" cellspacing="0" bordercolor="#00FF00">

              <tr>

                <td>[b3.val;block=tr;p1=[b1.$];p2=[b2.$]]</td>

              </tr>

            </table>

          </td>

        </tr>

      </table>

    </td>

  </tr>

</table>
And the php:
<?

//Here is an example with data stored into a Php array like:

//   $src['A'] = array('a1'=>'a1-1');



include_once('tbs_class.php');

$TBS = new clsTinyButStrong;

$TBS->LoadTemplate('levels3.htm');


$src = array (

  'A' => array (

    'a1' =>array('a1-1','a1-2'),

    'a2'=>array('a2-1','a2-2')

    ),

  'B' => array (

    'b1' =>array('b1-1','b1-2'),

    'b2'=>array('b2-1','b2-2')

    ),

  'C' => array ('c1' =>array('c1-1','c1-2'),'c2'=>array('c2-1','c2-2'))

);


$TBS->MergeBlock('b1','array','src');    //string syntax

$TBS->MergeBlock('b2','array','src[%p1%]');

$TBS->MergeBlock('b3','array','src[%p1%][%p2%]');

$TBS->Show();

?>
Hope you can tweak this into a solution for your problem.
Pirjo
By: TomH
Date: 2007-04-08
Time: 18:22

Re: sub-block indexing error? Re: MergeBlock() with array of arrays (with inner numeric indexes)

Pirjo,
Thanks for pushing me onward!

The example you provided also did NOT work after I modified the template to the same form as mine.
So after some many, many puzzled re-re-re-reading of the diff's I did see the simple error I was making. Solved.

Results (working!) now at http://tomhenry.us/tbs3/

Thanks for TBS,
TomH