0

I am trying to use Tinybutstrong to merge a template with an sql query result. Below is the PHP code being run, the custom database reader plugin I am attempting to use, the template file and the error message.... The code is being run from within a wordpress site, hence the use of the wpdb object and the custom database reader plugin.

The code being run:

        include_once('wp-content/themes/mine/tbs_plugins/tbsdb_wpdb.php');      
        $link = new wpdb($username, $password, $dbname, $servername);
        $sql_query = 'select year from mytable limit 10';
        $TBS = new clsTinyButStrong;
        $TBS->LoadTemplate('wp-content/themes/mine/templates/mytemplate.htm');  
        $TBS->MergeBlock('blk1', $link, $sql_query);
        $TBS->Show();

Custom database reader plugin tbsdb_wpdb.php:

    <?php
function tbsdb_wpdb_open(&$source,&$query) {
    $source->get_results($source->prepare($query),ARRAY_A);
    return $source;
}

function tbsdb_wpdb_fetch(&$Rs,$num) {
    if ($num<=$Rs->num_rows) {
      return $Rs->get_row(null,ARRAY_A,$num-1) ;
    }
    else {
        return False ;
    }
}

function tbsdb_wpdb_close(&$source) {
    // not needed
}
?>

Part of Template mytemplate.htm:

.
.
.
    [blk1;block=begin]
    <table><tr><td align="center">[blk1.year]</td></tr></table>
    [blk1;block=end]
.
.
.

Error message:

TinyButStrong Error in field [blk1.year...]: item before 'year' is neither an object nor an array. Its type is NULL. This message can be cancelled using parameter 'noerr'.

I have listed out the results from the query in a for loop as follows:

$rows = $link->get_results($sql_query);
    echo "<table>";
foreach ($rows as $obj) :
    echo "<tr><td>" . $obj->Year . "</td></tr>";
endforeach;
echo "</table>";

this gives me a correct result but when using TBS class/Loadtemplate/Mergeblock/Show... no joy, I get the error message... Any ideas would be appreciated.

M B
  • 1

1 Answers1

0

Solved it myself by modifying the tbsdb_wpdb.php to:

function tbsdb_wpdb_open(&$source,&$query) {
    global $link;
    global $sql_query;
    return $link->get_results($sql_query,ARRAY_A);
}

function tbsdb_wpdb_fetch(&$Rs,$num) {
    global $link;
    global $sql_query;
    if ($num<=$link->num_rows) {
      return $link->get_row($sql_query,ARRAY_A,$num-1) ;
    }
    else {
      return False ;
    }
}

So essentially my global declarations were missing (or rather, in the wrong place).

M B
  • 1