Script in function won't echo
When I try to echo
<script type='text/javascript'>
function rotate_$num(number) {
num = number%$i;
$('.$num').fadeOut('normal');
$('#'+(num+1)).fadeIn('normal');
number++;
setTimeout('rotate_$num('+number+')',300);
}
</script>
from inside a function in PHP it doesn't echo out, but when I change <script>
to <div>
it works. Also, it works when the echo is just in a php file with nothing else but the echo in it, it works.
Here's the entire function:
function rename_name($name) {
return str_replace(' ', '', $name);
}
function pane($result) 开发者_运维问答{
if($result['title']) {$title = ': '.$result['title'];}
$name = rename_name($result[name]);
echo "<div class='pane $name'>
<h2>$result[name]$title</h2>";
if (is_array($result['data'])) {
$num = rand(0, 9999);
foreach($result['data'] as $option) {
$i++;
if($i == 1) $display = 'block'; else $display = 'none';
echo "<div id='$num$i' class='$num' style='display: $display;'><h3>$option[data]<span class='whats'>$option[kind]</span></h3></div>";
}
//The below will echo when script is changed to div, why should it matter?
echo "<script type='text/javascript'> function rotate_$num(number) { num = number%$i;
$('.$num').fadeOut('normal');
$('#'+(num+1)).fadeIn('normal');
number++;
setTimeout('rotate_$num('+number+')',300);
}
</script>";
//The above will echo when script is changed to div, why should it matter?
}
else {
echo "<h3>".$result['data']."<span class='whats'>$result[kind]</span></h3>";
}
echo "</div>";
}
And here is the $result array being passed to it
Array
(
[name] => Mint
[type] => 0
[data] => Array
(
[0] => Array
(
[data] => 67
[kind] => hits today
)
[1] => Array
(
[data] => 24
[kind] => unique hits today
)
[2] => Array
(
[data] => 158351
[kind] => hits total
)
[3] => Array
(
[data] => 17826
[kind] => unique hits total
)
)
)
I put in the exact same array, and got the expected output.
Are you fetching this data via AJAX then placing it on the page? Javascript cannot write script tags to the DOM (at least, not via innerHTML
) and will therefore simply ignore them.
If this AJAX data, it looks like you're using lots of unnecessary bandwidth to make this transfer, especially by transferring what is essentially the same script over and over. I'd recommend transferring just the data (either in HTML or, if you're feeling really cool, in JSON then having the client run the template), then letting the original script handle the rotation. Not only will it actually work, but it'll also come out cleaner and more manageable. After all, if I were working on this app and wanted to change how pane rotation works, this is not where I'd look first for that particular snippet of code.
I'm not sure if this is the problem, but I try not to let strings span multiple lines like you've done with your echo statement. Try using heredoc notation, like this.
$javascript = <<EOT
<script type='text/javascript'> function rotate_$num(number) { num = number%$i;
$('.$num').fadeOut('normal');
$('#'+(num+1)).fadeIn('normal');
number++;
setTimeout('rotate_$num('+number+')',300);
}
</script>
EOT;
echo $javascript;
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
精彩评论