Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Zapisywanie danyvh do txt - puste pola w formularzu.
-Gość_Bart_*-
post 24.06.2006, 12:00:39
Post #1





Goście







Mam taki oto skrypt jak poniżej, zapisuje ondane do pliku txt, później inny skrypt te dane odczytuje. W tej chwili gdy dane pole no: pole1 zostanie puste (tn nic do niego nie wpiszę) skrypt w pliku txt zastępuje starą wartość pola1 na nową czyli na nic (bo nic nie wpisaliśmy) czy jest możliwość by w takim przypadku pozostawiał on starą wartość ?

  1. <?php
  2. $pole1 = trim($_POST['pole1']);
  3. $pole2 = trim($_POST['pole2']);
  4. $pole3 = trim($_POST['pole3']);
  5. $pole4 = trim($_POST['pole4']);
  6. $pole5 = trim($_POST['pole5']);
  7. $pole6 = trim($_POST['pole6']);
  8. $pole7 = trim($_POST['pole7']);
  9. $pole8 = trim($_POST['pole8']);
  10. $pole9 = trim($_POST['pole9']);
  11. $pole10 = trim($_POST['pole10']);
  12. $pole11 = trim($_POST['pole11']);
  13. $pole12 = trim($_POST['pole12']);
  14.  
  15.  
  16. if(empty($pole1) and empty($pole2) and empty($pole3) and empty($pole4) and empty($pole5) and empty($pole6) and empty($pole7) and empty($pole8) and empty($pole9) and empty($pole10) and empty($pole11) and empty($pole12)) {
  17.  
  18.  
  19. echo '<form action="" method="post">
  20. <textarea name="pole1" style="width: 80px; height: 20px;"></textarea><textarea name="pole2" style="width: 80px; height: 20px;"></textarea><br>
  21. <textarea name="pole3" style="width: 80px; height: 20px;"> </textarea><textarea name="pole4" style="width: 80px; height: 20px;"></textarea><br>
  22. <textarea name="pole5" style="width: 80px; height: 20px;"> </textarea><textarea name="pole6" style="width: 80px; height: 20px;"></textarea><br>
  23. <textarea name="pole7" style="width: 80px; height: 20px;"> </textarea><textarea name="pole8" style="width: 80px; height: 20px;"></textarea><br>
  24. <textarea name="pole9" style="width: 80px; height: 20px;"> </textarea><textarea name="pole10" style="width: 80px; height: 20px;"></textarea><br>
  25. <textarea name="pole11" style="width: 80px; height: 20px;"> </textarea><textarea name="pole12" style="width: 80px; height: 20px;"></textarea><br>
  26.  
  27. <input type="submit" value="Zapisz" />
  28. </form>';
  29. }
  30. else {
  31.  
  32.  
  33. $pole1 = str_replace("n","<br>",$pole1);
  34. $pole2 = str_replace("n","<br>",$pole2);
  35. $pole3 = str_replace("n","<br>",$pole3);
  36. $pole4 = str_replace("n","<br>",$pole4);
  37. $pole5 = str_replace("n","<br>",$pole5);
  38. $pole6 = str_replace("n","<br>",$pole6);
  39. $pole7 = str_replace("n","<br>",$pole7);
  40. $pole8 = str_replace("n","<br>",$pole9);
  41. $pole9 = str_replace("n","<br>",$pole9);
  42. $pole10 = str_replace("n","<br>",$pole10);
  43. $pole11 = str_replace("n","<br>",$pole11);
  44. $pole12 = str_replace("n","<br>",$pole12);
  45.  
  46.  
  47.  $dane = "$pole1`$pole2`$pole3`$pole4`$pole5`$pole6`$pole7`$pole8`$pole9`$pole10`$pol
    e11`$pole12`"
    ;
  48.  
  49.  $file = "baza.txt";
  50.  $fp = fopen($file, "w");
  51.  flock($fp, 2);
  52.  fwrite($fp, $dane);
  53.  flock($fp, 3);
  54.  fclose($fp);
  55.  echo "Dane zostały zapisane!<br />";
  56.  }
  57. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 9)
piotrekkr
post 24.06.2006, 12:08:07
Post #2





Grupa: Zarejestrowani
Postów: 386
Pomógł: 25
Dołączył: 28.09.2005

Ostrzeżenie: (0%)
-----


  1. <?php
  2. if(!empty($_POST['pole1'])){
  3. //zapisz do pliku
  4. }else{
  5. //np qyswietl komunikat ze puste pole
  6. };
  7. ?>


aha i jeszcze zamiazt
  1. <?php
  2. str_replace("n","<br>",$pole1);
  3. ?>

uzywaj:
  1. <?php
  2. nl2br($pole1);
  3. ?>


Ten post edytował piotrekkr 24.06.2006, 12:11:18
Go to the top of the page
+Quote Post
-Gość-
post 24.06.2006, 12:10:08
Post #3





Goście







Dzięki, ale nie do końca o to mi chodziło. Gdyż ja chcę by w przypadku gdy pole1 jest puste w pliku txt pozostawała stara wartość tego pola1 czyli zapisana np: wczoraj.

Ewentualnie czy da radę, by w polach formularzy wyświetlały się wartości, które są w pliku baza.txt ?
Go to the top of the page
+Quote Post
piotrekkr
post 24.06.2006, 12:12:43
Post #4





Grupa: Zarejestrowani
Postów: 386
Pomógł: 25
Dołączył: 28.09.2005

Ostrzeżenie: (0%)
-----


a rozdzielasz czyms te dane w pliku jakims konkretnym znakiem??

jesli chodzi o wyswietlanie tego w formularzu to da rade jesli czyms poszczegolne pola w pliku txt sa rozdzielone np znaczkiem | czyli
Kod
pole1 | pole2 | pole3 itd


Ten post edytował piotrekkr 24.06.2006, 12:14:29
Go to the top of the page
+Quote Post
-Gość-
post 24.06.2006, 12:39:09
Post #5





Goście







W txt pola poodzielane są "`" .
Go to the top of the page
+Quote Post
piotrekkr
post 24.06.2006, 12:45:22
Post #6





Grupa: Zarejestrowani
Postów: 386
Pomógł: 25
Dołączył: 28.09.2005

Ostrzeżenie: (0%)
-----


a do jest jedna linia czy ilosc linii w pliku sie zmienia??
Go to the top of the page
+Quote Post
-Gość-
post 24.06.2006, 13:08:20
Post #7





Goście







Wszystko jest w jednej linii.
Go to the top of the page
+Quote Post
piotrekkr
post 24.06.2006, 13:25:55
Post #8





Grupa: Zarejestrowani
Postów: 386
Pomógł: 25
Dołączył: 28.09.2005

Ostrzeżenie: (0%)
-----


odczytujesz sobie plik i uzywasz :
  1. <?php
  2. $pola=explode("`",$tekst_z_pliku);
  3. ?>

i teraz masz
  1. <?php
  2. //pierwsze pole
  3. $pola[0];
  4. //drugie 
  5. $pola[1]
  6. //itd
  7. ?>


zeby je wyswietlic w formularzu wystarczy w tych polach
  1. <?php
  2. echo '<textarea name="pole1" style="width: 80px; height: 20px;">'.$pola[0].'</textarea>';
  3. echo '<textarea name="pole2" style="width: 80px; height: 20px;">'.$pola[1].'</textarea>'; //itd
  4. ?>


no i teraz masz pola zapelnione starymi danymi jak cos ktos zmieni to poprostu czyscisz plik i wpisujesz wartosci z textarea do pliku

Ten post edytował piotrekkr 24.06.2006, 13:31:52
Go to the top of the page
+Quote Post
-Gość-
post 24.06.2006, 13:36:38
Post #9





Goście







Ok, wszystko rozumiem chyba, ale $tekst_z_pliku to jest w moim przpadku będzie co ?
Go to the top of the page
+Quote Post
-Gość-
post 24.06.2006, 13:45:31
Post #10





Goście







Ok, już wszystko jasne :-)
Dzięki za pomoc!
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 02:00