tags:

views:

39

answers:

1

My goal here is to open temp.php, explode by ### (line terminator), then by %% (field terminator). Change a specific field, on a specific line, then implode everything back together and save it.

There are a couple variables at play here:

row = the target row number
target = the target field/column number
nfv = the info that i want to place into the target field

Im using the counter $i to count until i get to the desired row. Then counter $j to count til i get to my desired field/target. So far this gives me an error for invalid implode arguments, or doesn't save any data at all. Im sure there are a couple things wrong, but i am frustrated and lost.

<?
$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");
?>

If someone can tell what is wrong with this, please be detailed so i can learn why its not working.

Here is some sample csv data for testing

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###
+1  A: 

The following should work. This also eliminates a lot of unnecessary looping

<?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");
?>

The looping you were doing was removed as the row were numerically indexed already anyways. Accessing the array element directly is much faster than looping through the elements until you find what you want.

Jonathan Fingland
while i try this, could you elaborate a little as to what you did and why? Thanks for taking the time btw.
Patrick
I tried this, whats happening is say i selcect row 1 and my target. Its running successfully, but its creating a blank line and not updating the row/target specified. Looks like its putting in six #'s in a row. But i also dont see my updated info anywhere (value of nfv)
Patrick
I had an error at `$info = explode("%%", $value);` I corrected it in the edit. please make sure yours says `$info = explode("%%", $target_row);`
Jonathan Fingland
I just tested this on php 5.2.9 and can confirm it is working. See http://www.ashita.org/StackOverflow/temp.php for the data, and http://www.ashita.org/StackOverflow/csv.php for the file above (minus the Location change)
Jonathan Fingland
It works! Thank you so much for the time and for explaining what was going on here.
Patrick
glad to help and happy to hear its working now
Jonathan Fingland