Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [txt,php] edycja tekstowej bazy danych
papik
post 17.09.2006, 00:53:41
Post #1





Grupa: Zarejestrowani
Postów: 15
Pomógł: 0
Dołączył: 30.08.2006
Skąd: Libiąż

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


Stworzyłem przykładową bazę tekstową:

1|imieA|nazwiskoA|hasloA
2|imieB|nazwiskoB|hasloB
3|imieC|nazwiskoC|hasloC

Wiem już jak odczytać określony wpis z wybranego rekordu, ale nie mogę dojść do tego jak po zmodyfikowaniu wpisu (np. hasloB) zapisac go ponownie w bazie.

Proszę o wyrozumiałość i wskazówki dla błądzącego w ciemnościach.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
dr_bonzo
post 17.09.2006, 02:09:37
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


A nie prosciej uzyc bazy danych?


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
sf
post 17.09.2006, 07:11:57
Post #3





Grupa: Zarejestrowani
Postów: 1 597
Pomógł: 30
Dołączył: 19.02.2003
Skąd: Tychy

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


Hm...

cos = file( plik );

wyszukanie i zmiana na wybranej linijce cos

ponowne zapisanie bazy danych fwrite(fp, implode("\n", cos));

Ten post edytował sf 17.09.2006, 07:12:56


--------------------
Zapraszam na mój php blog, tworzenie stron.
Go to the top of the page
+Quote Post
php programmer
post 17.09.2006, 07:43:54
Post #4





Grupa: Zarejestrowani
Postów: 1 045
Pomógł: 5
Dołączył: 8.11.2004
Skąd: trójmiasto

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


Oto fragment rzeczywistego sprawdzonego kodu,
oczywiście jeśli jest bardzo dużo danych to lepiej użyć
prawdziwej bazy danych, ale w tamtym przypadku nie było
tylko kilkadziesiąt wierszy:

  1. <?php
  2. $labels['this']  = 'dystrybutorzy.php';
  3. $labels['database'] = 'dystrybutorzy.db';
  4. $labels['text']  = 'dystrybutorzy.txt';
  5.  
  6. $labels['nazwa']  = 'Nazwa Firmy';
  7. $labels['adres']  = 'Adres';
  8. $labels['miasto'] = 'Miasto';
  9. $labels['kod']  = 'Kod pocztowy';
  10. $labels['telefon'] = 'Numer Telefonu';
  11. $labels['delete'] = 'Czy na pewno chcesz to skasować?';
  12. $labels['cancel'] = 'Anuluj';
  13. $labels['submit'] = 'OK';
  14.  
  15.  
  16. require("javascript.php");
  17. PostToJavaScript($labels);
  18.  
  19. if (!isset($_GET['action']))
  20.  $_GET['action'] = 'show';
  21.  
  22. if ($_GET['action']=='show')  Show();
  23. if ($_GET['action']=='edit')  Edit();
  24. if ($_GET['action']=='update') Update($_GET['id']);
  25. if ($_GET['action']=='delete') Remove($_GET['id']);
  26. if ($_GET['action']=='tekst') Editor();
  27. if ($_GET['action']=='save')  Save();
  28.  
  29. function Show(){
  30. global $labels;
  31.  
  32. gora();
  33. $plik = file($labels['database']);
  34. echo '<h2>Lista dystrybutorów:</h2>';
  35.  
  36. echo '<div style="height:25px;font-size:12px">';
  37. echo '<a href="'.$labels['this'].'?action=edit">Dodaj nowego </a>';
  38. echo '&nbsp;&nbsp;&nbsp;';
  39. echo '<a href="'.$labels['this'].'?action=tekst">Edytuj tekst poczatkowy</a>';
  40. echo '</div><br />';
  41.  
  42. foreach ($plik as $row){
  43. $row = unserialize($row);
  44. echo '<div style="height:25px;font-size:12px">';
  45. echo '<a href="java script:Delete('.$row['id'].')">';
  46. echo '<img src="../layout/delete.png" border="0" align="left" /></a>';
  47. echo '&nbsp;&nbsp;';
  48. echo '<a href="'.$labels['this'].'?action=edit&id='.$row['id'].'">';
  49. echo $row['nazwa'];
  50. echo '</a></div>';
  51. }
  52. }
  53.  
  54.  
  55. function InputText($name,$row=''){
  56. global $labels;
  57. echo '<table border="0" width="480px">';
  58. echo '<tr><td width="150px" style="font-size:12px">';
  59. echo $labels[$name];
  60. echo '</td><td align="right">';
  61. echo '<input name="'.$name.'" id="'.$name.'" value="'.$row[$name].'" style="width:320px">';
  62. echo '</td></tr></table>';
  63. }
  64.  
  65. function InputSubmit(){
  66. global $labels;
  67. echo '<table border="0" width="480px">';
  68. echo '<tr><td align="right">';
  69. echo '<input type="button" value="'.$labels['cancel'].'" style="width:80px" onClick="location=''.$labels['this'].''">';
  70. echo '<input type="submit" value="'.$labels['submit'].'" style="width:80px">';
  71. echo '</td></tr></table>';
  72. }
  73.  
  74. function Edit(){
  75. global $labels;
  76.  
  77. gora();
  78. echo '<h3>Edycja wpisu:</h3>';
  79. echo '<form action="'.$labels['this'].'?action=update&id='.$_GET['id'].'" method="post">';
  80.  
  81.  
  82. $plik = file($labels['database']);
  83. foreach ($plik as $row){
  84. $array = unserialize($row);
  85. if ($_GET['id']==$array['id']) {
  86.  $found = $array;
  87.  break;
  88.  }
  89. }
  90.  
  91. InputText('nazwa',$found);
  92. InputText('adres',$found);
  93. InputText('miasto',$found);
  94. InputText('stan',$found);
  95. InputText('kod',$found);
  96. InputText('telefon',$found);
  97. InputSubmit();
  98. echo '</form>';
  99. dol();
  100. }
  101.  
  102. function Update($id){
  103. global $labels;
  104.  
  105. if ($id) Remove($_POST['id']=$_GET['id'],false);
  106.  else  $_POST['id'] = time();
  107.  
  108. $row = serialize($_POST).chr(13).chr(10);
  109. $plik = fopen($labels['database'],'a');
  110. fputs($plik,$row);
  111. fclose($plik);
  112. Location($labels['this']);
  113. }
  114.  
  115. function Remove($id,$back=true){
  116. global $labels;
  117. $nowy = Array();
  118. $plik = file($labels['database']);
  119. foreach ($plik as $row){
  120. $array = unserialize($row);
  121. if ($id!=$array['id'])
  122. $nowy[] = $row;
  123. }
  124. $plik = fopen($labels['database'],'w');
  125. foreach ($nowy as $row)
  126.  fputs($plik,$row);
  127. fclose($plik);
  128. if ($back) Location($labels['this']);
  129. }
  130. ?>

Kod
<script language="javascript" type="text/javascript">

function Delete(id){
if (confirm(labels_delete))
   location = labels_this+'?action=delete&id='+id;
}
</script>

  1. <?php
  2. function Editor(){
  3. global $labels;
  4. gora();
  5.  
  6. $content = implode('',file($labels['text']));
  7.  
  8. require('../lib/editor/xina.php');
  9. ?>

  1. <form action="<?=$labels['this']?>?action=save" method="post">
  2. <textarea id="content" name="content" style="width:720px;height:480px;"><?=$content?></textarea>
  3.  
  4.  
  5. <div align="center">
  6. <input type="button" value="<?=$labels['cancel']?>" onClick="location=labels_this" style="width:80px">
  7. <input type="submit" value="<?=$labels['submit']?>" style="width:80px" >
  8. </div>
  9. </form>

  1. <?php
  2. dol();
  3. }
  4.  
  5.  
  6. function Save(){
  7. global $labels;
  8. $plik = fopen($labels['text'],'w');
  9. $content = str_replace('"','"',trim($_POST['content']));
  10. fputs($plik,$content);
  11. fclose($plik);
  12. Location($labels['this']);
  13. }
  14. ?>


Ten post edytował php programmer 17.09.2006, 07:49:26
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:58