The following code is a method to grab the number of rows from a server.
<?php
//retrieving number of rows from server
$num_check = $handler->query("SELECT * FROM table");
$num_to_display = $num_check->rowCount();
?>
I'm using a counter widget written in Javascript to provide a customer facing counter that displays the number of rows found (numbers of entry in the table). The counter was written by chap named Chris Nanney. The section of the source that I am using is below.
<script>
$(function(){
//changed value to be a PHP variable.
var myCounter = new flipCounter('myCounter', {value: myVar, inc: 0, pace: 1000, auto: true});
$('#style-switcher a').on('click', function(e){
e.preventDefault();
$("#myCounter").removeClass().addClass('flip-counter '+$(this).data('style'));
$('#style-switcher a').removeClass('active');
$(this).addClass('active');
});
//the following lines I probably don't need and will delete. But as they are in use now, I felt it necessary to include them
$('#inc_slider').slider({
range: 'max',
value: 123,
min: 0,
max: 1000,
slide: function(event, ui){
$('#inc_value').text(ui.value);
myCounter.setIncrement(ui.value);
if (ui.value == 0) myCounter.setAuto(false);
else myCounter.setAuto(true);
}
});
// Pace
$('#pace_slider').slider({
range: 'max',
value: 1000,
min: 400,
max: 2000,
step: 100,
slide: function(event, ui){
myCounter.setPace(ui.value);
$("#pace_value").text(ui.value);
}
});
});
</script>
The trouble I am running into is changing the PHP variable $number_to_display to a Javascript variable that can be parsed by the parameters of the function specifically the value field. Some debugging showed that the number echoes properly from the PHP end so I doubt the PHP is the issue. More likely the issue is in converting it to Javascript. So far I have attempted to change it over to JS within the function itself, and some treasure hunting on other forums gave me the following stub which was my most recent attempt at passing it.
<script language="javascript">
/ <![CDATA[
var myVar = "<?php echo $num_to_display; ?>;""
alert(myVar);
// ]]>
</script>
Granted I'm not really JS savvy, I was wondering if anyone has some best practices for moving between PHP and JS. Am I not properly declaring the variable or passing it properly to the function?
EDIT:
Ok, thanks to the great help so far, I have the varibales parsing correctly. My question is, when I pass it to the function
$(function(){
var myCounter = new flipCounter('myCounter', {value: myVar, inc: 0, pace: 1000, auto: true});
It still is not passing in properly. Am I missing single or double quotation marks or something like that?
"character at the end of the linevar myVar = "<?php echo $num_to_display; ?>;""a typo?