Categories > TinyButStrong general >

MergeField not calling user function

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Don Bledsoe
Date: 2008-06-29
Time: 18:44

MergeField not calling user function

I've worked on this for several days without much success. I am not a very good programmer, but I am working on it.

I have this template, view.tpl.html:
<TABLE class=format width="400">
    <TR><TD class="dataL Year"><b>[blk.year;block=tr+tr+tr+tr+tr]</b></TD><TD class="dataL Award">[blk.award]</TD></TR>
    <TR><TD class="dataL Year" rowspan="4"><img src="img/[var.oscarimg]" border=0 width=25 height=55></TD><TD class=dataL>[blk.title]</TD></TR>
    <TR><TD class="dataL">[var.writers]</TD></TR>
    <TR></TD><TD class="dataL">[blk.origintype;nodata] [blk.originauthor;nodata]</TD></TR>
    <TR></TD><TD class="dataL">[blk.notes;onformat=noslash;fct=stripslashes;nodata]</TD></TR>
</TABLE>
THE PROBLEM ARE:  [var.oscarimg] AND [var.writers]

Here is the view.php file:
$writers= "";
$oscarimg = "";

$TBS->LoadTemplate('tpl/view.tpl.html');

$TBS->MergeField('oscar','oscarimg',true);
$TBS->MergeField('writers','getwriter',true);

$TBS->MergeBlock('blk','tbssql','SELECT * FROM awards ORDER BY year,oscar');
$TBS->Show();

/*  FUNCTIONS ---------------------------------- */

function getwriter($BlockName, &$CurrRec, $RecNum) {
    global $writers;
    if($CurrRec['writer2'] == "") {
      $writers = $CurrRec['writer1'];
    } else {
      $writers = $CurrRec['writer1'] . ", " . $CurrRec['writer2'];
    }
    if($CurrRec['writer3'] !== "") { $writers .=  ", " . $CurrRec['writer3']; }
    if($CurrRec['writer4'] !== "") { $writers .=  ", " . $CurrRec['writer4']; }
    if($CurrRec['writer5'] !== "") { $writers .=  ", " . $CurrRec['writer5']; }
    if($CurrRec['writer6'] !== "") { $writers .=  ", " . $CurrRec['writer6']; }
    if($CurrRec['writer7'] !== "") { $writers .=  ", " . $CurrRec['writer7']; }
    return $writers;
}

function oscarimg($FieldName,&$CurrVal) {
   global $oscar;
   if($CurrRec['oscar'] == 0) {
        $oscar = "oscar_color_sm.jpg";
   }
    else {
        $oscar = "oscar_blank_sm.jpg";
   }
    return $oscar;
}
By: Skrol29
Date: 2008-07-01
Time: 01:55

Re: MergeField not calling user function

Hi Don,

You have not the correct syntax for the functions getwriter() and oscarimg().
You are using MergeField(), so the correct syntax is here:
  http://www.tinybutstrong.com/manual.php#php_mergefield

In oscarimg(), you are using a variable $CurrRec which is set anywhere.
By: Don Bledsoe
Date: 2008-07-01
Time: 02:21

Re: MergeField not calling user function

I guess I'm confused. The data fields in each MySQL record includes:

oscar -- digit 0,1,2,3,4, or 9 (0=won OSCAR,2-4=nominated, 9=no OSCAR given)
writer1 -- always NOT NULL
writer2 to writer7 -- may be NULL

So, if oscar=0 then show image of OSCAR, else show blank image. I'll continue working on it.
By: TomH
Date: 2008-07-01
Time: 12:18

Re: MergeField not calling user function

Not sure I see exactly what is not working after you fix what Skrol29 says... but maybe can make it more simple with just one ondata function like this...
$TBS->LoadTemplate('tpl/view.tpl.html');

$TBS->MergeBlock('blk','tbssql','SELECT * FROM awards ORDER BY year,oscar');
$TBS->Show();

/*  FUNCTIONS ---------------------------------- */

function fct_oscar($BlockName, &$CurrRec, $RecNum) {

if($CurrRec['writer2'] == "") {
      $CurrRec['writers'] = $CurrRec['writer1'];
} else {
      $CurrRec['writers'] = $CurrRec['writer1'] . ", " . $CurrRec['writer2'];
}
if($CurrRec['writer3'] !== "") {$CurrRec['writers'] .=  ", " . $CurrRec['writer3']; }
if($CurrRec['writer4'] !== "") { $CurrRec['writers'] .=  ", " . $CurrRec['writer4']; }
if($CurrRec['writer5'] !== "") { $CurrRec['writers'] .=  ", " . $CurrRec['writer5']; }
if($CurrRec['writer6'] !== "") { $CurrRec['writers'] .=  ", " . $CurrRec['writer6']; }
if($CurrRec['writer7'] !== "") { $CurrRec['writers'] .=  ", " . $CurrRec['writer7']; }

if($CurrRec['oscar'] == 0) {
  $CurrRec['oscarimg'] = "oscar_color_sm.jpg";
   }
else {
  $CurrRec['oscarimg'] = "oscar_blank_sm.jpg";
   }
}

And the HTML becomes...
<TABLE class=format width="400">
<TR><TD class="dataL Year"><b>[blk.year;block=tr+tr+tr+tr+tr;ondata=fct_oscar]</b></TD><TD class="dataL Award">[blk.award]</TD></TR>
<TR><TD class="dataL Year" rowspan="4"><img src="img/[blk.oscarimg]" border=0 width=25 height=55></TD><TD class=dataL>[blk.title]</TD></TR>
<TR><TD class="dataL">[blk.writers]</TD></TR>
<TR></TD><TD class="dataL">[blk.origintype;nodata] [blk.originauthor;nodata]</TD></TR>
<TR></TD><TD class="dataL">[blk.notes;onformat=noslash;fct=stripslashes;nodata]</TD></TR>
</TABLE>

Of course NOT TESTED and needs to be verified  -- my typing might miss something -- but hope you get my idea from this.

Hope that helps,
TomH
By: Don Bledsoe
Date: 2008-07-18
Time: 06:18

Re: MergeField not calling user function

TomH,

I apologize for not posting this back sooner, but I've been ill and am just now getting this working.

Admittedly, I'm a noob and this and your advice was VERY helpful in pointing me in the right direction. I wanted to post the final, working version of the script and template file so others can see what my problems were and the solution that's now working.

view.php:
$TBS->LoadTemplate('tpl/view.tpl.html');
$TBS->MergeBlock('blk','tbssql','SELECT * FROM awards ORDER BY year,award,oscar');
$TBS->Show();

/*  FUNCTIONS ---------------------------------- */

//    USAGE:    [myfield; onformat=noslash; fct=stripslashes]
function noslash($FieldName, &$CurrVal, &$CurrPrm) {
    $fct = $CurrPrm['fct'];
    if (function_exists($fct)) {
        $CurrVal = $fct($CurrVal);
    }
}

function fct_oscar($BlockName, &$CurrRec, $RecNum) {

    global $writers;
    if($CurrRec['writer2'] == "") {
        $CurrRec['writers'] = $CurrRec['writer1'];
    } else {
        $CurrRec['writers'] = $CurrRec['writer1'] . ", " . $CurrRec['writer2'];
    }

    if($CurrRec['writer3'] !== "") { $CurrRec['writers'] .=  ", " . $CurrRec['writer3']; }
    if($CurrRec['writer4'] !== "") { $CurrRec['writers'] .=  ", " . $CurrRec['writer4']; }
    if($CurrRec['writer5'] !== "") { $CurrRec['writers'] .=  ", " . $CurrRec['writer5']; }
    if($CurrRec['writer6'] !== "") { $CurrRec['writers'] .=  ", " . $CurrRec['writer6']; }
    if($CurrRec['writer7'] !== "") { $CurrRec['writers'] .=  ", " . $CurrRec['writer7']; }

    if($CurrRec['oscar'] == 0) { $CurrRec['oscarimg'] = "oscar_color_sm.jpg"; } else { $CurrRec['oscarimg'] = "oscar_blank_sm.jpg"; }

    global $award;
    if($CurrRec['award'] == "BS") { $CurrRec['award'] = "Academy Award for Best Story"; }
    if($CurrRec['award'] == "OS") { $CurrRec['award'] = "Academy Award for Best Original Screenplay"; }

    if($CurrRec['oscar'] == 0) { $CurrRec['award'] = $CurrRec['award'] . " -- Winner"; }
    if($CurrRec['oscar'] == 1) { $CurrRec['award'] = $CurrRec['award'] . " -- Nominated"; }
    if($CurrRec['oscar'] == 2) { $CurrRec['award'] = $CurrRec['award'] . " -- Nominated"; }
    if($CurrRec['oscar'] == 3) { $CurrRec['award'] = $CurrRec['award'] . " -- Nominated"; }
    if($CurrRec['oscar'] == 4) { $CurrRec['award'] = $CurrRec['award'] . " -- Nominated"; }
    if($CurrRec['oscar'] == 5) { $CurrRec['award'] = $CurrRec['award'] . " -- Nominated"; }
    if($CurrRec['oscar'] == 6) { $CurrRec['award'] = $CurrRec['award'] . " -- Nominated"; }
    if($CurrRec['oscar'] == 9) { $CurrRec['award'] = "NO AWARD GIVEN"; }

    global $origin;
    if($CurrRec['origintype'] != "") {
        $CurrRec['origin'] = $CurrRec['origintype']." by ".$CurrRec['originauthor'];
    } else {
        $CurrRec['origin'] = "";
    }
}

<!-- CONTENT -->
<TABLE class="format">
<TR class="topBorder"><TD class="dataL Year leftBorder"><b>[blk.year;block=tr+tr+tr+tr+tr;ondata=fct_oscar]</b></TD><TD class="dataL Award rightBorder">[blk.award]</TD></TR>
<TR class="leftBorder"><TD class="dataC Year" rowspan="4"><img src="img/[blk.oscarimg]" border=0 width=25 height=55></TD><TD class="Title rightBorder"><b>[blk.title]</b></TD></TR>
<TR class="leftBorder"><TD class="dataL rightBorder">[blk.writers]</TD></TR>
<TR class="leftBorder"><TD class="dataL rightBorder">[blk.origin]</TD></TR>
<TR class="bottomBorder leftBorder"><TD class="dataL rightBorder">[blk.notes;onformat=noslash;fct=stripslashes;nodata]</TD></TR>
</TABLE>
<!-- /CONTENT -->

Thanks again for your help. We need to make a TBS "Cook Book" one of these days.