0

This is the code i'm using:

<?php 
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$) |(\.Gif$)"; //valid image extensions 
$files = array(); 
$curimage=0; 
if($handle = opendir($"http://newsxpressmedia.com/files/radar-simulation-files")) { 
    while(false !== ($file = readdir($handle))){ 
        if(eregi($pattern, $file)){ //if this file is a valid image 
            //Output it as a JavaScript array element 
            $files[] = $file; 
            $curimage++; 
        } 
    } 
    closedir($handle); 
} 
?> 

<!DOCTYPE html>

<html>
   <head>
      <title>change picture</title>

      <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type='text/css'>
    #Timer_Countdown{
    background:black;
    color:yellow;
    font-weight:bold;
    text-align:center;
}
  </style>

      <script type = "text/javascript">

          function displayNextImage() {
              x = (x === images.length - 1) ? 0 : x + 1;
              document.getElementById("img").src = images[x];
          }

          function displayPreviousImage() {
              x = (x <= 0) ? images.length - 1 : x - 1;
              document.getElementById("img").src = images[x];
          }

var images = <?=json_encode($files)?>;
//var images = [];
var x = -1; 
var swap_hours = 0;
var swap_minutes = 0;
var swap_seconds = 5;

var down_counter_hours;
var down_counter_minutes;
var down_counter_seconds;

function initTimer() {

    down_counter_hours = swap_hours;
    down_counter_minutes = swap_minutes;
    down_counter_seconds = swap_seconds;
    counter = setInterval(switcher, 1000);
}

function restartCounter() {
        down_counter_hours = swap_hours;
        down_counter_minutes = swap_minutes;
        down_counter_seconds = swap_seconds;
}

function switcher() {
    down_counter_seconds--;
    if (down_counter_hours <= 0 && down_counter_minutes <= 0 && down_counter_seconds <= 0) {
        swapColor();
        restartCounter();
    }
    if (down_counter_seconds <= 0 && down_counter_minutes > 0) {
        down_counter_seconds = 60;
        down_counter_minutes--;
    }
    if (down_counter_minutes <= 0 && down_counter_hours > 0) {
        down_counter_minutes = 60;
        down_counter_hours--;
    }

    document.getElementById("Timer_Countdown").innerText =        down_counter_hours+":"+down_counter_minutes+":"+down_counter_seconds;
}

function swapColor() {
    displayNextImage();
}

      </script>
      <div id="div_hours" class="div_box"></div>
      <div id="div_minutes" class="div_box"></div>
      <div id="div_seconds" class="div_box"></div>
      <div id="div_switcher" class="div_box"></div>
   </head>

   <body onload = "initTimer()">
       <div id="Timer_Countdown">&nbsp;</div>
       <img id="img" src="http://newsxpressmedia.com/files/theme/radar000005.Gif">
       <button onclick="displayPreviousImage(); restartCounter()">Previous</button>
       <button onclick="displayNextImage(); restartCounter()">Next</button>
   </body>
</html>

The error is on the line:

var images = <?=json_encode($files)?>;

If i change this line to this line:

var images = [];

Then the code is working fine but without using the php files variable. Something is wrong with the line: var images = <?=json_encode($files)?>;

I tried to change this line to: echo json_encode($images); or to var images = echo json_encode($files); but same error.

I'm using weebly to build my site and my site server is on ipage.com

How can i fix the error ?

Patrick Klug
  • 14,056
  • 13
  • 71
  • 118
Haim Shaban
  • 179
  • 1
  • 1
  • 11
  • `=json_encode($files)?>` isn't a valid javascript expression. – lurker Oct 29 '14 at 23:54
  • 1
    Is all of your code in a PHP file? – showdev Oct 29 '14 at 23:54
  • 1
    Hit *"View page source"* in your browser. The problem should be apparent then – Phil Oct 29 '14 at 23:55
  • showdev no the php code is only what i show in my question it's above the html code. I don't have any php file on my server(filemanager of my site). – Haim Shaban Oct 29 '14 at 23:58
  • 1
    Without some [server-side configuration](http://stackoverflow.com/questions/23574306/executing-php-code-inside-a-js-file#answer-23574475), you won't be able to execute PHP code from a file that is not `.php`. – showdev Oct 30 '14 at 00:00
  • 1
    I saw this now in weebly.com site: Can I use PHP, MySQL and Other Server-Side Languages? " You can edit your site with HTML, CSS and Javascript (client-side languages); however, Weebly does not support building your site with server-side languages, such as PHP or ASP. We also do not currently offer database access." is that mean i can't use php at all ? – Haim Shaban Oct 30 '14 at 00:01
  • 1
    Phil's view page source would clearly clarify that - if all of your php code is present in the page output, then there's no php interpreter to work on it. – Sacho Oct 30 '14 at 00:05
  • Sacho i did view source and the php code is there marked in green but the code is there. – Haim Shaban Oct 30 '14 at 00:15
  • It shouldn't be. As mentioned by Sacho, PHP code is intended to be executed on the server, generating HTML code for the browser. You should never see PHP code in your page source. This indicates that your PHP code is not being executed. – showdev Oct 30 '14 at 00:24
  • This is bad i'm paying weebly and they are not supporting php. I should have check it before signing with them. – Haim Shaban Oct 30 '14 at 00:35
  • Is there any other sites like weebly.com but that support php ? Maybe free or not free but something good that support also php. – Haim Shaban Oct 30 '14 at 01:09

3 Answers3

0

It looks like you don't have short tags enabled(and are using a pre 5.4 php version), thus the line <?=json_encode($files)?> is not parsed by php and is just send straight through. var images = <... is not a valid javascript expression. Here's some information about php's short_open_tag parameter: http://php.net/manual/en/ini.core.php#ini.short-open-tag

Sacho
  • 2,169
  • 1
  • 14
  • 13
  • `=` isn't part of short-open-tags anymore. OP also tried ` – Phil Oct 29 '14 at 23:56
  • 1
    No, he did not. He also didn't list his php version. – Sacho Oct 29 '14 at 23:57
  • Well, he kind of did but it's not entirely clear so I'll give you the benefit of the doubt – Phil Oct 29 '14 at 23:59
  • How did he "kind of"? It doesn't mention doing so anywhere. – Sam Hanley Oct 29 '14 at 23:59
  • From the OP ~ *"I tried to change this line to: echo json_encode($images); or to var images = echo json_encode($files); but same error."* I agree, it's hard to tell *exactly* what OP has tried though. Edit: seems OP isn't using PHP on the server at all – Phil Oct 30 '14 at 00:00
  • 2
    This is generally why I avoid posting answers before clarifying with the OP, but occam's razor suggested that as long as he was right about the line of the error, that this would be the problem. In hindsight, the possibility of there being *no php support at all* would produce the same error. – Sacho Oct 30 '14 at 00:03
0

A couple of changes. Change

$pattern ="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$) |(\.Gif$)";

to

$pattern = '/\.(png|jpg|jpeg|gif)$/i';

and

eregi($pattern, $file)

to

preg_match($pattern, $file)

and see what happens. eregi deprecated as PHP 5.3.0.

StackSlave
  • 10,613
  • 2
  • 18
  • 35
-2

You are using '=' inside the PHP tags. Try this:

var images = <?json_encode($files)?>;
codeviper
  • 78
  • 10