Call of unknown method '_compile_source'. Smarty 3 - php

Hopefully someone can help me with this. I am using smarty within CMSMS and have something called a User Defined Tag running within my page. This contains the following code:
$db = cmsms()->GetDb();
$menu = $smarty->get_template_vars('page');
$user_id = $smarty->get_template_vars('userid');
if (!isset($user_id)) {
$user_id = -1;
}
// Getting menu items from DB
$query = 'SELECT * FROM '. cms_db_prefix() .'module_tools_options
WHERE active = 1 AND user_id = ? AND menu = ?
ORDER BY sort';
$dbresult = $db->Execute($query, array($user_id, $menu));
while ($dbresult && $row = $dbresult->FetchRow()) {
$smarty->_compile_source('preprocess template', $row['title'], $_compiled);
#ob_start();
$smarty->_eval('?>' . $_compiled);
$result = #ob_get_contents();
#ob_end_clean();
echo '<li id="menu_' . $row['option_id'] . '">' . $result . "</li>\n";
}
I have upgraded the CMSMS installation so it now runs smarty 3 and this has broken my page. I get the following error:
/lib/smarty/sysplugins/smarty_internal_templatebase.php: Call of unknown method '_compile_source'.
I guess the Compile Source method has been depreciated in Smarty 3. Can anyone point me in the right direction of its replacement or a method to get this working again?
Many Thanks

Replace:
$smarty->_compile_source('preprocess template', $row['title'], $_compiled);
#ob_start();
$smarty->_eval('?>' . $_compiled);
$result = #ob_get_contents();
#ob_end_clean();
With:
$result = $smarty->fetch('string:'.$row['title']);

Related

Display pho echo list in Descending order

Am displaying list haviing
Date
News Heading
Short Descrption
The list is spread to around 100 pages, having 20 news in each page
Issue is: This is working absolute fine in php 7.3, 7.4 in joomla 3.10 where on clicking url - list is shown spread over multiple pages having sort by date wise as first criteria, latest date of publishing is coming
But when same used on php 8.0.x - its showing incorrectly, where on clicking URL - last page of list having page number 100 is shown first. Now when i add on limitstart=0 in url then its showing correctly as the first page.
Now when i change from descending to ascending - its bringing the content on last page and its opening, but page number is 100 again
Seems like URL when opened is directly taking to last page of news item as published (although no limitstart is mentioned in it), which is incorrect as ideally should open in descending order and open the page having latest one
Below is code of views/list/tmpl/default.php
if(count($this->items) >0){
//$i=1;
foreach($this->items as $newslist)
{
$date = JFactory::getDate($newslist->n_date);
$list .='<h3><strong>'.$newslist->v_heading.'</strong></h3>
<p>'. $date->format('F j, Y').'</p>
<p>'.substr($newslist->v_short_description,0,100).'</p>
<p><i>Know More on:- </i><b><i>'.$newslist->v_heading.'</i></b></p><hr/><br>';
//$i=$i+1;
}
}else{
JError::raiseError(404, "Message");
}
<?php echo $list?>
and for models/list.php this is the function
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query
->select(
$this->getState(
'list.select', 'DISTINCT a.*'
)
);
$query->from('`#__news` AS a');
if (!JFactory::getUser()->authorise('core.edit', 'com_news'))
{
$query->where('a.state = 1');
}
// Filter by search in title
$search = $this->getState('filter.search');
if (!empty($search))
{
if (stripos($search, 'id:') === 0)
{
$query->where('a.id = ' . (int) substr($search, 3));
}
else
{
$search = $db->Quote('%' . $db->escape($search, true) . '%');
$query->where('( a.n_heading LIKE ' . $search . ' )');
}
}
/*
// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering');
$orderDirn = $this->state->get('list.direction');
if ($orderCol && $orderDirn)
{
$query->order($db->escape($orderCol . ' ' . $orderDirn));
}
*/ //Order by date
$query->order ('a.n_date DESC');
$query->order ('a.id DESC');
return $query;
}
This is the code for views/list/view.html.php
public function display($tpl = null)
{
$app = JFactory::getApplication();
$this->state = $this->get('State');
$this->items = $this->get('Items');
$this->pagination = $this->get('Pagination');
$this->params = $app->getParams('com_news');
$this->filterForm = $this->get('FilterForm');
$this->activeFilters = $this->get('ActiveFilters');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
throw new Exception(implode("\n", $errors));
}
$this->_prepareDocument();
parent::display($tpl);
}
Unsure how to achieve in and why its not working in php 8.0 where url should open the 1st page and not last page

Why is Tinybutstrong Mergeblock giving me the error: 'item before ... is neither an object nor an array. Its type is NULL..'

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.
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).

stopping duplicate urls in a form submission

I have a community site, where people can put up pages, and the url generates from their name. e.g. My Business, becomes my-business. I want to create a way, that if there is another My Business, it will check and make the url my-business-2, my-business-3, etc.
function check_url($url) {
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
if(mysqli_num_rows($qry)>0) {
$slugs = array();
while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
if(in_array($url, $slugs)) {
$max = 1;
while(in_array(($url . '-' . ++$max ), $slugs)) $url .= '-' . $max;
}
}
return $url;
}
This is my function, but this still wont work, as if there is a business called My Bus, it will make it my-bus-2, when it would have been unique at my-bus. I have experimented with other functions too, but this one is the closest I got. Can anyone tell me one that works perfectly?
So form my understanding, you are trying to avoid inserting/generating duplicate URL. Output will be something like this -
www.example.com/my-business
www.example.com/my-business-1
www.example.com/my-business-2
www.example.com/my-business-3
...
Try using this function -
function check_url($base_url, $new_url='', $num=0) {
if($new_url == '') {
$new_url = $base_url;
}
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url = '$new_url'");
if(mysqli_num_rows($qry) > 0) {
while($row = mysqli_fetch_array($qry)) {
$num++;
check_url($base_url, $base_url . '-' . $num, $num);
}
}
return $url;
}
Basically, this function will check the database until it finds a valid URL. Each time it will increment the number and recursively call the same function to generate new/valid URL.
Simple and basic!
#SpritsDracula has working code, but his function will query your database each time it executes. That being said, the recursion is a nice concept. Here is a piece of code that will save your the multiple database calls.
function check_url($url) {
$qry = mysqli_query($this->con, "SELECT * FROM businesses WHERE url LIKE '$url%'");
if(mysqli_num_rows($qry)>0) {
$baseUrl = $url;
$slugs = [];
while($row = mysqli_fetch_array($qry)) $slugs[] = $row['url'];
$max = 1;
while(in_array($url, $slugs)) {
$url = $baseUrl . "-" . $max++;
}
}
return $url;
}

How to request MYSQL elements instead of using Array in PHP

I made language php code which working fine. I request the translations from different like en.php, de.php.
In en.php i have one array:
$language = Array('homepage' => 'Site Home','contact' => 'Contact Us');
I use $_SESSION for get the language en, de, hu and the others. I get the language files with this code:
$sql = mysql_query("SELECT * FROM languages ORDER BY langID");
$count = mysql_num_rows($sql);
if ($count) {
while ($ds = mysql_fetch_array($sql)) {
switch ($_SESSION['language']) {
case $ds['tag']:
include_once('language/' . $ds['tag'] . '.php');
break;
default:
include_once('language/en.php');
break;
}
}
}
But I would like to change array for mysql database. In my DB i have a table with:
translatesID
tag(its now the 'en' or can be other like 'de')
key(what i want to be the array like 'homepage')
value(which would be the 'Site Home')
In index.php I get the 'Site Home' with the following php code:
<?php echo $language['homepage']; ?>
My question is which Mysql request or PHP code can solve my request?
Problem solved with this code:
$language = array();
$sql = mysql_query("SELECT * FROM translates WHERE '" . $_SESSION['language'] . "' = tag");
while($row = mysql_fetch_array($sql)) {
$language[$row['key']] = $row['value'];
}

Incorporating next/ previous in my 'foreach statement

I want to have a previous|next link on my page to change pictures.
I use a function to get relevent elements. However, I do not know what additional code is require in my function and where to place it. Also what should be in the html section.
I have looked at many pages on next/previous from 'foreach' but I cannot seem to relate to them.
Code:
function image_data($image_album_id) {
$image_album__id = (int)$image_album_id;
$args = func_get_args();
unset($args[0]);
$fields = '`'.implode('`, `', $args).'`';
$query = mysql_query("SELECT $fields FROM `album_images`
WHERE `image_album_id`=$image_album_id AND `member_id`= '1'");
$ query_result = mysql_fetch_assoc($query);
foreach ($args as $field) {
$args[$field] = $query_result[$field];
}
return $args;
}
Html Page:
Last|
Next
</div>
<?php
$image_album_id =$_GET['image_album_id'];
$image_data = image_data($image_album_id, 'album_id', 'albumname', 'ext', 'timestamp');
echo '';
?>
<td class="smallfont albumthumb2" align="center" valign="middle" >
<img alt="" class="album_cover" src="<?php echo 'images/albums/thumbs/'. $image_data['album_id']. '/'. $image_album_id. '.' .$image_data['ext'];?> " height="175px" width="175px">
</td>
Many thanks. I hope I make sense.
Thanks for the speedy response.
Since there is a lot to look at and digest I thought I would just see if it works.
Alas no.
There is a parse error: syntax error, unexpected
'<' on line
$prev_link = Previous;
The only thing I notice within that section was an extra curly bracket after 'title="$prev_name"}'
I see there is the same for the 'title="$next_name"}'
WIth reference to your specific questions.
I get to the album_viewT page when I click on a link in a previous page. This contains tiny thumbnails. The link being localhost/Testing/album_view.php?artist_id=4&image_album_id=4 as an example.
Not sure if I fully understand "order of date is by image_album_data
Yes there are almost 3,000 rows in the database.
I should also mention that album_id has been replaced by artist_id.
Should the href be changed to "album_view.php/id/...
Your question lacks some data i.e. what is the page you are visiting that produces this code (the URL that is) and how is your data sorted. For instance I can see that you are providing potentially more information using the album_viewT.php? script but that is not necessarily the one that displays the HTML that you have produced.
So after all this I will make some assumptions and hopefully that will give you the right guidance to get to the solution.
I am assuming that your visiting page is http://mysite.com/albums/id/25
I am also assuming that the order of the data is by image_album_id.
Finally I am assuming that you have data in your db and I don't need to check whether there are returned data in the template for the current image. You will need to sort that out yourself.
You will need to get the data first from the database:
function image_data($image_album_id)
{
$results = array(
'previous' => FALSE,
'current' => FALSE,
'next' => FALSE,
);
$image_album__id = (int)$image_album_id;
$args = func_get_args();
unset($args[0]);
$fields = '`'.implode('`, `', $args).'`';
// Current record
$results['current'] = get_data(
$fields,
"AND image_album_id = {$image_album_id}"
);
// Previous - no need to run this if we don't have a current
if ($results['current'])
{
// Current record
$results['previous'] = get_data(
$fields,
"AND image_album_id < {$image_album_id} " .
"LIMIT 1"
);
}
// Next - no need to run this if we don't have a current
if ($results['current'])
{
// Current record
$results['next'] = get_data(
$fields,
"AND image_album_id > {$image_album_id} " .
"LIMIT 1"
);
}
// If all went well the $results array will contain the data
// for the 3 records. If we are at the beginning or the end,
// the previous and/or next will be FALSE
return $results;
}
function get_data($fields, $where = '')
{
$return = FALSE;
// Template
$template = "SELECT %s "
. "FROM album_images "
. "WHERE member_id = 1 %s";
// Current record
$sql = sprintf($template, $fields, $where);
$query = mysql_query($sql);
$query_result = mysql_fetch_assoc($query);
// If data has been found
if ($query_result)
{
$return = $query_result;
}
return $return;
}
For your HTML page:
<?php
$image_album_id = $_GET['image_album_id'];
$image_data = image_data(
$image_album_id,
'album_id', 'albumname', 'ext', 'timestamp'
);
$prev_link = '';
$next_link = '';
if ($image_data['previous'])
{
$prev_id = $image_data['previous']['album_id'];
$prev_name = $image_data['previous']['albumname'];
$prev_link = <a href="/albums/id/{$prev_id}" title="$prev_name"}>Previous</a>";
}
if ($image_data['next'])
{
$next_id = $image_data['next']['album_id'];
$next_name = $image_data['next']['albumname'];
$next_link = <a href="/albums/id/{$next_id}" title="$next_name"}>Next</a>";
}
$curr_id = $image_data['current']['album_id'];
$curr_ext = $image_data['current']['ext'];
?>
<?php echo $prev_link; ?>|<?php echo $next_link; ?>
</div>
<td class="smallfont albumthumb2" align="center" valign="middle">
<a href="album_viewT.php?images/albums/thumbs/
<?php echo "{$curr_id}/{$image_album_id}.{$curr_ext}; ?>
<img alt="" class="album_cover"
src="images/albums/thumbs/
<?php echo "{$curr_id}/{$image_album_id}.{$curr_ext}"; ?>
height="175px"
width="175px" />
</a>
</td>
Note: I have split the line for the img and a tags in the HTML file for clarity.

Categories