deleting line from an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ogo796
    New Member
    • Jan 2008
    • 44

    deleting line from an array

    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_2 4) -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:

    Code:
            function write($lineNumbers,$lineNumber)
    	{
    		if($_POST["cite1"] && $_POST["cite2"] && $_POST["cite3"])
    		$data =$_POST["cite1"].";" .$_POST["cite2"].";".$_POST["cite3"];	
    
    		$q=$_POST["path"];
    
    		$myArray = file($q."Registry.txt");
    		//echo "the linenumber is ".$lineNumbers." the line is ". $myArray[$lineNumbers-1]."<br/>";
    
    		//Open registry file and wipe contents
    
    		$fp = fopen($q."Registry.txt", "w");
    		//$myArray[$lineNumbers-1] = $myArray[$lineNumbers-1].$data;
    		//echo $myArray[$lineNumbers-1]."<br />";
    		$newdata = $myArray[$lineNumbers-1].$data;
    		//echo $newdata."<br/>";
    
    		// remove the line from a file
    		unset($myArray["$lineNumbers-1"]);
    		// put the data at the end of my file
    		array_push("$myArray","$newdata");
    
    		usort($myArray,"strnatcmp");
    
    		//Loop through the registry array
    
    		foreach ($myArray as $value)
    
    		{
    
    			//Write the sorted contents to registry line-by-line
    
      			fwrite($fp,$value);
    
    		
    
    	}
    
    
    
    		// Close the registry file
    
    		fclose($fp);
    		echo "the registry updated successfully <br />";
    		$myArray1 = file($q."Registry.txt");
    		echo "new registry is ".$myArray1[$lineNumbers-1]."<br />";
    		echo "line after is ".$myArray1[$lineNumbers]."<br />";				
    
    	}
    
       
    
    
    
    ?>
    Last edited by Markus; Oct 30 '08, 02:14 PM. Reason: added # tags
  • Ajm113
    New Member
    • Jun 2007
    • 161

    #2
    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.

    Comment

    Working...