Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top