My code is a website counter (counter.php). It works perfectly when run directly, meaning
each time you refresh the page the counter increments starting from 5754; however, when the counter.php is included in another page containing other jQuery plugins, it stops working. The counter is static then; it will show 5754 all the time.
Please help me and clarify where I am going wrong.
This section is where the incrementation (counting of the visitors) takes place:
<?php
session_start();
if(isset($_SESSION['executed']))
$_SESSION['executed'] = $_SESSION['executed']+ 1;
else
$_SESSION['executed'] = 5754;
?>
The page itself, which is self explanatory:
<head>
<script src="counter/js/jquery-1.7.1.j"> type="text/javascript"></script>
Where the counter leaves:
<div class="counter-wrap">
<div class="counter-number">
</div>
</div>
<div>
An inline CSS:
<style>
.counter-wrap {
height:18px;
overflow:hidden;
}
.counter-number {
height:198px;
width:12px;
position:relative;
background-image:url(http://developer.mindtouch.com/@api/deki/files/4548/=counter_ticker_bg.gif);
float:left;
}
</style>
And now the jQuery code:
<script type="text/javascript">
$(".counter-number").each( function(i) {
$(this).attr('id','num'+i);
});
function loadinput() {
var newval = $("#numgo").val();
loadticker(newval);
}
function loadticker(ticnum) {
var fticnum = add_commas(ticnum);
var numheight=18;
addticker(fticnum);
if (ticnum && ticnum != 0) {
var s = String(fticnum);
for (i=s.length;i>=0; i--)
{
var onum=s.charAt(i);
$("#num"+i).attr('value',onum);
}
$(".counter-number").each( function() {
var nval=$(this).attr("value");
if (!isNaN(nval)) {
var nheight = Number(nval)*numheight*-1;
$(this).animate({ top: nheight+'px'}, 1500 );
}
if (nval==','){
$(this).animate({ top: '-180px'}, 1500 );
}
});
}
}
function addticker(newnum) {
var digitcnt = $(".counter-number").size();
var nnum = String(newnum).length;
var digitdiff = Number(nnum - Number(digitcnt));
if (digitdiff <0) {
var ltdig = (Number(nnum)-1);
$(".counter-number:gt(" + ltdig + ")").remove();
}
for(i=1;i<=digitdiff;i++) {
$(".counter-wrap").append('<div class="counter-number" id="num' + (Number(digitcnt+i-1)) + '"> </div>');
}
}
function add_commas(nStr) {
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
</script>
</head>
Here is where the function is called:
<body onload="loadticker( '<?php echo $_SESSION['executed']; ?>' ) ">
</body>
</html>
It doesn’t seem to me that it’s a jQuery problem. I would suggest trying to add your PHP code at the top of your code, even before you
<html>opening tag.Try this: