I get the error message syntax error missing ; before statement at "var galleryarray=new Array();" . "\n"; here is the php code
function returnimages($dirname=".") {
$pattern="\.(jpg|jpeg|png|gif|bmp)$";
$files = array();$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
echo 'galleryarray[' . $curimage .']=["' . $file . '"];' . "\n";
$curimage++;
}
}
closedir($handle);
}
return($files);
}
echo "var galleryarray=new Array();" . "\n";
returnimages();
and here is the javascript:
var galleryarray=new Array();
var curimg=0
function rotateimages(){
document.getElementById("slideshow").setAttribute("src", "slideshow_images/"+galleryarray[curimg])
curimg=(curimg<galleryarray.length-1)? curimg+1 : 0
}
window.onload=function(){
setInterval("rotateimages()", 2500)
}
i just don't see my mistake any help with this problem would be appreciated
klein
Replace your below line:
echo "var galleryarray=new Array();" . "\n";
with the following line:
echo "<script>var galleryarray=new Array();</script>";
You are adding a JS code without script tag so you need to add this tag.
EDITED:
echo 'galleryarray[' . $curimage .']=["' . $file . '"];' . "\n";
You also have error in the above line replace it with the below line:
echo '<script>galleryarray["' . $curimage .'"]=["' . $file . '"];</script>';
Related
I'm trying to recursively link some Javascript files in a directory into a HTML page:
<?php
$dir = '/opt/lampp/htdocs/my_project/js';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST );
foreach ( $iterator as $path ) {
if ($path->isDir()) {
print('dir >>> ' . $path->__toString() . PHP_EOL . '\n');
} else {
print($path->__toString() . PHP_EOL . '\n\n\n');
echo("<script src='$path->__toString() . PHP_EOL>\</script>");
}
}
?>
This, however, doesn't seem to work. How can I go about adding all the files in the directory and sub-directories recursively? Any working recursive approach will do. It doesn't have to be the way I'm trying it above.
Thank you all in advance.
Try changing your foreach block to below code and see if that helps?
foreach ( $iterator as $path ) {
if ($path->isDir()) {
print('dir >>> ' . $path . PHP_EOL);
} else {
print($path . PHP_EOL);
echo("<script src='$path'></script>".PHP_EOL);
}
}
The issue was with the syntax of "<script src='$path->__toString() . PHP_EOL>\</script>" changing this line to "<script src='$path'></script>" will help.
I'm using PHP to call a Js function with values generated by PHP.
$fp = fopen($_FILES['file']['tmp_name'], 'rb');
while(($line = fgets($fp)) !== false)
{
$split = explode(":", $line);
echo '
<script type="text/javascript">
var a = updateHashes("' . $split[0] . '", "' . $split[1] . '");
console.log(a);
</script>';
}
But my code adds some line breaks to the code, which cause errors as you can see in the following screenshot:
What could I do to fix this?
You can try to use the php trim() function on each line, that should solve.
$split = explode(":", trim($line));
Add trim()
echo '
<script type="text/javascript">
var a = updateHashes("' . trim($split[0]) . '", "' . trim($split[1]) . '");
console.log(a);
</script>';
in the below php statement I want to assign ID to EmployeeName.I will use the ID tag to search an element in the echoed list by name.Where am I making the mistake ?
<?php while( $toprow4 = sqlsrv_fetch_array( $stmt4) ) {
echo "<div class='parent-div'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" id = ' . $toprow4['EmployeeName'] .' "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>";
} ?>
Add the id to the div element as an attribute and not as textContent
echo "<div class='parent-div' id='" . $toprow4['EmployeeName'] . "'><span class='rank'>" . $toprow4['rank'] . "</span><span class='name'>" . $toprow4['EmployeeName'] . "</span><span class='points'>" . $toprow4['pointsRewarded'] . "</span></div>";
and try and make that name a valid ID by removing spaces or replace them by -.
You need to check your quotes. If you aren't already, I'd strongly recommend using an editor with code highlighting.
<?php
while ($toprow4 = sqlsrv_fetch_array( $stmt4)) {
$rank = $toprow4['rank'];
$id = $toprow4['EmployeeName'];
$points = $toprow4['pointsRewarded'];
echo "<div class='parent-div'><span class='rank'>$rank</span>";
echo "<span class='name' id='$id'></span><span class='points'>$points<span></div>";
}
?>
Seems like you have a big problem escaping your output string correctly. Perhaps you should to try write it clean by using templates and sprintf() like in this sample.
i did not test the logic of the while statement you have given
<?php
$template = '<div class="parent-div">';
$template .= '<span class="rank">%s</span>';
$template .= '<span class="name">id = %s</span>';
$template .= '<span class="points">%s</span>';
$template .= '</div>';
while($toprow = sqlsrv_fetch_array($stmt4) {
echo sprintf(
$template,
$toprow['rank'],
$toprow['EmployeeName'],
$toprow['pointsRewarded']
)
}
?>
Your code is not clean, so you failed on making correct quotes and single-quotes.
I'm making a music player using php and javascript. I list the files like this:
<?php
if (isset($_GET["action"])) {
$action = htmlspecialchars($_GET["action"]);
if ($action == "listen") {
function listFolderFiles($dir) {
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li>'. $ff . '';
if (is_dir($dir . '/' . $ff)) listFolderFiles($dir . '/' . $ff);
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles("music");
}
} else {
echo '> listen';
}
?>
And I change the song like this:
<script>
function changesong(url) {
$("#audioplayer").attr("src", url);
$("#audioplayer").trigger('play');
}
</script>
The problem is that songs with quotes in them won't play (for example Don't Stop Me Now). Is there an easy way to fix this?
You should use addslashes(), like this:
echo '<li>'. $ff . '';
You can escape quotes for javascript function in HTML code like this:
Note that you probably need to escape double quotes as well, since they can interfere with HTML tag.
$link = $dir . '/' . $ff;
$link = str_replace("'", "'", $link);
$link = str_replace('"', """, $link);
echo '<li>'. $ff . '';
I'm making a program that shows several descriptions taken from a database (MySQL):
echo "<input type=\"submit\" id=\"Boton" . $i . "\" value=\"Mostrar Descripcion\""
. " onclick=cambiarBoton(" . $i . ", \"" . $row["descripcion"] . "\") />";
echo "<div class=\"entrada-descripcion\" id=\"Descripcion" . $i . "\"> </div>";
where $row["descripcion"] is the access to the description and $i is an identifier because i'm using loops. That code generates a button that i must press to show the description. The javascript function "cambiarBoton" is the one that do that changes:
function cambiarBoton(i, d){
if (document.getElementById("Boton"+i).value == "Mostrar Descripcion"){
document.getElementById("Boton"+i).value = "Ocultar Descripcion";
document.getElementById("Descripcion"+i).innerHTML = d;
}else if(document.getElementById("Boton"+i).value == "Ocultar Descripcion"){
document.getElementById("Boton"+i).value = "Mostrar Descripcion";
document.getElementById("Descripcion"+i).innerHTML = "";
}
}
Ok, but there is a problem in the program and this is that i can't pass the description to the function in javascript and this doesn't works. How can i do this?
(I must do this without using AJAX)
I answer to the question I think you're asking is:
<?php echo "<script>var data = ".$row["description"].";</script>"; ?>
The problem was in the call of the function "cambiarBoton" in the code PHP,
it should have been this:
echo "<input type=\"submit\" id=\"Boton" . $i . "\" value=\"Mostrar Descripcion\""
. " onclick=\"cambiarBoton(" . $i . ", '" . $row["descripcion"] . "')\" />";
And then it works.