I am using Smarty with this settings:
$smarty = new Smarty;
$smarty -> caching =3600;
$smarty -> compile_check =true;
$smarty -> compile_dir = 'theme/compile/';
$smarty -> config_dir = 'theme/libs/';
$smarty -> cache_dir = 'theme/cache/';
$smarty -> plugins_dir = 'theme/libs/plugins/';
$smarty->left_delimiter = '{';
$smarty->right_delimiter = '}';
$smarty -> clear_compiled_tpl();
I want to program a simple visitors counter with this function :
function counter() {
$ip = $_SERVER['REMOTE_ADDR'];
$now = time();
$y1 = jgmdate("Y ", $now);
$y1 = (int) $y1;
$m1 = jgmdate("m", $now);
$m1 = (int) $m1;
$d1 = jgmdate("d", $now);
$d1 = (int) $d1;
$result3 = mysql_query("SELECT `times`,`id` FROM `stat_ip` where `IP`='$ip' AND `year`='$y1' AND `month`='$m1' AND `day`='$d1' ;");
unset($n3);
$n3 = (int) mysql_num_rows($result3);
echo $n3;
if ($n3 == 0) {
mysql_query("INSERT INTO `stat_ip` (`id` ,`IP` ,`year` ,`month` ,`day`) VALUES (NULL , '$ip', '$y1', '$m1', '$d1') ;");
} else if ($n3 == 1) {
$row3 = mysql_fetch_array($result3);
$s = (int) $row3['times'] + 1;
mysql_query("UPDATE `stat_ip` SET `times` = '$s' WHERE `id` = '".$row3['id']."' ;");
} else {
echo("error");
}
}
everything is ok , but my query executes more than a time in this line(Probably all queries) :
mysql_query("UPDATE `stat_ip` SET `times` = '$s' WHERE `id` = '".$row3['id']."' ;") ;
I think smarty has something to do with my problem!
when I write this code :
$q=$smarty -> fetch('index.tpl');
the query executes 1 time however when I change my code to :
$q=$smarty -> fetch('index.tpl');
echo $q;
or
$smarty -> display('index.tpl');
my queries execute more than one time!:(
for more information:
http://www.smarty.net/forums/viewtopic.php?p=81161
A Common mistake is to have an empty img-tag or script-tag in your html output - this makes the browser re-request the current page.
Why are you calling $smarty->clear_compiled_tpl();?
It does delete all compiled template and cause a recompilation each time a page is called.
Remove this line.
Where and how did you call for function counter?
my problem is solved :
I was using slide show that has this line in java script codes :
I changed the src="#" to src="" at jquery.nivo.slider.js
And it works fine now however I really don't know why browsers work funny !!!
thank you
Smarty is just a template engine, it shouldn't cause problems with the database. Your problem is likely somewhere outside the template engine.
Related
So I'm trying to fetch a points table for users to add points in by garnering their total points and adding it with the installation points, however by trying to fetch their "latest" points, new users who do not have an existing row in the table will throwback an error "InvalidArgumentException; Data Missing". This would be my code below, and are there any other ways around this?
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
$points['points_total'] = $currentpoint + $battery['point_installation'];
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first(); of your code return an object and you are try to perform math (addition) operation on that object which is not possible. You can update your code like below.
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
if($currentpoint != null)
{
$points['points_total'] = $currentpoint->points_total + $battery['point_installation'];
}
else {
$points['points_total'] = $battery['point_installation'];
}
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
I have this code, in which i have a if statement inside while loop, the problem is that i can't make it work because it always returns the same comment ID after the if statement is accomplished. I couldn´t found a solution. I'd appreciate any help. Thank you.
while($fila = mysql_fetch_assoc($resultado)) {
$archivos = $fila['archivos'];
$fecha = $fila['fecha'];
$comentarioID = $fila['id'];
$comentario = $fila['comentario'];
if(strlen($comentario) > 100){
$comentario = substr($comentario, 0, 100);
$vermas = '...Ver mas';
}
echo $comentario.$vermas;
}
First off MySQL is deprecated. Use MySQLi. You should also define $vermas outside of the if() statement, because if the strlen is less than or equal to 100 characters, then $vermas will be left undefined.
Try this...
while($fila = mysqli_fetch_array($resultado)) {
$archivos = $fila['archivos'];
$fecha = $fila['fecha'];
$comentarioID = $fila['id'];
$comentario = $fila['comentario'];
if(strlen($comentario) > 100){
$comentario = substr($comentario, 0, 100);
}
$vermas = "...<a href='' class='vermas' id='vermas_".$comentarioID."'>Ver mas</a>";
echo $comentario.$vermas;
}
I have the following variables in my cart.php file:
<?php $readyTotal = $qty*price ?>;
Now I want to do is that if the $readyTotal > $1000 then call a file which is adding an item into the customer's cart.
($slq= msyql_query("INSERT into `table`.....value.....");
and if the value of $radyTotal < $1000;
dont add any extra item or if it has been added just remove it by calling the removal file.
($sql =mysql_query("DELETE from `table` where .......");
Please note that how to do this I want to call/include the files in my cart.php file. but i am unable to do this. is there any other way to do it or by using ajax/jquery which check every the value of $readyTotal every time when it changes.
function ad(){
$signature =getSignature();
$item = 21;
$type = 100;
$type= 0;
$sql2 = mysql_query("Select * From `temp` where `item`='$item' AND `price`= 0 AND
`signature`='$signature' AND `type`=0 AND `toppings`=0 " );
$count= mysql_num_rows($sql2);
if($count ==0){
$sql = mysql_query("INSERT INTO `temp`
(`item`,`price`,`signature`,`type`,`toppings`, `isCoupon`)
VALUES
('$item','$type','$signature','0','0', '1')");
}
}
function removeMe(){
mysql_query("DELETE FROM `temp` WHERE `item`=21 AND `signature`='".getSignature()."'");
}
When I try the above function individual file it works fine, but when i use both in it does not woks please.... what is wrong with this...
<?php
include_once("customfunctionfile.php");
$object = new quiz;
$readyTotal = $qty*price ;
if($readyTotal > 1000){
$object->ad();
}else{
$object->removeMe();
}
?>;
customfunctionfile.php
class quiz{
function ad(){
$signature =getSignature();
$item = 21;
$type = 100;
$type= 0;
$sql2 = mysql_query("Select * From `temp` where `item`='$item' AND `price`= 0 AND
`signature`='$signature' AND `type`=0 AND `toppings`=0 " );
$count= mysql_num_rows($sql2);
if($count ==0){
$sql = mysql_query("INSERT INTO `temp`
(`item`,`price`,`signature`,`type`,`toppings`, `isCoupon`)
VALUES
('$item','$type','$signature','0','0', '1')");
}
}
function removeMe(){
mysql_query("DELETE FROM `temp` WHERE `item`=21 AND
`signature`='".getSignature()."'");
}
}
You can achieve this something like this using functions without including file.
<?php
$readyTotal = $qty*price;
if($readyTotal > 1000) //considering amount in $
{
addItemToCart($cartDetails) // call function to add item into the cart
}
else {
removeItemFromCart($cartDetails) //call function to remove item from cart
}
?>
Define functions addItemToCart and removeItemFromCart to fire your queries.
I use FOSElasticaBundle in my project to search a list of ads. I can get all the result but I can't order it in "asc" or "desc". I have seen different tutorial speaking about Filtering. But it's not working.
if (!empty($cati)) {
$query = new \Elastica\Query\Bool();
if((!empty($cati)) && $cati!='1')
{
$query1 = new \Elastica\Query\Match();
$query1->setFieldQuery('post.cat_id', $cati);
$query->addMust($query1);
}
}
else {
$query = new \Elastica\Query\MatchAll();
}
$elasticaQuery = new \Elastica\Query();
$elasticaQuery->setQuery($query);
$elasticaQuery->setSize($nbPerPage);
$elasticaQuery->setFrom(($page - 1) * $nbPerPage);
$elasticaQuery->addSort(array('date', array("desc"));
$repistoryManager = $this->container->get('fos_elastica.manager');
$repistory = $repistoryManager->getRepository('AdsManagerBundle:Post');
$eq = new \Elastica\Query();
$eq->setQuery($query);
$finder = $this->container->get('fos_elastica.index.ads.post');
$elasticaResultSet = $finder->search($eq);
$ed = $elasticaResultSet->getResults();
I can't get where the problem is!
You did in a mistake in your code (parse error + bad construction of the JSON syntax):
$elasticaQuery->addSort(array('date' => 'desc'));
I created file of code working perfect ... but problem is I added the file into my website header {include file="header_featured.html"} It is not displaying any thing ... But if write the url in browser with its .php file name like www.domain.com/header_featured.php it shows results what is the problem ...? even all of the other files of cms/script also included in header and displaying perfect results ..
header_featured.php
<?php
require_once "include/include.php";
global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);
$ads = array();
$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50");
while ($temp = mysql_fetch_assoc($featured_ads)) {
$record = array();
$record['ad_id'] = $temp['id'];
$record['ad_title'] = $temp['title'];
//check for image if featured ad have image (Need to fetch only single image)
$featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no");
$img = mysql_fetch_assoc($featured_ads_images);
$record['img_id'] = $img['id'];
$record['ad_img'] = $img['picture'];
$record['img_folder'] = $img['folder'];
$ads[] = $record;
}
$smarty->assign('ads', $ads);
$db->close();
if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }
$smarty->display('header_featured.html');
close();
?>
header_featured.html
{foreach item=item from=$ads}
{$item.ad_id}
{$item.ad_title}
{$item.img_id}
{$item.ad_img}
{$item.img_folder}
{/foreach}
My way of including the file into header
{include file="header_featured.html"}
It seems you should simple in your PHP code write:
<?php include ('header_featured.php'); ?>
to display your content.
However in fact you should move the smarty part to index.php, include header_featured.php file and in index.php display Smarty template.
For example I would do it it this way:
index.php
<?php
require_once "include/include.php";
global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);
$header_featured = '';
require('header_featured.php');
$db->close();
if($db->error!='') {
$db_error = $db->getError();
$smarty->assign('db_error',$db_error);
}
else {
$header_featured = $smarty->fetch('header_featured.tpl');
}
$smarty->assign('header_featured', $header_featured );
$smarty->display('index.tpl');
?>
header_featured.php
<?php
$ads = array();
$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50");
while ($temp = mysql_fetch_assoc($featured_ads)) {
$record = array();
$record['ad_id'] = $temp['id'];
$record['ad_title'] = $temp['title'];
//check for image if featured ad have image (Need to fetch only single image)
$featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no");
$img = mysql_fetch_assoc($featured_ads_images);
$record['img_id'] = $img['id'];
$record['ad_img'] = $img['picture'];
$record['img_folder'] = $img['folder'];
$ads[] = $record;
}
$smarty->assign('ads', $ads);
index.tpl
<html>
<head>
....
<body>
{$header_featured}
...
</body>
</html>
header_featured.tpl
{foreach item=item from=$ads}
{$item.ad_id}
{$item.ad_title}
{$item.img_id}
{$item.ad_img}
{$item.img_folder}
{/foreach}