|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
knownothing Smarty n00b
Joined: 03 Nov 2015 Posts: 2
|
Posted: Wed Nov 04, 2015 3:55 am Post subject: Help for passing value to function and calling function |
|
|
Hi There,
My website is using smarty 2.6.26. I do not know much about smarty or php.
I need to implement google rich snippet and following is the php file code,
Code: |
function schema($item_id, $best=5, $round=null){
$stats = getStats($item_id, $best, $round); //get rating from DB
//in case of any database related errors
if($stats==0) {echo ' !Error! '; return;}
//dont print snippet if item has less then 1 vote to avoid google error message
if($stats['votes']<1) {echo '<br />item has less then 1 vote'; return;}
//markup template for scheme.org. Edit it as you like.
$template = <<< EOF
<div itemscope itemtype="http://schema.org/CreativeWork">
<a itemprop="url" href="{$stats['url']}">
<span itemprop="name">{$stats['title']}</span>
</a>
<span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
<meta itemprop="worstRating" content = "0">
<span itemprop="ratingValue">{$stats['average']}</span>
out of <span itemprop="bestRating">{$stats['best']}</span>
(based on <span itemprop="ratingCount">{$stats['votes']}</span> ratings)
</span>
</div>
EOF;
echo $template;
}
include_once( 'www.your_site.com/path_to/config.php');
//connect to BD
$conn = @mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn){return;}
else{
$result = @mysql_select_db($db_name);
if(!$result){
echo mysql_error();
return;
}
}
function getStats($item_id, $best=5, $round=null){
GLOBAL $rating_sum, $conn;
//get item statistics
$query = "SELECT mean AS average, votes, title, link AS url, time FROM $rating_sum WHERE id='$item_id'";
$result = @mysql_query($query,$conn);
if(!$result){echo mysql_error(); $stats=0; return;}
elseif($result && mysql_num_rows($result)>0){
$stats = mysql_fetch_array($result);
$stats['url']=urldecode($stats['url']);
}else{
$stats['average']=0;
$stats['votes']=0;
$stats['title']='new item';
$stats['url']='';
$stats['time']=time();
}
$stats['mean']=$stats['average']; //save mean in % from 0 to 100
if($best == null) $best = 5;
if($round == null) $stats['average']=round($best*$stats['mean']/10)/10; //recalculate rating from % to stars and round to first decimal. Output like: 4.3 stars
else $stats['average']=round($best*$stats['mean']/100); //recalculate rating from % to stars and round to int. Output like: 4 stars
$stats['best']=$best;
return $stats;
}
?>
|
I need to put following in template file where rich snippet data should appear,
Code: | <?php
include_once ('www.your_site.com/path_to/snippets.php');
//call the functions
schema($itemID);
?> |
I tried to include this code in {php}{/php} tags but it is breaking my page.
Any help would be much appreciated.
Thanks |
|
Back to top |
|
knownothing Smarty n00b
Joined: 03 Nov 2015 Posts: 2
|
Posted: Thu Nov 05, 2015 6:39 am Post subject: Smarty::PHP_ALLOW |
|
|
Anybody to help, please? I also tried Smarty::PHP_ALLOW but still after calling function my page is breaking. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|