471,092 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

deleting line from an array

44
hi guys

i have a problem here it seems like i dont get a solution.i manage to write to the text file but i want to update the existing line without writting a new line.
my problem is when i want to append at the end of a line my programm write at the beginning of a next line.

example

1 (2008_22)-simon; thuli ; tumi
2 (2008_23)-dude ; dan ;

3 dan;tumu(2008_24) -danken ;bobo;

my problem is i want to add : dan and tumi at the end of the second line
not at the begining of the third line.

someone look at my code and help:

Expand|Select|Wrap|Line Numbers
  1.         function write($lineNumbers,$lineNumber)
  2.     {
  3.         if($_POST["cite1"] && $_POST["cite2"] && $_POST["cite3"])
  4.         $data =$_POST["cite1"].";" .$_POST["cite2"].";".$_POST["cite3"];    
  5.  
  6.         $q=$_POST["path"];
  7.  
  8.         $myArray = file($q."Registry.txt");
  9.         //echo "the linenumber is ".$lineNumbers." the line is ". $myArray[$lineNumbers-1]."<br/>";
  10.  
  11.         //Open registry file and wipe contents
  12.  
  13.         $fp = fopen($q."Registry.txt", "w");
  14.         //$myArray[$lineNumbers-1] = $myArray[$lineNumbers-1].$data;
  15.         //echo $myArray[$lineNumbers-1]."<br />";
  16.         $newdata = $myArray[$lineNumbers-1].$data;
  17.         //echo $newdata."<br/>";
  18.  
  19.         // remove the line from a file
  20.         unset($myArray["$lineNumbers-1"]);
  21.         // put the data at the end of my file
  22.         array_push("$myArray","$newdata");
  23.  
  24.         usort($myArray,"strnatcmp");
  25.  
  26.         //Loop through the registry array
  27.  
  28.         foreach ($myArray as $value)
  29.  
  30.         {
  31.  
  32.             //Write the sorted contents to registry line-by-line
  33.  
  34.               fwrite($fp,$value);
  35.  
  36.  
  37.  
  38.     }
  39.  
  40.  
  41.  
  42.         // Close the registry file
  43.  
  44.         fclose($fp);
  45.         echo "the registry updated successfully <br />";
  46.         $myArray1 = file($q."Registry.txt");
  47.         echo "new registry is ".$myArray1[$lineNumbers-1]."<br />";
  48.         echo "line after is ".$myArray1[$lineNumbers]."<br />";                
  49.  
  50.     }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. ?>
Aug 5 '08 #1
1 1107
Ajm113
161 100+
PHP should be like C++, where you can make a while loop then get something like fgets into the while loop and count for each line into a int and get the lines into a string array and if you want to replace a certain line just enter,

array[$iLine--] = "My data goes here."; = Line1, so just minus the id number by one.

For removel just make the line equal nothing in double quotes and make a if statment weather if that line has something or not to break the line or not or even enter the text.

Then get all the arrays representing the line and print them back out into the file like new into each line using that if statment.

I wanted to be a bit of a help, but that should give you a idea what to do.

BTW use the PHP tags please! So its easier to read your code. Its that button with the php page uptop in the editor.
Aug 5 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by flupke | last post: by
18 posts views Thread by Dan | last post: by
reply views Thread by Bandit | last post: by
46 posts views Thread by DP | last post: by
13 posts views Thread by programming | last post: by
3 posts views Thread by =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.