Categories > TinyButStrong general >

Problems with MergeBlock and an array

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Raptor
Date: 2007-04-13
Time: 18:37

Problems with MergeBlock and an array

i got a problem merging an array to an block when using a dynamic query. when is use the syntax as described in the manual like

$tpl_test->MergeBlock('antwort','array','test[%p1%][antwort]');

i just get an empty page caus TBS seems to kill the script. when i use a  syntax like

$tpl_test->MergeBlock('antwort','array',$test[0][antwort]);

it works. It seems the problem appears only when i use the source variable in "tbs string form". when i use it as normal PHP variable it works? So how can i fix this, caus i need the TBS syntax for using %p1% for my dynamic query?

Im usinh PHP5 and TBS 3.0.2
By: TomH
Date: 2007-04-13
Time: 23:41

Re: Problems with MergeBlock and an array

For reference... you need to show us the code for both the main and sub MergeBlock()... and the html template code...

I have example of what you are trying to do, here
http://tomhenry.us/tbs3/  the source code might help you...

Cheers,
TomH




By: Raptor
Date: 2007-04-14
Time: 00:11

Re: Problems with MergeBlock and an array

Yep, thx Tom. Ive already found ur example at ur website by browsing through the board. I think i made it exactly the same way, but it doesnt works. Here the full Code:

<?php

$tpl_test = $this;
$tpl_test->LoadTemplate(PATH . "templates/" . TEMPLATE . "/content/test.html");

$module = new modules;
$module = $module->getModule('2007-03-29.001');

$i = 0;
foreach ($module->frage as $frage) {
                  
                $test[$i]["text"] = $frage;
                $j = 0;
                foreach ($frage->antwort as $antwort) {
                       $test[$i]["antwort"][$j]["text"] = utf8_decode($antwort);
                    $j++;
                }
                $i++;
            }
           
$tpl_test->MergeBlock('frage',$test);
$tpl_test->MergeBlock('antwort','array',$test[0][antwort]);

$tpl_test->Show();
?>

the code works as long as i comment out the second MergeBlock or use a php there variable instead TBS syntax
By: TomH
Date: 2007-04-14
Time: 02:12

Re: Problems with MergeBlock and an array

I struggled with the same symptoms in mine for a _long_ time.. turned out the problem was the template code organization

Show us your html template code...
By: Raptor
Date: 2007-04-14
Time: 10:13

Re: Problems with MergeBlock and an array

aaalright, here we go ;-)

<div id="MCTEST">
<h1>CME-Test</h1>
      <h2>Test [moduleName]</h2>
      <p>Es sind jeweils eine oder mehrere Antworten m&ouml;glich.<br>
        <br>
      </p>
      <table class="testtable" width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2" class="quest">[frage.#][frage.text;block=table]</td>
    <td class="testAdd">&nbsp;</td>
  </tr>
  <tr>
    <td class="testDesc"><input type="radio" name="quest5">    </td>
    <td class="testinputs">[antwort.text;p1=[frage.$];block=tr]</td>
    <td class="testAdd">&nbsp;</td>
  </tr>
</table>
<hr>
</div>
By: TomH
Date: 2007-04-14
Time: 14:36

Re: Problems with MergeBlock and an array

The html looks okay to me, thanks for posting it.

All I can see is that the version of the sub MergeBlock does NOT contain the %p1% parameter - needed to pass the reference [frag.$] from the main query to the subquery

Have you tried this arrangement...
$tpl_test->MergeBlock('frage','array','test');
$tpl_test->MergeBlock('antwort','array','test[%p1%][antwort]');



By: Raptor
Date: 2007-04-14
Time: 20:25

Re: Problems with MergeBlock and an array

oh, i know it doesnt contain the parameter, it was just an example to show that it works when i use an php variable instead of writing it like 'xxx'. i already tried ur version before but the only thing i get is an empty page ... =(
By: Pirjo Posio
Date: 2007-04-15
Time: 01:26

Re: Problems with MergeBlock and an array

Hi Raptor,
I believe your data is not an "array of arrays", like this:
$test[0]['antwort'][] = array('topic'=>'dabadaba', 'value'=>'605');
$test[0]['antwort'][] = array('topic'=>'goo', 'value'=>'25');

That's why it only works with the php variable.
By: Raptor
Date: 2007-04-15
Time: 11:58

Re: Problems with MergeBlock and an array

Hm ur definition
$test[$i]["antwort"][$j] = array('text'=>$antwort);
should be the same as mine
$test[$i]["antwort"][$j]["text"] = $antwort;
However i tried ur version but the result is the same, an empty page =(
By: TomH
Date: 2007-04-15
Time: 15:04

Re: Problems with MergeBlock and an array

Hmmm... when all else fails there is value in checking assumptions.

I would follow Ms Pirjo's lead a bit more and look at the underlying data, dump the data so you can verify that the arrays are structured as expected.

Something like this should work...
$print_array = print_r($test,true);

then template html
<pre>[var.print_array;htmlconv=no;noerr]</pre>

It might help us to help if you post that too.

Keep on going,
TomH
By: Raptor
Date: 2007-04-15
Time: 15:48

Re: Problems with MergeBlock and an array

Thats the result i get when dumping the variable. In my opinion its correct?
Array
(
    [0] => Array
        (
            [text] => 
Frage 1: Welches Kriterium kommt in der Sturzrisikoskala des Abington Memorial Hospital nicht zum Tragen?
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => Stürze in der Vorgeschichte
                        )

                    [1] => Array
                        (
                            [text] => Gang und Gleichgewicht
                        )

                    [2] => Array
                        (
                            [text] => Geschlecht
                        )

                    [3] => Array
                        (
                            [text] => Aktivitäten
                        )

                    [4] => Array
                        (
                            [text] => Alter
                        )

                )

        )

    [1] => Array
        (
            [text] =>
Frage 2: Welche Sturzdefinition geht auf das Expertenteam des DNQP zurück?
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => Ein Sturz wird als unvorhergesehenes Ereignis definiert, das den Betroffenen aus liegender, sitzender oder höherer Position mit Kopf, Rumpf oder Gliedmaßen auf den Boden oder einen Gegenstand aufschlagen lässt.
                        )

                    [1] => Array
                        (
                            [text] => Ein Sturz ist jedes Ereignis, in dessen Folge eine Person unbeabsichtigt auf dem Boden oder auf einer tiefer liegenden Ebene zu liegen kommt.
                        )

                    [2] => Array
                        (
                            [text] => Ein Sturz bedeutet eine unerwünschte Bewegung Richtung Boden oder auf ein tiefer gelegenes Niveau. Eingeschlossen werden Situationen, in denen der Bewohner auf dem Boden liegend / sitzend vorgefunden wird, ohne dass er Auskunft zum Hergang geben kann.
                        )

                    [3] => Array
                        (
                            [text] => Ein Sturz ist eine unkontrollierte Bewegungsänderung mit Folgen.
                        )

                    [4] => Array
                        (
                            [text] => Ein Sturz ist jedes plötzliche, unbeabsichtigte und unkontrollierte Herun-terfallen/-gleiten des Körpers aus dem Liegen, Sitzen oder Stehen auf ei-ne tiefere Ebene.
                        )

                )

        )

    [2] => Array
        (
            [text] =>
Frage 3: Laut Studienergebnisse der Humboldt-Universität (2003) stürzen in deutschen Krankenhäusern:
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => 1,8 % der Patienten
                        )

                    [1] => Array
                        (
                            [text] => 12,3 % der Patienten
                        )

                    [2] => Array
                        (
                            [text] => 8,7 % der Patienten
                        )

                    [3] => Array
                        (
                            [text] => 3,1 % der Patienten
                        )

                    [4] => Array
                        (
                            [text] => 9,9 % der Patienten
                        )

                )

        )

    [3] => Array
        (
            [text] =>
Frage 4: In geriatrischen Abteilungen ereignen sich:
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => 33  % der Sturzereignisse
                        )

                    [1] => Array
                        (
                            [text] => 9,4 % der Sturzereignisse
                        )

                    [2] => Array
                        (
                            [text] => 2,7 % der Sturzereignisse
                        )

                    [3] => Array
                        (
                            [text] => 27 % der Sturzereignisse
                        )

                    [4] => Array
                        (
                            [text] => 19,8 % der Sturzereignisse
                        )

                )

        )

    [4] => Array
        (
            [text] =>
Frage 5: Welche durchschnittlichen Behandlungskosten entfallen auf die Behandlung einer hüftnahen Fraktur als Folge eines Sturzereignisses?
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => ca. 25.000 Euro
                        )

                    [1] => Array
                        (
                            [text] => ca. 10.000 Euro
                        )

                    [2] => Array
                        (
                            [text] => ca. 7.700 Euro
                        )

                    [3] => Array
                        (
                            [text] => ca. 55.000 Euro
                        )

                    [4] => Array
                        (
                            [text] => ca. 1.800 Euro
                        )

                )

        )

    [5] => Array
        (
            [text] =>
Frage 6: Lt. Schadenauswertung des Ecclesia Versicherungsdienstes zu Sturzereignisschäden trifft folgende Aussage zu:
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => Das Pflegepersonal ist überlastet.
                        )

                    [1] => Array
                        (
                            [text] => Das Pflegepersonal ist in Bezug auf das Thema „Sturzprävention“ ungenügend geschult.
                        )

                    [2] => Array
                        (
                            [text] => Eine Sturzanamnese liegt regelmäßig vor.
                        )

                    [3] => Array
                        (
                            [text] => Sturzprävention ist wirkungslos.
                        )

                    [4] => Array
                        (
                            [text] => Die Kostenträger finanzieren keine Präventionsmaßnahmen.
                        )

                )

        )

    [6] => Array
        (
            [text] =>
Frage 7: Welche Ziele werden mit einer Sturzprophylaxe verfolgt?
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => Stürze sollen verhindert werden.
                        )

                    [1] => Array
                        (
                            [text] => Stürze sollen in ihrem Ausmaß gemildert werden
                        )

                    [2] => Array
                        (
                            [text] => Stürze sollen verhindert und die Sturzfolgen minimiert werden.
                        )

                    [3] => Array
                        (
                            [text] => Stürze sollen möglichst durch Ruhigstellung der Patienten vermieden werden.
                        )

                    [4] => Array
                        (
                            [text] => Die richtige Sturztechnik soll vermittelt werden.
                        )

                )

        )

    [7] => Array
        (
            [text] =>
Frage 8: Als intrinsische Risikofaktoren für einen Sturz gelten nicht:
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => Seheinschränkung
                        )

                    [1] => Array
                        (
                            [text] => Gehhilfen
                        )

                    [2] => Array
                        (
                            [text] => Angst vor Stürzen.
                        )

                    [3] => Array
                        (
                            [text] => Sturzvorgeschichte
                        )

                    [4] => Array
                        (
                            [text] => Beleuchtung
                        )

                )

        )

    [8] => Array
        (
            [text] =>
Frage 9: Welche Information ist für die retrospektive Dokumentation eines Sturzereignisses i.d.R. ohne Relevanz?
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => Ort des Sturzes
                        )

                    [1] => Array
                        (
                            [text] => Familienanamnese
                        )

                    [2] => Array
                        (
                            [text] => Folgen des Sturzes
                        )

                    [3] => Array
                        (
                            [text] => Eingeleitete Folgemaßnahmen
                        )

                    [4] => Array
                        (
                            [text] => Fixierungsmaßnahmen
                        )

                )

        )

    [9] => Array
        (
            [text] =>
Frage 10: Welche Maßnahmen unterstützen nicht die Sturzprophylaxe?
   
   
   
   
   

            [antwort] => Array
                (
                    [0] => Array
                        (
                            [text] => Einsatz von Hüftprotektoren
                        )

                    [1] => Array
                        (
                            [text] => Erstellen einer Sturzanamnese im Rahmen der Pflegeanamnese
                        )

                    [2] => Array
                        (
                            [text] => Schaffung einer sicheren Umgebung (Beleuchtung, Betthöhe etc.)
                        )

                    [3] => Array
                        (
                            [text] => Wechsellagerung
                        )

                    [4] => Array
                        (
                            [text] => Medikationsstatuserhebung
                        )

                )

        )

)
By: Raptor
Date: 2007-04-15
Time: 15:54

Re: Problems with MergeBlock and an array

btw, i still think it cant be my array, caus when i use the source parameter without apostrophes as normal php variable it works perfectly. it just hangs up when im using TBS syntax with apostrophes ...
By: Pirjo Posio
Date: 2007-04-16
Time: 22:29

Re: Problems with MergeBlock and an array

If I had this problem, I might rearrange the data like this:
$frage = array();
$frage[0] = array('text' => 'Frage 1: Welches...?');
$frage[0]['antwort'][0] = array('text' => 'Stürze...');
$frage[0]['antwort'][1] = array('text' => 'Gang...');
$frage[0]['antwort'][2] = array('text' => 'Geschlecht...');
$frage[1] = array('text' => 'Frage 2: Welche...?');
$frage[1]['antwort'][0] = array('text' => 'Ein Sturz wird...');
$frage[1]['antwort'][1] = array('text' => 'Ein Sturz ist..');
$frage[1]['antwort'][2] = array('text' => 'Ein Sturz bedeutet...');
The above data array is very understandable to us humans. :-)

The merging part of the PHP code would be:
...
$tpl_test->MergeBlock('frage','array','frage');
$tpl_test->MergeBlock('antwort','array','frage[%p1%][antwort]');
$tpl_test->Show();
And the table part of the HTML:
   <table class="testtable" width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2" class="quest">[frage.#].&nbsp;[frage.text;block=table]</td>
    <td class="testAdd">&nbsp;</td>
  </tr>
  <tr>
    <td class="testDesc"><input type="radio" name="quest5"> </td>
    <td class="testinputs">[antwort.text;p1=[frage.$];block=tr]</td>
    <td class="testAdd">&nbsp;</td>
  </tr>
</table>
By: Raptor
Date: 2007-04-17
Time: 14:43

Re: Problems with MergeBlock and an array

tried this one too. result stays the same .... when is use apostrophes the site hangs up and i get an empty page, when removing the apostrophes and using a php variable it works.

if its not a bug i guess TBS got some problems with my server maybe?
By: Pirjo Posio
Date: 2007-04-18
Time: 01:04

Re: Problems with MergeBlock and an array

I'm sorry you could not be helped with my code. For me it works well, I tested it.
But, I use PHP4 and TBS 3.2.0. You said you use PHP5 and TBS 3.0.2. Why don't you use the latest version of TBS?
The only thing I know is to check and recheck, that your quotes are in order. I prefer single quotes, as you can see from my code.
Remember also to use that %p1% in your antwort-MergeBlock (or any sub-block).
I cannot do more, since I don't have your environment.
By: Raptor
Date: 2007-04-19
Time: 14:37

Re: Problems with MergeBlock and an array

FOR EVERYONE WHO GOT THE SAME PROBLEM, HERE´S THE SOLUTION:

The problem is how PHP5 handles variables! TBS can only work with global variables, but PHP5 do not handle variables without definition as global ones as PHP4 did. Variables without definition automatically becomes private ones. So u have simply to use
$GLOBALS['test'][]
instead of
$test[]
However big thanks to Tom and Pirjo, who tried their best to help out ;-)

Greetz Raptor
By: Pirjo Posio
Date: 2007-04-20
Time: 08:19

Re: Problems with MergeBlock and an array

Congratulations, Raptor, for finding the solution!

Your findings deserve the right to be put in the TBS manual, if it is not yet mentioned clearly, clearly, that with TBS and PHP5 one MUST specifically define the variables as global. And also in the Golden Rules. How is it, Skrol29? I know that you have reminded people of the need of global variables a thousand times...