In the following code, I cannot get the $list array to load into the js array (jspgst). Help, anybody?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>aaaaaa</title>

</head>
<body>
<script type="text/javascript">

var jspgst= new Array();

<?php
// file example 1: read a text file into an array, with
// each line in a new element

$filename="linksfile.txt";
$lines = array();
$file = fopen($filename, "r");
while(!feof($file)) {

//read file line by line into a new array element 
$lines[] = fgets($file,20); 

}
fclose ($file);

//read file line by line into a new array element 

foreach($lines as $url){
?>
//store PHP urls into 'pages' JavaScript array
jspgst[jspgst.length]='<?php echo $url?>';

<?php
}

?>
document.write("This is just a check")
</script>

</body>
</html>

    1. When posting PHP code, please use the board's [PHP][/PHP] bbcode tags - they make the code much, much easier to read (as well as aiding the diagnosis of syntax errors).

    2. At a quick glance, I can't see much wrong with your code. At this point, it would help us to have either a) the URL to this page, or b) the relevant outputted HTML code.

    3. You could clean up your code by simply using [man]file/man to load the file into a variable as an array of lines.

      It works fine for me, but are all your URI's only 20 characters long?

        Brad and Rince - Thanks for your quick replies....
        As a newbie I am unfamiliar with the reference to bb tags PHP - intuitively, I am thinking these

        should replace the <?php lines? Tried the file read with the same result.....Rince says the code worked for him-- Is is possible my PHP config file has a bad entry preventing this from working? The really rare thing is I have another php program that does the load of an array into js and it works! but the array was not loaded from a file - simply by code....think this could be the problem?

          OK after having a more in depth look (rather than just running it through the debugger) I think you'll find that this will work for you.

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
          <title>aaaaaa</title>
          
          </head>
          <body>
          <script type="text/javascript">
          
          var jspgst= new Array();
          
          <?php
          // file example 1: read a text file into an array, with
          // each line in a new element
          
          $filename="linksfile.txt";
          $lines = array();
          $file = fopen($filename, "r");
          while(!feof($file)) {
          
          //read file line by line into a new array element
          $lines[] = fgets($file,80);
          
          }
          fclose ($file);
          
          //read file line by line into a new array element
          $jspgst = "jspgst[jspgst.length]=";
          
          foreach($lines as $url){
          
          //store PHP urls into 'pages' JavaScript array
          $jspgst .= "'".trim($url, "\r\n")."';\n";
          
          
          }
          echo $jspgst;
          ?>
          document.write("This is just a check")
          </script>
          
          </body>
          </html>

          I changed the [man]fgets/man value to 80 to catch all the URI's in the file of URI's I had here.

            Rince - this worked ! almost or I did something wrong. I am attaching my file so you can see it and maybe determine my error. I added document.write(jsarr[0]) after the first d-w and get a length of one. Displayed the reulting table and had only the first entry in the file. Also, the echo statement displayed nothing. Thanks in advance for your help....much appreciated.

              Ok. Why are you doing document.write(jsarr[0]) when your array is called jspgst?

              This works:

              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
              <head>
              <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
              <title>aaaaaa</title>
              
              </head>
              
              <body>
              <script type="text/javascript">
              
              var jspgst= new Array();
              
              <?php
              // file example 1: read a text file into an array, with
              // each line in a new element
              
              $filename="linksfile.txt";
              $lines = array();
              $file = fopen($filename, "r");
              while(!feof($file)) {
              
              //read file line by line into a new array element
              $lines[] = fgets($file,20);
              
              }
              fclose ($file);
              
              //read file line by line into a new array element
              $jspgst = "";
              
              $i=0;
              foreach($lines as $url){
              
              //store PHP urls into 'pages' JavaScript array
              if (trim($url) !='') {
              
              
              	$jspgst .= "jspgst[". $i. "]='".trim($url, "\r\n")."';\n";
              	$i++;
              }
              }
              
              echo $jspgst;
              ?>
              
              
              document.write("This is just a check");
              document.write(jspgst[10]);
              </script>
              
              </body>
              </html>

              Sorry it took so long my js is a bit rusty.

                Also, as I said earlier, you can use [man]file[/man] to replace all of this code:

                $file = fopen($filename, "r"); 
                while(!feof($file)) { 
                
                //read file line by line into a new array element 
                $lines[] = fgets($file,80); 
                
                } 
                fclose ($file); 

                with simply this:

                $file = file($filename);

                  Rince - I apologize, I copied the wrong statement....but it does work and I thank you and Brad for your help.....

                    Write a Reply...