I made a code for a moving text in a mask. A tried to change the text randomly but after one change...the script works, but the text remains the same. The mt_rand() function works just once. What is the problem in this script?!
$(document).ready(function() {
<?php
$rid = mt_rand(1,10);
echo "$(\"#moving_fact\").text(\"".$rowf[$rid]["fact"]."\");";
?>
var pos=$("#moving_fact").position();
var width=$("#moving_fact").width() + 50;
pos.left= 800;
$("#moving_fact").css({left: pos.left});
setInterval(function() {
if (width + pos.left > 0) {
pos.left= pos.left - 1;
$("#moving_fact").css({left: pos.left});
} else {
pos.left = 800;
$("#moving_fact").css({left: pos.left});
width = $("#moving_fact").width() + 50;
<?php
$rid = mt_rand(1,10);
echo "$(\"#moving_fact\").text(\"". $rowf[$rid]["fact"] ."\")";
?>
}
},10);
})
You can't use PHP
to execute with JavaScript
directly! PHP
is server-side and JavaScript
is client-side.
In that case just use JavaScript
to generate random value.
PHP
is server side and javascript
is client side so you cannot use PHP function in javascript You have to use javascript function Math.random()
.
Math.floor((Math.random() * 10) + 1);