<h2>Skrypt tworzy księgę gości z danymi przechowywanymi w pliku.
Do działania potrzebuje pliku txt</h2>
//Plik guestbook.inc ##
<?PHP
$ile = 2;
$rek_size = 5;
function skipRekord($fp, $rek_size)
{
for($i = 0; $i < $rek_size; $i++){
fgets($fp);
if(feof($fp)){
break;
}
}
}
function readGuestBook($from, $ile, $rek_size)
{
$contents = "";
if(($fp = fopen("guestbook.txt", "r")) === false)
return false;
for($i = 0; $i < $from; $i++){
skipRekord($fp, $rek_size);
}
for($i = $from * $rek_size; $i < ($from + $ile) * $rek_size; $i++){
if(feof($fp)){
break;
}
$contents .= fgets($fp)."<BR>";
}
fclose($fp);
return substr($contents, 0, strlen($contents) - 4);
}
function addToGuestBook($imie, $nazwisko, $email, $contents)
{
if(($fp = @fopen("guestbook.txt", "r")) === false)
return false;
$tempC = fread($fp, filesize("guestbook.txt"));
fclose($fp);
$fp = fopen("guestbook.txt", "w");
fputs($fp, $imie."\r\n");
fputs($fp, $nazwisko."\r\n");
fputs($fp, $email."\r\n");
fputs($fp, $contents."\r\n");
fputs($fp, "\r\n");
fputs($fp, $tempC);
fclose($fp);
}
if(isSet($_GET["from"])){
$from = $_GET["from"];
}
else{
$from = 0;
}
if($from <= 0){
$from = 0;
$prev = 0;
$next = $ile;
}
else{
$prev = $from - $ile;
$next = $from + $ile;
}
if(isSet($_POST["imie"])){
$imie = $_POST["imie"];
}
else{
$imie = "";
}
if(isSet($_POST["nazwisko"])){
$nazwisko = $_POST["nazwisko"];
}
else{
$nazwisko = "";
}
if(isSet($_POST["email"])){
$email = $_POST["email"];
}
else{
$email = "";
}
if(isSet($_POST["contents"])){
$contents = $_POST["contents"];
}
else{
$contents = "";
}
if($imie == "" && $nazwisko == ""
&& $email == "" && $contents == ""){
$tempStr = readGuestBook($from, $ile, $rek_size);
if($tempStr == "" || $tempStr == "\r\n"){
print("Koniec wpisów"."<BR>");
$next -= $ile;
}
else{
print($tempStr);
}
}
else{
addToGuestBook($imie, $nazwisko, $email, $contents);
print(readGuestBook($from, $ile, $rek_size));
}
$code = <<<TEMP
<A HREF="guestbook.php?from=$prev">Poprzednie</A>
<A HREF="guestbook.php?from=$next">Następne</A>
TEMP;
echo("$code");
?>
Plik add.inc
<FORM ACTION="http://127.0.0.1/guestbook.php"
METHOD="POST"
>
<TABLE BORDER="0"
ALIGN="center"
STYLE="background-color:yellow;"
>
<TR>
<TD>Imię:</TD>
<TD>
<INPUT TYPE="text" NAME="imie" STYLE="width:150">
</TD>
<TD>Nazwisko:</TD>
<TD>
<INPUT TYPE="text" NAME="nazwisko" STYLE="width:152">
</TD>
</TR><TR>
<TD>Adres email:</TD>
<TD COLSPAN="3">
<INPUT TYPE="text" NAME="email" STYLE="width:380">
</TD>
</TR><TR>
<TD>Wpis:</TD>
<TD COLSPAN="3">
<TEXTAREA NAME="contents" rows="5" cols="45" STYLE="width:380">
</TEXTAREA>
</TD>
</TR><TR>
<TD COLSPAN="4" ALIGN="center">
<INPUT TYPE="submit" VALUE="Dodaj">
</TD>
</TR>
</TABLE>
</FORM>
Gotowe rozwiązanie.
<?PHP
$ile = 2;
$rek_size = 5;
function skipRekord($fp, $rek_size)
{
for($i = 0; $i < $rek_size; $i++){
fgets($fp);
if(feof($fp)){
break;
}
}
}
function readGuestBook($from, $ile, $rek_size)
{
$contents = "";
if(($fp = fopen("guestbook.txt", "r")) === false)
return false;
for($i = 0; $i < $from; $i++){
skipRekord($fp, $rek_size);
}
for($i = $from * $rek_size; $i < ($from + $ile) * $rek_size; $i++){
if(feof($fp)){
break;
}
$contents .= fgets($fp)."<BR>";
}
fclose($fp);
return substr($contents, 0, strlen($contents) - 4);
}
function addToGuestBook($imie, $nazwisko, $email, $contents)
{
if(($fp = @fopen("guestbook.txt", "r")) === false)
return false;
$tempC = fread($fp, filesize("guestbook.txt"));
fclose($fp);
$fp = fopen("guestbook.txt", "w");
fputs($fp, $imie."\r\n");
fputs($fp, $nazwisko."\r\n");
fputs($fp, $email."\r\n");
fputs($fp, $contents."\r\n");
fputs($fp, "\r\n");
fputs($fp, $tempC);
fclose($fp);
}
if(isSet($_GET["from"])){
$from = $_GET["from"];
}
else{
$from = 0;
}
if($from <= 0){
$from = 0;
$prev = 0;
$next = $ile;
}
else{
$prev = $from - $ile;
$next = $from + $ile;
}
if(isSet($_POST["imie"])){
$imie = $_POST["imie"];
}
else{
$imie = "";
}
if(isSet($_POST["nazwisko"])){
$nazwisko = $_POST["nazwisko"];
}
else{
$nazwisko = "";
}
if(isSet($_POST["email"])){
$email = $_POST["email"];
}
else{
$email = "";
}
if(isSet($_POST["contents"])){
$contents = $_POST["contents"];
}
else{
$contents = "";
}
if($imie == "" && $nazwisko == ""
&& $email == "" && $contents == ""){
$tempStr = readGuestBook($from, $ile, $rek_size);
if($tempStr == "" || $tempStr == "\r\n"){
print("Koniec wpisów"."<BR>");
$next -= $ile;
}
else{
print($tempStr);
}
}
else{
addToGuestBook($imie, $nazwisko, $email, $contents);
print(readGuestBook($from, $ile, $rek_size));
}
$code = <<<TEMP
<A HREF="guestbook.php?from=$prev">Poprzednie</A>
<A HREF="guestbook.php?from=$next">Następne</A>
TEMP;
echo("$code");
?>
//Plik add.inc ###########################################
<FORM ACTION="http://127.0.0.1/guestbook.php"
METHOD="POST"
>
<TABLE BORDER="0"
ALIGN="center"
STYLE="background-color:yellow;"
>
<TR>
<TD>Imię:</TD>
<TD>
<INPUT TYPE="text" NAME="imie" STYLE="width:150">
</TD>
<TD>Nazwisko:</TD>
<TD>
<INPUT TYPE="text" NAME="nazwisko" STYLE="width:152">
</TD>
</TR><TR>
<TD>Adres email:</TD>
<TD COLSPAN="3">
<INPUT TYPE="text" NAME="email" STYLE="width:380">
</TD>
</TR><TR>
<TD>Wpis:</TD>
<TD COLSPAN="3">
<TEXTAREA NAME="contents" rows="5" cols="45" STYLE="width:380">
</TEXTAREA>
</TD>
</TR><TR>
<TD COLSPAN="4" ALIGN="center">
<INPUT TYPE="submit" VALUE="Dodaj">
</TD>
</TR>
</TABLE>
</FORM>
//Główny skrypt przetwarzający guestbook.php ##############
<?PHP
$ile = 2;
$rek_size = 5;
function skipRekord($fp, $rek_size)
{
for($i = 0; $i < $rek_size; $i++){
fgets($fp);
if(feof($fp)){
break;
}
}
}
function readGuestBook($from, $ile, $rek_size)
{
$contents = "";
if(($fp = fopen("guestbook.txt", "r")) === false)
return false;
for($i = 0; $i < $from; $i++){
skipRekord($fp, $rek_size);
}
for($i = $from * $rek_size; $i < ($from + $ile) * $rek_size; $i++){
if(feof($fp)){
break;
}
$contents .= fgets($fp)."<BR>";
}
fclose($fp);
return substr($contents, 0, strlen($contents) - 4);
}
function addToGuestBook($imie, $nazwisko, $email, $contents)
{
if(($fp = @fopen("guestbook.txt", "r")) === false)
return false;
$tempC = fread($fp, filesize("guestbook.txt"));
fclose($fp);
$fp = fopen("guestbook.txt", "w");
fputs($fp, $imie."\r\n");
fputs($fp, $nazwisko."\r\n");
fputs($fp, $email."\r\n");
fputs($fp, $contents."\r\n");
fputs($fp, "\r\n");
fputs($fp, $tempC);
fclose($fp);
}
if(isSet($_GET["from"])){
$from = $_GET["from"];
}
else{
$from = 0;
}
if($from <= 0){
$from = 0;
$prev = 0;
$next = $ile;
}
else{
$prev = $from - $ile;
$next = $from + $ile;
}
if(isSet($_POST["imie"])){
$imie = $_POST["imie"];
}
else{
$imie = "";
}
if(isSet($_POST["nazwisko"])){
$nazwisko = $_POST["nazwisko"];
}
else{
$nazwisko = "";
}
if(isSet($_POST["email"])){
$email = $_POST["email"];
}
else{
$email = "";
}
if(isSet($_POST["contents"])){
$contents = $_POST["contents"];
}
else{
$contents = "";
}
if($imie == "" && $nazwisko == ""
&& $email == "" && $contents == ""){
$tempStr = readGuestBook($from, $ile, $rek_size);
if($tempStr == "" || $tempStr == "\r\n"){
print("Koniec wpisów"."<BR>");
$next -= $ile;
}
else{
print($tempStr);
}
}
else{
addToGuestBook($imie, $nazwisko, $email, $contents);
print(readGuestBook($from, $ile, $rek_size));
}
$code = <<<TEMP
<A HREF="guestbook.php?from=$prev">Poprzednie</A>
<A HREF="guestbook.php?from=$next">Następne</A>
TEMP;
echo("$code");
?>