Jump to content
php.lv forumi

Mini guestbook


Gacha

Recommended Posts

Nu te ir no mazinja guestbook faila add.php kods ar kuru tiek ievietots posts text failaa

<?php
if ($message != '') 
{
 $message = str_replace ("\n","<br>",$message);
 $message = strip_tags ($message, '<br>');

 $newRow = '<div class="viewGuestbook">' . ($message) .
      '<br>' . date('d.m.Y H:i') . ' - ' . strip_tags ($name) .
      '</div>';
        
 $oldRows = join ('', file ('guestbook.txt') );
 $fileName = fopen ('guestbook.txt', 'w');
 fputs ($fileName, $newRow . chr(13) . chr(10) . $oldRows);
 fclose ($fileName);
}

include ("readbook.php");
?>

Bet kaa vareetu uztaisit lai var arii izdzest postus?

 

PS. Man jau daudzi ir teikushi lai izmantoju MySQL bet mans to nerubi un nav kas paskaidro. Domaju, ka varu izmantot texta failu, jo mana lapa ir lloti mazinja.

Link to comment
Share on other sites

Varētu būt diezgan ķēpīgi...

Bet ja vēlies varētu idejiski būt šāds variants:

Dzēšanas lapai padod ciparu $k, kurš apzīmē komentāru pēc kārtas no faila sākuma.

Komentārus varētu, piemēram samest masīvā pēc pazīmes "<div class='viewGuestbook'>"

kaut kas līdzīgs $mas = explode("<div class='viewGuestbook'>", file('guestbook.txt'));

Un tad vienkārši izmest no masīva $k - to elementu.

Visbeidzot visu masīvu implode stringā un stringu saglabāt pāri vecajiem guestbook.txt datiem.

 

P.S. visdrīzāk, ka tas explode šādi nedarbosies, bet nu "you get the point" :D

Link to comment
Share on other sites

Nu es apmēram saprotu, kā tas notiek, bet nezinu kā to lai uztaisa. Cik no tā koda gabala sapratu tad viņš tos postus šķiro pēc līnijām, kuras viņš iemet katru reizi aiz posta.

Link to comment
Share on other sites

Nu te ir viens kods, varbūt jūs te kaut ko saprotat

<?php

 

function showArray ($dataArray) {

if (is_array($dataArray)) {

foreach ($dataArray as $rowData) { echo $rowData."<br>"; }

}

}

 

 

 

# openFile look at a data file ($dataFile) and load information into data array ($dataArray)

# - it returns 1 (true) if $dataFile is written into $dataArray successfully

# - it returns 0 (false) if the $dataFile does not exists.

function openFile ($dataFile, &$dataArray, &$error) {

if (!file_exists($dataFile)) {

$error = 1; # dataFile does not exists

return 0; # abort and return 0

}

 

if (!filesize($dataFile)) {

$error = 2; # dataFile is zero length

return 0; # abort and return 0

}

 

# dataFile does exists

$dataArray = file($dataFile);

$error = 0;

return 1;

}

 

 

 

# writeFile look at a data file ($dataFile) and write information in data array ($dataArray) to it

# - it returns 1 (true) if $dataArray is written into $dataFile successfully

# - it returns 0 (false) if $dataArray is not an array (1).

function writeFile ($dataFile, $dataArray, &$error) {

if (!is_array($dataArray)) {

$error = 1; # dataArray is not an array

return 0; # abort and return 0

}

 

# no error

$str = "";

foreach ($dataArray as $rowData) {

$temp = trim($rowData);

$temp = str_replace ("\n","<br>",$temp);

$str .= trim($temp)."\n";

}

 

@chmod($dataFile, 0766);

$fp = fopen($dataFile,"w");

fwrite ($fp, $str);

fclose ($fp);

 

$error = 0;

return 1;

}

 

 

 

# appendFile look at a data file ($dataFile) and append ONE row of information ($rowData) to it

# - it has no return value

function appendFile ($dataFile, $rowData) {

$temp = trim($rowData);

$temp = str_replace ("\n","<br>",$temp);

$str = trim($temp)."\n";

 

@chmod($dataFile, 0766);

$fp = fopen($dataFile,"a");

fwrite ($fp, $str);

fclose ($fp);

}

 

 

 

# deleteRow look at a data file ($dataFile) and delete ONE row by its row number ($rowID)

# - it returns 1 (true) if $rowID is found in the $dataFile and removed successfully

# - it returns 0 (false) if (1) the $dataFile does not exists, or (2) the $rowID is out of bound.

function deleteRow ($dataFile, $rowID, &$error) {

if (openFile ($dataFile, $dataArray, $error)) {

if ($rowID > count($dataArray)) {

$error = 2; # row does not exists

return 0; # abort and return 0

}

 

array_splice ($dataArray, $rowID, 1);

if (writeFile ($dataFile, $dataArray, $error)) {

$error = 0;

return 1;

}%0

Link to comment
Share on other sites

Nu te ir viens kods, varbūt jūs te kaut ko saprotat

<?php

 function showArray ($dataArray) {
   if (is_array($dataArray)) {
     foreach ($dataArray as $rowData) { echo  $rowData."<br>"; }
   }
 }



# openFile look at a data file ($dataFile) and load information into data array ($dataArray)
# - it returns 1 (true) if $dataFile is written into $dataArray successfully
# - it returns 0 (false) if the $dataFile does not exists.
function openFile ($dataFile, &$dataArray, &$error) {
   if (!file_exists($dataFile)) {
     $error = 1; # dataFile does not exists
     return 0;   # abort and return 0
   }

   if (!filesize($dataFile)) {
     $error = 2; # dataFile is zero length
     return 0;   # abort and return 0
   }

 # dataFile does exists
   $dataArray = file($dataFile);
   $error = 0;
   return 1;
}



# writeFile look at a data file ($dataFile) and write information in data array ($dataArray) to it
# - it returns 1 (true) if $dataArray is written into $dataFile successfully
# - it returns 0 (false) if $dataArray is not an array (1).
function writeFile ($dataFile, $dataArray, &$error) {
   if (!is_array($dataArray)) {
     $error = 1; # dataArray is not an array
     return 0;   # abort and return 0
   }

 # no error
  $str = "";
  foreach ($dataArray as $rowData) {
	 $temp = trim($rowData);
	 $temp = str_replace ("\n","<br>",$temp);
	 $str .= trim($temp)."\n";
 }

 @chmod($dataFile, 0766);
  $fp  = fopen($dataFile,"w");
  fwrite ($fp, $str);
  fclose ($fp);

   $error = 0;
 return 1;
}



# appendFile look at a data file ($dataFile) and append ONE row of information ($rowData) to it
# - it has no return value
function appendFile ($dataFile, $rowData) {
 $temp = trim($rowData);
 $temp = str_replace ("\n","<br>",$temp);
 $str = trim($temp)."\n";

 @chmod($dataFile, 0766);
  $fp  = fopen($dataFile,"a");
  fwrite ($fp, $str);
  fclose ($fp);
}



# deleteRow look at a data file ($dataFile) and delete ONE row by its row number ($rowID)
# - it returns 1 (true) if $rowID is found in the $dataFile and removed successfully
# - it returns 0 (false) if (1) the $dataFile does not exists, or (2) the $rowID is out of bound.
function deleteRow ($dataFile, $rowID, &$error) {
   if (openFile ($dataFile, $dataArray, $error)) {
     if ($rowID > count($dataArray)) {
       $error = 2; # row does not exists
       return 0;   # abort and return 0
     }

     array_splice ($dataArray, $rowID, 1);
     if (writeFile ($dataFile, $dataArray, $error)) {
       $error = 0;
     return 1;
     }
   } else { return 0; }
}



# deleteRow look at a data file ($dataFile) and delete ONE row by its row number ($rowID)
# - it returns 1 (true) if $rowID is found in the $dataFile and removed successfully
# - it returns 0 (false) if (1) the $dataFile does not exists, or (2) the $rowID is out of bound.
function updateRow ($dataFile, $rowID, $rowData, &$error) {
   if (openFile ($dataFile, $dataArray, $error)) {
     if ($rowID > count($dataArray)) {
       $error = 2; # row does not exists
       return 0;   # abort and return 0
     }
     array_splice ($dataArray, $rowID, 1, $rowData);
     if (writeFile ($dataFile, $dataArray, $error)) {
       $error = 0;
     return 1;
     }
   } else { return 0; }
}



# findRows look at a data file ($dataFile) for a non case-sensitive search string ($needle)
# - it returns 1 (true) if $needle is found in the $dataFile and return the result in a data
#   array ($resultArray).  Each row in the $resultArray contains the row number and row data.
# - it returns 0 (false) if (1) the $dataFile does not exists, or (2) the $needle is not found.
 function findRows ($dataFile, $needle, &$resultArray, &$error) {
   if (openFile ($dataFile, $dataArray, $error)) {
     $needle = strtolower($needle);
     $i = 0;
     $rowNumber = 0;

     foreach ($dataArray as $rowData) {
       if (substr_count(strtolower($rowData), $needle)) {
         $resultArray[$i] = array($rowNumber, $rowData);
         $i++;
       }
       $rowNumber++;
     }

     if ($i) {
       $error = 0; # match founds, NO error 
       return 1;   
     } else {
       $error = 2; # NO match founds
       return 0; 
     }
   } else { return 0; }
 }
?>

Link to comment
Share on other sites

ja pareizi esmu izpratis, tad drukāšanu ārā veic šī koda daļa:

 function showArray ($dataArray) {
  if (is_array($dataArray)) {
    foreach ($dataArray as $rowData) { echo  $rowData."<br>"; }
  }
}

lai blakus būtu dzēšanas poga izmaini to kaut kā šādi:

 function showArray ($dataArray) {
  if (is_array($dataArray)) {
    $n=0;
    foreach ($dataArray as $rowData) { echo  $rowData."<input type=buton onclick='delete.php?id=". $n ."' value='Izdzēst'><br>";
    $n++;   }
    }
}

Un lapā delete.php tiek izmantota funkcija deleteRow()..., kurai tiek padota id vērtība, kas arī ir izdzēšamās rindas numurs

Edited by Aleksejs
Link to comment
Share on other sites

Nee, tas ko tu tur izgriezi tas ir no pedejaa posta. Tas pedejais koda gabals ir kaut kaads scripts par flat failiem, bet mana scriptaa jauna rinda ir apm taa

$filename = "data.txt";
$datums  = date("d.m.y - H:i");
$title = $_POST['title'];
$message = $_POST['message'];
$inputstring = "<table width='200' border='1' cellpadding='0' cellspacing='0' bgcolor='#999999'>
 <tr>
   <td bgcolor='#000066'><div align='center'><font color='#CCCCCC'><strong>$title</strong></font></div></td>
 </tr>
 <tr>
   <td>$message</td>
 </tr>
</table>
<table width='200' border='0' cellspacing='0' cellpadding='0'>
 <tr>
   <td> $datums</td>
 </tr>
</table>";
$fp = fopen ( $filename,"w");
fwrite ( $fp, "$inputstring\n");

fclose ($fp);

Te tam visam prosta pa vidu ir $title un $message un varetu pielikt podzinju.

Tur daudz varetu but nepareizi, jo shis ir mans pirmais darbs uz php.

Link to comment
Share on other sites

×
×
  • Create New...