Einzelne Daten einer einzelnen Zeile in einer Textdatei ändern

webdesign

Mitglied
Hallo zusammen.
Ich möchte gerne ein PHP Script zu programmieren, mit welchem ich einzelne Daten einer Zeile in einer Textdatei ändern kann, ohne dass es mir die anderen Daten der Zeile auch ändert. Ich habe etwas gebastelt, funktioniert jedoch nicht.
Hier mal das Formular:

HTML:
<form name="newEntry" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<table width="100%">
<tr>
<td>Tag:</td><td colspan="3">Datum:</td><td>Zeit:</td><td>St&uuml;ck:</td><td>Ort:</td><td>Location:</td><td>Reservation:</td><td>BNR:</td>
</tr>
<tr>

<td>
<select name="replace_tag" size="1">
<option selected></option>
<option>Mo</option>
<option>Di</option>
<option>Mi</option>
<option>Do</option>
<option>Fr</option>
<option>Sa</option>
<option>So</option>
</select>
</td>

<td>
<select name="replace_datum1" size="1" id="datum">
<option selected></option>
<option>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>08</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
</td>

<td>
<select name="replace_datum2" size="1" id="datum">
<option selected></option>
<option>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>08</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
</td>

<td>
<input name="replace_datum3" type="text" id="datum" size="5">
</td>

<td><input name="replace_zeit" type="text" id="zeit" size="5"></td>

<td><input name="replace_stueck" type="text" id="stueck" size="7"></td>

<td><input name="replace_ort" type="text" id="ort" size="7"><br></td>

<td><input name="replace_location" type="text" id="location" size="7"></td>

<td><input name="replace_reservation" type="text" id="reservation" size="7"></td>

<td><input name="bnr" type="text" id="reservation" size="4"></td>


</tr>
</table><br>
<input name="post" type="submit" id="post" value="eintragen"><br>

</form>

Und jetzt der PHP Code:
PHP:
<?php


 $bnr = $HTTP_POST_VARS['bnr'];
 $replace_tag = $HTTP_POST_VARS['replace_tag'];
 $replace_datum1 = $HTTP_POST_VARS['replace_datum1'];
 $replace_datum2 = $HTTP_POST_VARS['replace_datum2'];
 $replace_datum3 = $HTTP_POST_VARS['replace_datum3'];
 $replace_zeit = $HTTP_POST_VARS['replace_zeit'];
 $replace_stueck = $HTTP_POST_VARS['replace_stueck'];
 $replace_ort = $HTTP_POST_VARS['replace_ort'];
 $replace_location = $HTTP_POST_VARS['replace_location'];
 $replace_reservation = $HTTP_POST_VARS['replace_reservation'];
 $replace_datum = "$replace_datum1.$replace_datum2.$replace_datum3";
 
 $fehler = "ERROR. Im BNR Feld muss eine Zahl eingegeben werden!<a href='javascript:history.back()'><b>Bitte Hier klicken</b></a>";
 
 
 $datei = "dateiname.txt";

 if($post)
 { 
 	if ($bnr =="")	
 	{
	echo("$fehler");
 	}
	else
	{
	$inhalt = file($datei);
		$anzahl = count($inhalt);
 		$nr = $bnr;
 		
			$old_eintrag = explode("|-|",$inhalt[$nr]); //Inhalt zerteilen
			// Variablen definieren
			$old_tag = $old_eintrag[0];
			$old_datum = $old_eintrag[1];
			$old_zeit = $old_eintrag[2];
			$old_stueck = $old_eintrag[3];
			$old_ort = $old_eintrag[4];
			$old_location = $old_eintrag[5];
			$old_reservation = $old_eintrag[6];
			
		
		if(($replace_tag = "")||($replace_tag = $old_tag)) 
		{
		 $new_tag = $old_tag;
		 }
		 else
		 {
		 $new_tag = $replace_tag;
		 }
		 
		if(($replace_datum = "")||($replace_datum = $old_datum))
		 {
		 $new_datum = $old_datum;
		 }
		 else
		 {
		 $new_datum = $replace_datum;
		 }
		 
		if(($replace_zeit = "")||($replace_zeit = $old_zeit))
		 {
		 $new_zeit = $old_zeit;
		 }
		 else
		 {
		 $new_zeit = $replace_zeit;
		 }
		 
		if(($replace_stueck = "")||($replace_suteck = $old_stueck))
		 {
		 $new_stueck = $old_stueck;
		 }
		 else
		 {
		 $new_stueck = $replace_stueck;
		 }
		 
		 
		if(($replace_ort = "")||($replace_ort = $old_ort))
		 {
		 $new_ort = $old_ort;
		 }
		 else
		 {
		 $new_ort = $replace_ort;
		 }
		 
		if(($replace_location = "")||($replace_location = $old_location))
		 {
		 $new_location = $old_location;
		 }
		 else
		 {
		 $new_location = $replace_location;
		 }
		 
		if(($replace_reservation = "")||($replace_reservation = $old_reservation))
		 {
		 $new_reservation = $old_reservation;
		 }
		 else
		 {
		 $new_reservation = $replace_reservation;
		 }
		
		 
		// Neue Formularelemente die ersetzt werden sollen
		$rep_neu = "$new_tag|-|$new_datum|-|$new_zeit|-|$new_stueck|-|$new_ort|-|$new_location|-|$new_reservation\n";
		
		// Herauslesen der Daten
		for($x = 0; $x < $anzahl; $x++)
			{
			$rep_old = "$inhalt[$nr]";
			$inhalt[$nr] = str_replace("$rep_old","$rep_neu",$inhalt[$nr]);
			}
		
		// Neue Daten hinein schreiben.
		
		$fp = fopen($datei,"w");
		for($i = 0; $i < $anzahl; $i++)
			{ 
			$schreibe[$i] = trim($inhalt[$i]);
			fwrite($fp,$schreibe[$i]."\n", 1024);
			}
		
		fclose($fp);
		
	}
}


if(file_exists($datei))
	{
	$inhalt = file($datei,"r");
	$anzahl = count($inhalt);
		for($i = 0; $i < $anzahl; $i++)
		{
			$eintrag = explode("|-|",$inhalt[$i]); //Inhalt zerteilen
			// Variablen definieren
			$tag = $eintrag[0];
			$datum = $eintrag[1];
			$zeit = $eintrag[2];
			$stueck = $eintrag[3];
			$ort = $eintrag[4];
			$location = $eintrag[5];
			$reservation = $eintrag[6];
			$eintragnummer = $i;
		
			// Daten ausgeben
			echo"<center><table width=\"650\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td width=\"10\" align=\"center\" valign=\"top\">&nbsp;</td><td align=\"left\" valign=\"top\" width=\"30\">$tag</td><td width=\"10\" align=\"center\" valign=\"top\">&nbsp;</td><td align=\"left\" valign=\"top\" width=\"70\">$datum</td><td width=\"10\" align=\"center\" valign=\"top\">&nbsp;</td><td align=\"left\" valign=\"top\" width=\"50\">$zeit</td><td width=\"10\" align=\"center\" valign=\"top\">&nbsp;</td><td align=\"left\" valign=\"top\" width=\"150\">$stueck</td><td width=\"10\" align=\"center\" valign=\"top\">&nbsp;</td><td align=\"left\" valign=\"top\" width=\"100\">$ort</td><td width=\"10\" align=\"center\" valign=\"top\">&nbsp;</td><td align=\"left\" valign=\"top\" width=\"100\">$location</td><td width=\"10\">&nbsp;</td><td align=\"left\" valign=\"top\" width=\"*\">$reservation</td><td width=\"10\">&nbsp;</td><td align=\"left\" valign=\"top\" width=\"10\">$eintragnummer</td></tr></table>\n</center>";
		}
	} 
	else 
	{ 
	echo"Es wurde keine Datei gefunden"; 
	}

Mit der BNR gibt man die Nummer der Zeile an, in der man den Eintrag ändern möchte. Die BNR wird auch als Variable "$eintragnummer" in der Ausgabe angezeigt.

Ich hoffe, dass mir jemand weiter helfen kann.

Gruss Jonas
 

Neue Beiträge

Zurück