[EdLUG] Help 5

Swithun Crowe swithun at swithun.servebeer.com
Wed May 6 21:42:26 UTC 2020


Hello

I've corrected the first bit of code.

_music.php_

<html>
<body>

<?php
function getmusic()
// Open a directory, and read its contents
{
$musicdir = array(); // declare and initialise an array like this
$dir = "Music/";

if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo $file . "<br>";
      array_push($musicdir, $file); // array_push alters the array in place
    }
    closedir($dh);
  }
  return $musicdir; // missing $ and typo
}
?>

</body>
</html>

You can't (directly) call PHP functions from inside Javascript, or execute
PHP in the browser.

_HTML file_

<script>

var array musicdir() = getmusic();

 while (($file = readdir($musicdir)) !== false){
      echo $file . "<br>";

</script>

In the PHP code, your function prints out the contents of the directory,
so maybe it doesn't need to return an array as well. But you can't mix PHP
and Javascript.

Swithun.


More information about the EdLUG mailing list