我的目标是打开temp.php,按### (行终止符)分解,然后按%% (字段终止符)分解。更改特定行上的特定字段,然后将所有内容内爆在一起并保存。
这里有几个变量在起作用:
row =目标行号
target =目标字段/列号
nfv =我要放入目标字段的信息
我使用计数器$i进行计数,直到到达所需的行。然后对$j进行计数,直到我到达我想要的字段/目标。到目前为止,这给了我一个关于无效内爆参数的错误,或者根本没有保存任何数据。我确定有几件事是错的,但我很沮丧和迷失。
<?
$row = $_GET['row'];
$nfv = $_GET['nfv'];
$target = $_GET['target'];
$data = file_get_contents("temp.php");
$csvpre = explode("###", $data);
$i = 0;
$j = 0;
foreach ( $csvpre AS $key => $value){
$i++;
if($i == $row){
$info = explode("%%", $value);
foreach ( $info as $key => $value ){
$j++;
if($j == "$target"){
$value = $nfv;
}
}
$csvpre[$key] = implode("%%", $info);
}
}
$save = implode("###", $csvpre);
$fh = fopen("temp.php", 'w') or die("can't open file");
fwrite($fh, $save);
fclose($fh);
header("Location: data.php");
?>如果有人能说出这是什么问题,请详细,以便我可以了解为什么它不工作。
以下是一些用于测试的示例csv数据
1%%4%%Team%%40%%75###2%%4%%Individual%%15%%35###3%%4%%Stunt Group%%50%%150###4%%4%%Coed Partner Stunt%%50%%150###5%%4%%Mascot%%15%%35###6%%8%%Team%%40%%75###7%%8%%Stunt Group%%50%%150###8%%8%%Coed Partner Stunt%%50%%150###9%%3%%Team%%40%%75###10%%1%%Team%%40%%75###11%%1%%Solo%%15%%35###12%%1%%Duet%%50%%150###13%%2%%Team%%50%%50###14%%2%%Solo%%15%%35###15%%2%%Duet%%50%%150###16%%8%%Individual%%15%%35###发布于 2009-10-28 01:06:17
下面的方法应该是可行的。这也消除了许多不必要的循环。
<?php
$row = $_GET['row'];
$nfv = $_GET['nfv'];
$target = $_GET['target'];
$data = file_get_contents("temp.php");
$csvpre = explode("###", $data);
//removed i and j. unnecessary.
//checks if the row exists. simple precaution
if (isset($csvpre[$row]))
{
//temporary variable, $target_row. just for readability.
$target_row = $csvpre[$row];
$info = explode("%%", $target_row);
//check if the target field exists. just another precaution.
if (isset($info[$target]))
{
$info[$target] = $nfv;
}
//same as yours. just pack it back together.
$csvpre[$row] = implode("%%", $info);
}
$save = implode("###", $csvpre);
$fh = fopen("temp.php", 'w') or die("can't open file");
fwrite($fh, $save);
fclose($fh);
header("Location: data.php");
?>你正在做的循环被删除了,因为行已经被数字索引了。直接访问数组元素比遍历元素直到找到所需内容要快得多。
https://stackoverflow.com/questions/1632236
复制相似问题