TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

using ondata functions in both main and sub blocks

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: TomH
Date: 2014-11-11
Time: 18:58

using ondata functions in both main and sub blocks

I have main and sub blocks in template like this
                        <table  width="500" border=1 style="border: 1px; margin-left:0px;">
            <tr>
            <td  colspan=1 >

            <!-- [main.#; block=table; ondata=l_event_blk;] -->
            <a href="[main.website_primary;magnet=a;mtype=m+m;]" target="_MFR">[main.name; htmlconv=no;]</a>
            <zzz><br>[main.website_alt; htmlconv=no; noerr;]</zzz>
            </td>   
           
            <td valign=top>

            <itemhere>
           
            <!-- [sub.#; block=itemhere;   ondata=m_event_blk1;  p1=[main.mfg_id];] -->
            [sub.name;  htmlconv=no; noerr;]
            <li>[sub.contacttype;magnet=li;noerr]</li>
            <li>[sub.company;magnet=li;noerr]</li>
            <li style="margin-top:5px; margin-bottom:5px;"><i>territory: [sub.territory;magnet=li;noerr]</i> </li>
            <li>market segment: [sub.marketsegment;magnet=li;noerr] </li>
            <li>phone:[sub.phone;magnet=li;noerr] </li>
            <li>[sub.email_links;htmlconv=no;]</li>   
            <li>[sub.web_links;htmlconv=no;]</li>   

            </itemhere>
           
            </td>
            </tr>

            </table>


The PHP is like this
        $sql = "SELECT * FROM reps WHERE $name_search $company_search ORDER BY name ";
        $TBS->MergeBlock('main', 'mysql', $sql); // or use alternate syntax below to count the rows

        $querymfgs="SELECT * FROM mfgs,mfgs_reps WHERE (rep_id = %p1%) AND mfgs.mfg_id = mfgs_reps.mfg_id ORDER BY mfgs.name";
        $TBS->MergeBlock('sub', 'reps_mfgs', $querymfgs);

$TBS->Show();

// ------------------------------  BELOW is the ondata f() used in the MAIN block  ----------------------------

function l_event_blk($BlockName,&$CurrRec,$RecNum){
// ---------    DIG the websites out and format the result(s)  --------------------   
if($CurrRec['website']!='') {   // ---------------------------------  *******  THIS IS LINE number 316 in the error messaga ---------------
    // test if triggering ondata function at all ;)
    $CurrRec['see_ondata'] = "<hr>Ondata Triggered<hr>";
       
    $pieces = array(0);
    UNSET($pieces);
    settype($websitelist,"ARRAY");  //$CurrRec['email']
    $db_val = str_replace(",", " ", $CurrRec['website']) ;   
   
    $pieces = explode(" ", $db_val);

    foreach ($pieces as $Dkey=>$Dval) {
             $val = rtrim($Dval, ",");
            if( strstr($Dval, "www") ){  // if only need to determine if a needle occurs in haystack strstr in place of preg_match as it is faster
                $websitelist[]=$Dval;
            }
    }
    $CurrRec['website_primary'] = $websitelist[0];
   
    if(count($websitelist)>1){
        $alt_list = array_shift($websitelist); // remove the first (primary) website

        foreach ($alt_list AS $Akey=>$Aval){
        $wlinks.="<a href=\"http://$Aval\" target='_REP'>$Aval</a><br />";
        }
        $wlinks = "Related Websites:<br>".rtrim($wlinks, "<br />");
    } // END if (count()
    $CurrRec['website_alt'] =  $wlinks;
   
    }// END if ($CurrRec['website'])
}
// END function l_event_blk()

// ------------------------------  BELOW is the ondata f() used in the sub block  ----------------------------

function m_event_blk1($BlockName,&$CurrRec,$RecNum){   // syntax for 'ondata'
    $CurrRec['display_descript'] = nl2br($CurrRec['display_descript']);

// ----------   DIG the emails and prepare the result(s)  ------------------   
  if ($CurrRec['email']) {

   $pieces = array(0);
   UNSET($pieces);

    settype($emaillist,"ARRAY");  //$CurrRec['email']
   
    $db_val = str_replace(",", " ", $CurrRec['email']) ;   
   
    // ======= dig out the multiple emails from $result['email'] =====
    $pieces = explode(" ", $db_val);
    
    foreach ($pieces as $key=>$val) {
             $val = rtrim($val, ",");
            //MM if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$', $val)){
            //ZZ
            // if(eregi('http', $val)){
            // if(eregi('www', $val)){
            // if(preg_match("/www/i", $val)){  // the "i" after the pattern delimiter specifies a case INsensiotive search
            if( strstr($val, "@") ){  // if only need to determine if a needle occurs in haystack strstr in place of preg_match as it is faster
                $email_array[] = $val;
                //$email_sub[] = array("single_website"=>$val);
                $emaillist[]=$val;
            }
    }
   
    if(count($emaillist)>1){
        foreach ($emaillist AS $Ekey=>$Eval){
        $elinks.="<a href=\"mailto:$Eval\" target='_email'>$Eval</a><br /> ";
        }
        $elinks = "Email Contacts:<br>".rtrim($elinks, "<br />");
        }
    else{
    $elinks = "Email: <a href=\"mailto:$emaillist[0]\"  target='_email'>$emaillist[0]</a>";
    }
   
   $CurrRec['email_links'] =  $elinks;
    
  }
   // END -- if($CurrRec['email'])
   
   
// ---------    DIG the websites out and format the result(s)  --------------------   
  if ($CurrRec['website']!='') {

   $pieces = array(0);
   UNSET($pieces);
//  $CurrRec['web_link'] = '<a href="http://'.$CurrRec['website'].'" target="_consult">'.$CurrRec['website'].'</a>';

    settype($websitelist,"ARRAY");  //$CurrRec['email']
    settype($website_array,"ARRAY");  //$CurrRec['email']
   
    $db_val = str_replace(",", " ", $CurrRec['website']) ;   
   
    $pieces = explode(" ", $db_val);

    foreach ($pieces as $key=>$val) {
             $val = rtrim($val, ",");
            if( strstr($val, "www") ){  // if only need to determine if a needle occurs in haystack strstr in place of preg_match as it is faster
                $website_array[] = $val;
                $website_sub[] = array("single_website"=>$val);
                $websitelist[]=$val;
            }
    }


    if(count($websitelist)>1){
        foreach ($websitelist AS $Lkey=>$Lval){
        $wlinks.="<a href=\"http://$Lval\" target='_REP'>$Lval</a><br />";
        }
        $wlinks = "Websites:<br>".rtrim($wlinks, "<br />");
        }
    else{
    $wlinks = "Website: <a href=\"http://$websitelist[0]\"  target='_REP'>$websitelist[0]</a>";
    }
   
   $CurrRec['web_links'] =  $wlinks;
        
}
// END if ($CurrRec['website'])

}
// function m_event_blk1() ondata function



The ondata function l_event_blk() in the MAIN block is giving a FATAL error

Fatal error: Cannot use object of type stdClass as array in /home/bdr/public_html/reps_directory_dev.php on line 316

This error message is not helping me understand where to start
((the line 316 in the error message is the line above marked as  ***** in the PHP code))

If I remove the call to the ondata=l_event_blk()
* the error stops
* the main block is visible (without the processing of course)
* the sub block ondata does work as expected


The ondata function m_event_blk() for the SUB block is working as expected



Any help or suggestions would be greatly appreciated
TomH
By: Skrol29
Date: 2014-11-12
Time: 00:03

Re: using ondata functions in both main and sub blocks

Hi TomH,

This error message is quite strange since $CurrRec should be a result of the function mysql_fetch_assoc().
Can you simply do a var_export() on variables $BlockName, $CurrRec, and $RecNum just before the error that holds the error.
if (!is_array($CurrRec)) {
  var_export($BlockName);
  echo "\n<br>\n";
  var_export($CurrRec);
  echo "\n<br>\n";
  var_export($RecNum);
exit;
}
By: TomH
Date: 2014-11-12
Time: 00:16

Re: using ondata functions in both main and sub blocks

Hello Skrol29, thank you for thje reply

Here is the result from your code

'main'
stdClass::__set_state(array( 'mfg_id' => '3592', 'name' => 'RPG Diffusor Systems, Inc. / Topakustik', 'specialty' => 'Acoustical treatments for ceilings; wood acoustic ceiling and wall panels, walls and cabinet doors; microperforated Topperfo-Micro gives the greatest sound absorption with the barely visible perforation. ', 'website' => 'http://www.rpginc.com http://www.topakustik.ch', 'city' => 'Upper Marlboro', 'state' => 'MD', ))
1

What does this say to you?

Thanks,
TomH
By: Skrol29
Date: 2014-11-12
Time: 01:00

Re: using ondata functions in both main and sub blocks

Hi,

It says the record is an object instead of being an array. I cannot explain Why.

Try to add the following line juts before the line that raises the error:
if (is_object($CurrRec)) $CurrRec = get_object_vars($CurrRec);
This should stop the error.

Can you give me your PHP version ?
By: TomH
Date: 2014-11-12
Time: 01:08

Re: using ondata functions in both main and sub blocks

Another bit of info for you...

If the ondata=l_event_blk; is removed from the MAIN block the Fatal Error does not happen

The ondata=m_event_blk1; in the SUB block is working (processing the CurrRec() data from MySQL

But that does NOT allow me to do any ondata= processing of the Mysql data from the MAIN query

That's the problem I was attempting to resolve when adding the ondata=l_event_blk; to the MAIN block produced the Fatal Error

TomH
By: TomH
Date: 2014-11-12
Time: 01:36

Re: using ondata functions in both main and sub blocks

Hooray!

Yes that works around the error (as you knew it would ;) )

Additional to my post sent 1:08 . . .
now that the error produced by the call to the ondata= function in the MAIN block has been bypassed with your patch
BOTH the MAIN ondata call and the SUB ondata call are processing Db data as expected
Thank you!

Will be very interested to hear what you find the cause of the problem (CurrRec being an object there not the expected array)

I'll help/test whatever you would like

PS The server is running PHP Version 5.3.28 (with MySQL Client API 5.6.21 )
PPS I am running TBS 3.9 with TbsSQL 3.2

Again, I thank you for TBS every day, and am very grateful for the support you provide.
TomH