传球达阵从PHP的JavaScript [英] passing array from php to javascript

查看:93
本文介绍了传球达阵从PHP的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好
我想几个数组传递从PHP的JavaScript。对于他们中的一些它的工作原理,对别人不是。我得到的文件名的数组,数组,它包含多个文本文件的内容。

Hello I'm trying to pass several arrays from php to javascript. For some of them it works, for others not. I get an array of filenames and an array which contains the content of several text files.

    <?php
     $album="./images/text_".$benutzerLang."_album1/";
  $fileArray=lsRandom("./images/album1");
  $listTextArray=initTexts($album,$fileArray);
  $falseArray=lsRandom("./images/album2");

  print $listTextArray[0];
  ?>

    <script language="javascript" type="text/javascript">

var filesArray=new Array(5);
var falseArray=new Array(5);
var textListArray=new Array(5);

<?php

$i=0;
foreach($fileArray as $element){
    print 'filesArray['.$i.']="'.$element.'";';

    $i++;
}
$i=0;
foreach($falseArray as $element){
    print 'falseArray['.$i.']="'.$element.'";';
    $i++;
}
$i=0;
foreach($listTextArray as $element){
    print 'textListArray['.$i.']="'.$element.'";';
    $i++;
}

?>

function createText(){//...
        </script>
    <?php



    function lsRandom($foldername){
   $files = array();
   $returnFiles=array();
   $indexes=array();
   $currentPath=getcwd();

   chdir($foldername);
       // Get the all files and folders in the given directory.
   $files = glob("*", GLOB_BRACE + GLOB_MARK);
   $indexes=(array_rand($files,5));
   shuffle($indexes);
   foreach($indexes as $in){
    $returnFiles[$in]=$files[$in];
   }

   chdir($currentPath);
   return $returnFiles;
    }

    function getFileText($fileName,$path){
   $filePath=''.$path.''.$fileName.'';
   //$file=fopen($filePath,'r');
   //$text=fread($file,filesize($filePath));
   $text=file_get_contents($filePath,false);
   return $text;
    }

    function initTexts($album, $images){
   $textArray1=array();
   $i=0;
   foreach($images as $im){
    $nameArray=explode(".",$im);
    $textName=''.$nameArray[0].'.txt';
    $textArray1[$i]=getFileText($textName, $album);
    $i++;
   }
   return $textArray1;
    }


    ?>

问题是$ listTextArray。在第8行,我可以打印整个数组$ listTextArray其中包含一些小TEXTFILES的内容和它的作品。但在进一步下跌的foreach - 循环。它不工作了。当我使用变量$ listTextArray在第二个PHP代码块我的PHP code的其余部分没有得到执行了。我不知道为什么就不能在该部分访问$ listTextArray。由于它与其他阵列$ fileArray并没有什么问题$ falseArray。

The problem is the $listTextArray. In the 8th row I can print the whole array $listTextArray which contains the content of some small textfiles and it works. But further down in the 'foreach - loop'. It doesn't work anymore. As soon as I use the variable $listTextArray in the second php block the rest of my php code doesn't get executed anymore. I don't know why it can not access $listTextArray at that part. Because its no problem with the other arrays $fileArray and $falseArray.

推荐答案

一些一般性的建议是:


  1. 这是很难解决这类问题没有错误消息。如果正在打印任何错误,你可以看到它,寻找命名的文件 php.log error.log中,或 httpd.log 或询问您的服务器管理员(S)。

  2. 请尝试使用 的print_r() 你的阵列,看看是否有一个在他们如何构建任何区别。例如,仅仅在PHP中设置阵列后:

  1. It's difficult to troubleshoot this kind of problem without the error message. If no error is being printed where you can see it, look for files named php.log, error.log, or httpd.log or ask your server admin(s).
  2. Try using print_r() on your arrays to see if there's any difference in how they're structured. For example, just after setting the arrays in PHP:

print_r($fileArray);
print_r($listTextArray);
print_r($falseArray);


  • 而不是通过循环构建JS数组,请尝试使用内置的 json_en code() 函数。这既简化了你的PHP code和时有问题可能导致更多有用的错误信息:

  • Rather than constructing the JS arrays via loops, try using the built-in json_encode() function instead. This both simplifies your PHP code and may cause more useful error messages when there are problems:

    var filesArray=<?php json_encode($fileArray) ?>;
    var falseArray=<?php json_encode($falseArray) ?>;
    var textListArray=<?php json_encode($listTextArray) ?>;
    


  • 这篇关于传球达阵从PHP的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆