Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t读取PHP中的csv并将其以html文件的形式传递给javascript 环境 Windows 8.1 64位 谷歌浏览器 我想做什么 最终目标_Javascript_Php_Windows_Csv_Raspberry Pi - Fatal编程技术网

Can';t读取PHP中的csv并将其以html文件的形式传递给javascript 环境 Windows 8.1 64位 谷歌浏览器 我想做什么 最终目标

Can';t读取PHP中的csv并将其以html文件的形式传递给javascript 环境 Windows 8.1 64位 谷歌浏览器 我想做什么 最终目标,javascript,php,windows,csv,raspberry-pi,Javascript,Php,Windows,Csv,Raspberry Pi,用树莓皮制作宠物监控系统。创建一个网页,在那里你可以检查宠物的流图像以及温度和湿度 当前问题 无法使用PHP读取csv数据(温度和湿度)并将其传递到html文件中的javascript 下面给了我一个空白页 test.html 我尝试了很多事情,包括在php子句中添加echo,用javascript硬编码一个示例数组,等等。。。白费力气 将行更改为[0,5,4],[1,9,10],[2,20,23]效果良好。所以PHP有点问题,但我不知道它是什么 我也提到了这个帖子。 - 但这并没有什么帮助

用树莓皮制作宠物监控系统。创建一个网页,在那里你可以检查宠物的流图像以及温度和湿度

当前问题 无法使用PHP读取csv数据(温度和湿度)并将其传递到html文件中的javascript

下面给了我一个空白页

test.html

我尝试了很多事情,包括在php子句中添加
echo
,用javascript硬编码一个示例数组,等等。。。白费力气

行更改为
[0,5,4],[1,9,10],[2,20,23]
效果良好。所以PHP有点问题,但我不知道它是什么

我也提到了这个帖子。 - 但这并没有什么帮助

另外,javascript控制台告诉我以下消息

The console tells me Uncaught SyntaxError: Unexpected token <
控制台告诉我未捕获的SyntaxError:意外令牌<
我如何解决这个问题?提前谢谢

更新1
我添加了
echo
,并在Raspberry Pi的web服务器上运行了该程序。但是,我仍然看到一个空白页面和
Uncaught SyntaxError:Unexpected token可能是因为php文件没有.php扩展名。尝试在
test.php中重命名
test.html

替换为
,将文件扩展名改为.php而不是.html,一切都会正常工作。

我编写了一些类似的代码:

$seperator = $_POST['seperator'];
$escape = $_POST['escape'];
$default_val = $_POST['default_val'];
$files = $_FILES['filesToUpload']['name'];
$files_array = array();
//no files selected
if ($files[0] == "") {
    echo "You have to select at least 1 file";
    exit();
}
//preprocess by creating an array per file with it's path and name
$count = 0;
foreach ($files as $file) {
        $current_file = array();
        $current_file['name'] = $file;
        $current_file['path'] = $_FILES['filesToUpload']['tmp_name'][$count];
        $files_array[$file] = $current_file;
        ++$count;
}

$translation_array = array();
$languages = array();
foreach ($files_array as $file_key => $file_value) {
    $text_file = file($file_value['path']);
    $languages[] = $file_value['name'];

    foreach ($text_file as $line_number => $line) {
        $line = rtrim($line, "\n");
        $line_parts = explode('=', $line);
        $translation_key = $line_parts[0];
        if ($file_value['name'] != 'brndportal.properties') {
            $translation_array[$translation_key][$file_value['name']] = $line_parts[1];
        } else {
            $translation_array[$translation_key][$file_value['name']] = $line_parts[0];
        }
    }
}

$translation_csv = fopen("files/translation.csv", "w") or die("Unable to open file!");

//headers
$txt = "key" . $seperator;
foreach ($files as $file) {
    $txt .= $file . $seperator;
}

$txt .= "\n";
fwrite($translation_csv, $txt);

//translations
foreach ($translation_array as $translation_key => $translation_arr) {

    if (array_key_exists('brndportal.properties', $translation_array[$translation_key])) {
        $txt = '';
        $txt .= $translation_key . $seperator;

        foreach ($languages as $language) {
            if(array_key_exists($language, $translation_arr)) {
                $translation_value = $translation_arr[$language];
            }
            else {
                $translation_value = $default_val;
            }
            if (strpos($translation_value, $seperator) !== false) {
                $translation_value = $escape . $translation_value . $escape;
            }

            $txt .= $translation_value . $seperator;
        }

        $txt .= "\n";
        fwrite($translation_csv, $txt);
    }
}
fclose($translation_csv);
它读取一个或多个CSV文件并解析行。你可以插入你自己的分隔符之类的东西


我认为最好将所有内容读入数组并序列化为JSON,这样您就可以通过ajax调用将其传递给javascript。我建议对该部分使用jQuery。

我在
未捕获的SyntaxError:Unexpected token
方面也遇到了同样的困难,它成功了。

你能展示一下
$str
内爆()之后的样子吗
你也尝试过
,正如@RiggsFolly指出的那样,代码是
而不是
你能展示一下$str在内爆后的样子吗()做一个
回显$str
并将结果粘贴到您的问题OK中,因此移动
echo$str
进入
也许你会看到我现在就试过了。我看到的不是空白页面,而是html源代码。请检查文件名是否没有变成
test.php.txt
,这可能是一个愚蠢的问题,但我会问您,您是通过web服务器运行此代码,而不是通过在资源管理器中双击文件名来启动此代码?右??文件名不是
test.php.txt
。我在做后者。抱歉,我不太了解web开发中的重要事项……如果需要在机器中安装web服务器(需要运行php文件),请查看XAMPP:Javascript控制台告诉您什么?我复制/粘贴了你的代码,所有这些修复程序都运行良好。控制台告诉我
uncaughtsyntaxerror:uncontractedtoken你是通过一个可以处理PHP的web服务器执行脚本的吗?如果您的Javascript控制台说“意外标记否,我是用浏览器执行的。文件扩展名是.php。好的,我来检查一下。谢谢@dixhom您必须自己做一些调整,但请随时提问,不要忘记调试(
var_dump
)这些东西,这样您就知道在哪里发生了什么。您的代码远远超出我的知识范围,因此我想从“解释”您的代码开始。非常感谢。
The console tells me Uncaught SyntaxError: Unexpected token <
$seperator = $_POST['seperator'];
$escape = $_POST['escape'];
$default_val = $_POST['default_val'];
$files = $_FILES['filesToUpload']['name'];
$files_array = array();
//no files selected
if ($files[0] == "") {
    echo "You have to select at least 1 file";
    exit();
}
//preprocess by creating an array per file with it's path and name
$count = 0;
foreach ($files as $file) {
        $current_file = array();
        $current_file['name'] = $file;
        $current_file['path'] = $_FILES['filesToUpload']['tmp_name'][$count];
        $files_array[$file] = $current_file;
        ++$count;
}

$translation_array = array();
$languages = array();
foreach ($files_array as $file_key => $file_value) {
    $text_file = file($file_value['path']);
    $languages[] = $file_value['name'];

    foreach ($text_file as $line_number => $line) {
        $line = rtrim($line, "\n");
        $line_parts = explode('=', $line);
        $translation_key = $line_parts[0];
        if ($file_value['name'] != 'brndportal.properties') {
            $translation_array[$translation_key][$file_value['name']] = $line_parts[1];
        } else {
            $translation_array[$translation_key][$file_value['name']] = $line_parts[0];
        }
    }
}

$translation_csv = fopen("files/translation.csv", "w") or die("Unable to open file!");

//headers
$txt = "key" . $seperator;
foreach ($files as $file) {
    $txt .= $file . $seperator;
}

$txt .= "\n";
fwrite($translation_csv, $txt);

//translations
foreach ($translation_array as $translation_key => $translation_arr) {

    if (array_key_exists('brndportal.properties', $translation_array[$translation_key])) {
        $txt = '';
        $txt .= $translation_key . $seperator;

        foreach ($languages as $language) {
            if(array_key_exists($language, $translation_arr)) {
                $translation_value = $translation_arr[$language];
            }
            else {
                $translation_value = $default_val;
            }
            if (strpos($translation_value, $seperator) !== false) {
                $translation_value = $escape . $translation_value . $escape;
            }

            $txt .= $translation_value . $seperator;
        }

        $txt .= "\n";
        fwrite($translation_csv, $txt);
    }
}
fclose($translation_csv);