ozestretch Posted September 17, 2009 Share Posted September 17, 2009 Was not sure how to title this post, but let me elaborate. I have the data in CSV format like so: variable 1, variable 2,variable 3 variable 4 variable 5, variable 6 variable 7, variable 8,variable 9 Each new line will have at least 1 variable and the first variable must not be changed (this is used as a reference) So if I was to change line 3, I would post something like this: variable 5, variable 6, variable 10 if I was adding to it, or variable 5 if I was removing item/s from it $csetting = explode(",",$_POST['setting']); $grades = preg_replace('/\r\n|\r/', "\n", $row['setting']); // $row['setting'] drawn from mysql in CSV format above $grades = explode("\n",$grades); $err=0; foreach($grades AS $key){ // get each new line if($key===$csetting[0]){ // only 1 name in records, was something deleted? if($key===$csettings){ // only 1 name in records, one name posted // no change needed $err++; }else{ // update with addition $allgrades.=$csettings.'\r\n'; } }elseif($key===$csettings){ // line equals post value $err++; }else{ $grades2 = explode(",",$key); foreach($grades2 AS $key2){ if($key2!=$csetting[0]){ $allgrades.=$key.'\r\n'; } } } } I am stumped basically, am I looking at this the wrong way? After 5 hours of retyping the same thing different ways, I got the feeling the tree I am barking at is actually a light pole. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/174637-solved-removing-or-adding-to-an-array-in-a-loop/ Share on other sites More sharing options...
ozestretch Posted September 18, 2009 Author Share Posted September 18, 2009 Not sure if that made sense. I have this: variable 1, variable 2,variable 3 variable 4 variable 5, variable 6 variable 7, variable 8,variable 9 And want to edit 1 line only. ie where: value1, value2,value3 I wish to add a new value to that line value1, value2,value3,new value or remove a line: value1, value2 The common value here is the prefixing value, in the above case, value 1. The posted data for adding would be posted as: value1, value2,value3,new value and likewise for removing value, would be: value1, value2 If I was replacing a value, that is easy, preg_replace(). But I need to find the line that is being changed, then apply the changes. Link to comment https://forums.phpfreaks.com/topic/174637-solved-removing-or-adding-to-an-array-in-a-loop/#findComment-920483 Share on other sites More sharing options...
emehrkay Posted September 18, 2009 Share Posted September 18, 2009 http://us2.php.net/manual/en/function.fgetcsv.php not hard to figure out the loop from here Link to comment https://forums.phpfreaks.com/topic/174637-solved-removing-or-adding-to-an-array-in-a-loop/#findComment-920489 Share on other sites More sharing options...
ozestretch Posted September 18, 2009 Author Share Posted September 18, 2009 When the idea first went through my head it seemed easy... but am approaching 26th hour of being awake and have become hazy. 1: Can I use fgetcsv() with a database row[]? This is where variable 1, variable 2,variable 3 variable 4 variable 5, variable 6 variable 7, variable 8,variable 9 comes from. I assumed I need to fopen() etc... Am I on right track that I need to get each line into an array, then search each array for the common value? Once I found that line, I need to set that key with the new value. Trying this now.. <?php $grades = preg_replace('/\r\n|\r/', "\n", $row['setting']); $grades = explode("\n",$grades); $csetting = explode(",",$_POST['setting']); $var = $csetting[0]; foreach($grades as $value) { if(stristr($value,$var)){ // key of array to be replaced $foundkey = array_key($grades, $value); } } $grades[$foundkey] = $csettings; $mynewcsvrow = implode("\n",$grades); // insert back into database with new values ?> Link to comment https://forums.phpfreaks.com/topic/174637-solved-removing-or-adding-to-an-array-in-a-loop/#findComment-920630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.