Categories > TinyButStrong general >

Can't Get Simple Custom Function to Work

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Maz
Date: 2006-04-14
Time: 01:52

Can't Get Simple Custom Function to Work

Code:

$pending = array();

$query = "
    SELECT
        p.product_title,
        p.review_state,
        p.product_date,
        p.owner_id,
        r.state_id
    FROM
        ".INV_PREFIX."products p
    LEFT JOIN
        ".INV_PREFIX."review_states r
    ON
        p.review_state=r.state_id
    WHERE
        p.owner_id = '".$mbr_config['id']."'
    ";

    $rst = $db->get_results($query, ARRAY_A);
   
    if($db->num_rows > 0)
    {
        $pending = $rst;
    }


$TBS->LoadTemplate('content/modules/store/product-status');
$TBS->MergeBlock('pending',$pending);
$TBS->Show();


function event_pending($BlockName,&$CurrRec,$RecNum)
{
    if ($CurrRec['state_id']==1)
    {
          $CurrRec['state_id'] = '<b>Pending Review</b>';
    }
}

Template:

        <table class="etched" summary="" id="pending">
            <tr>
                <th>ID</th>
                <th>Product</th>
                <th>Date</th>
                <th>Status</th>
            </tr>
            <tr>
                <td class="dark">[pending.#]</td>
                <td class="dark">[pending.product_title;noerr;block=tr;ondata=event_pending]</td>
                <td class="dark">[pending.product_date;frm='ddxx mmm yyyy hh:nn']</td>
                <td class="dark">[pending.state_id]</td>
            </tr>
            <tr>
                <td class="light" colspan="3">[pending;block=tr;nodata][lang.cell_no_results]</td>
            </tr>
            <tr>
                <td class="light">[pending.#]</td>
                <td class="light">[pending.product_title;noerr;block=tr;ondata=event_pending]</td>
                <td class="light">[pending.product_date;frm='ddxx mmm yyyy hh:nn']</td>
                <td class="light">[pending.state_id]</td>
            </tr>
        </table>

For some reason I can't get this simple function to work. It's like it's being completely ignored, as nothing at all is happening.
By: TomH
Date: 2006-04-14
Time: 02:27

Re: Can't Get Simple Custom Function to Work

Everything in the TBS side looks okay -- but I cannot test the PHP database query stuff

Need more info to be able to help

?? Does the script work correctly without the 'ondata' parm like this
<td class="dark">[pending.product_title;noerr;block=tr]</td>

If not, get that working first -- then add in the ondata func

By: Maz
Date: 2006-04-14
Time: 02:33

Re: Can't Get Simple Custom Function to Work

Sure, the array is populated perfectly:

Array
(
    [0] => Array
        (
            [product_title] => Midnight Beta 2
            [review_state] => 4
            [product_date] => 1135292972
            [owner_id] => 1
            [state_id] => 4
        )

    [1] => Array
        (
            [product_title] => Subway 2.01
            [review_state] => 4
            [product_date] => 1135036448
            [owner_id] => 1
            [state_id] => 4
        )

    [2] => Array
        (
            [product_title] => Arctic
            [review_state] => 4
            [product_date] => 1137133310
            [owner_id] => 1
            [state_id] => 4
        )

    [3] => Array
        (
            [product_title] => Blueshell 1.0
            [review_state] => 4
            [product_date] => 1138204157
            [owner_id] => 1
            [state_id] => 4
        )

    [4] => Array
        (
            [product_title] => Test Product 2
            [review_state] => 4
            [product_date] => 1140244725
            [owner_id] => 1
            [state_id] => 4
        )

    [5] => Array
        (
            [product_title] => A Test Mod
            [review_state] => 1
            [product_date] => 1144967645
            [owner_id] => 1
            [state_id] => 1
        )

)

and the data appears in the tabe fine. It's just the event_function which does't appear to be functioning.

I realised after I posted the original, that, according to the example, I needed to make a seperate field for event result:

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

    if ($CurrRec['state_id']==1) $CurrRec['state_level'] = '<b>Pending Review</b>' ;;
}

            <tr>
                <td class="dark">[pending.product_title;noerr;block=tr;ondata=event_pending]</td>
                <td class="dark">[pending.product_date;frm='ddxx mmm yyyy hh:nn']</td>
                <td class="dark">[pending.state_level;noerr;htmlconv=no]</td>
            </tr>
            <tr>
                <td class="light" colspan="4">[pending;block=tr;nodata][lang.cell_no_results]</td>
            </tr>
            <tr>
                <td class="light">[pending.product_title;noerr;block=tr;ondata=event_pending]</td>
                <td class="light">[pending.product_date;frm='ddxx mmm yyyy hh:nn']</td>
                <td class="light">[pending.state_level;noerr;htmlconv=no]</td>
            </tr>

But that hasn't solved it either. It's almost as if the function isn't being called at all.
By: TomH
Date: 2006-04-14
Time: 02:37

Re: Can't Get Simple Custom Function to Work

Try in function

if ($CurrRec['state_id']==1) $newvar = '<b>Pending Review</b>';

and in html
[var.newvar;block=tr;ondata=event_pending]
By: TomH
Date: 2006-04-14
Time: 02:40

Re: Can't Get Simple Custom Function to Work

Ooops

Reminder - data coming from database is thpe CHAR

Try adding quotes in IF test  "1"
if ($CurrRec['state_id']=="1") $newvar = '<b>Pending Review</b>';
By: Maz
Date: 2006-04-14
Time: 02:42

Re: Can't Get Simple Custom Function to Work

Bizarre!

TinyButStrong Error (Merge PHP global variables): Can't merge [var.newvar] because there is no PHP global variable named 'newvar'. This message can be cancelled using parameter 'noerr'.

So I might be right; it's not calling the function... Weird.
By: TomH
Date: 2006-04-14
Time: 02:47

Re: Can't Get Simple Custom Function to Work

My error
Believe you were correct with originally

if ($CurrRec['state_id']==1) $CurrRec['state_level'] = '<b>Pending Review</b>' ;

But check the ( ... =="1" )

By: Maz
Date: 2006-04-14
Time: 02:51

Re: Can't Get Simple Custom Function to Work

Checked with and qithout quotes, still the same (The db value is an INT)

Anyway, I think I may have spotted the problem, and it's almost embarrassing. I'm using Version  : 2.02.4. I see that the ondata for blocks wasn't introduced until 2.0.5:

"2.05      2005-08-31     
New feature: Parameter 'ondata' for blocks.
This parameter executes a custom user function each time a record is fetched from the data source. The difference with 'onsection' is that 'onsection' function is called each time a section is displayed. An 'ondata' function allows to modify the current record only once for any sections and also to calculate columns that can be used for header sections."
By: Maz
Date: 2006-04-14
Time: 03:00

Re: Can't Get Simple Custom Function to Work

Er, ignore what I just stated. That makes no sense at all. My verson is higher! lol Damn, I need to read...
By: edavison
Date: 2008-05-15
Time: 19:25

Re: Can't Get Simple Custom Function to Work

For those that might get help from this as well, I spent hours trying to get ondata to work with version 2.05.8 which should have proper support for it.  My symptoms were the same as this post.  I normally do not use a version this old but it took many hours and them forum searching before I checked the version number. I downloaded the latest, 3.3.0, and it worked!

Lesson to learn is to always check for the latest version before beginning development.