Jump to content

how to create a blog (NOT use MySQL or any other databases)?


newbe123

Recommended Posts

I want to create a text file based blog ( not using MySQL or other databases).

I have tried to google for tutorials but I couldn't find anything.

is there anyone who can help me and give me some tips?

is there any tutorials about this because I found nothing?

Any good books?

 

This is an assignment that I have to do and I am using this book:

Spring into PHP 5 by Addison Wesley.

 

I am a newbie in php and really want to do this because this is my last task and I really want to learn. So I'm grateful if anyone can help me.

 

 

Link to comment
Share on other sites

http://www.aurorapod.com/tutorial.php?id=4

 

very weird tutorial and the code does not work.

 

blog.php

 

<body>
<?
// Determines File you want to use
$mode = 0;
if ($mode == 0) { $opFile = "blogfile.txt"; }
if ($mode == 1) { $opFile = "blogfile1.txt"; }

// Opens Blog File to read or dies
$fp = fopen($opFile,"r") or die("Error Reading File");
  $data = fread($fp, filesize($opFile));
fclose($fp);

// Explodes data at line breaks
$line = explode("\n", $data);
$i=count($line);

for ($n=0 ; $n < $i-1 ; $n++ ) {
  $blog = explode("|", $line[$n]);

  if (isset($blog[0]))
   {
    echo "Posted by : " .$blog[0]."<br>";
    echo "Date : " .$blog[1]."<br>";
    echo "Title : " .$blog[2]."<br>";
    echo "Message : " .$blog[3]."<br>";
    echo "Avatar : <img src='$blog[4]'><br><br>";

    }
}
   ?>
</body>

 

addmsg.php

 

<body>



<form action="post.php" method="post" name="post">
<input type="text" tabindex="1" name="name" size="25" maxlength="25" value="" />
<input type="text" name="subject" size="45" maxlength="60" style="width:450px" tabindex="2" value="" />
<textarea name="message" rows="10" cols="35" wrap="virtual" style="width:450px" tabindex="3"></textarea>
<input type="text" name="avatar" size="45" maxlength="120" style="width:450px" tabindex="2" value="" />
<input type=submit value="Submit" name=submit>
</form>



</body>

 

 

 

post.php

 

<body>
<?php

$filename = "blogfile.txt";

if (!isset($message)) {
$name = $_POST['name'];
//$date = $_POST['date'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$avatar  = $_POST['avatar'];
}

$name = htmlspecialchars(trim(stripslashes($name)));
$subject = htmlspecialchars(trim(stripslashes($subject)));
$message = htmlspecialchars(trim(stripslashes($message)));
$avatar = htmlspecialchars(trim(stripslashes($avatar)));
$message = trim($message);
$postdate = date('d M Y');
$message = str_replace("\n", "[br]", $message);
$message = str_replace("\r", "", $message);
$message = str_replace("|", "¦", $message);
$blog = $name."|".$postdate."|".$subject."|".$message."|".$avatar."[end]\n" ;

$data = fopen($filename, "a");
fwrite($data, $blog);
fclose($data);
echo "Data Entered";
?>
</body>

 

 

 

 

 

Link to comment
Share on other sites

you might want to cut the problem into different pieces...

 

1. form management

2. saving form input to file (I would personaly use XML for this... have a look at XMLReader and XMLWriter on php.net)

3. rendering the blog (parsing the XML files and displaying the data)

 

you'll find tutorials for all of these elements...

Link to comment
Share on other sites

I'll try but it's not easy when you are a beginner. I have created <form> but then I just can not anymore and I do not know where to begin. That's why I wanted to see some examples to get started.

Some examples are too complicated for me and then I get only more confused.

Link to comment
Share on other sites

I'll try but it's not easy when you are a beginner. I have created <form> but then I just can not anymore and I do not know where to begin. That's why I wanted to see some examples to get started.

Some examples are too complicated for me and then I get only more confused.

 

If you're not familiar with how HTML forms work, you might just start learning about that.  What you're wanting to do requires a base knowledge on a number of topics (listed above).  The best way to tackle this is to learn each part separately, then you can put it all together.  Start by making a page that has a form and echoes the submission back to the user.  Then build up from there.

 

The truth of the matter is, in my opinion anyway, using flat files is much more complicated and has a steeper learning curve than using MySQL.

Link to comment
Share on other sites

this is an assignment that I have to do otherwise it would not be so difficult if I could use MySQL. Here I must use the flat file. I started PHP for about 2 weeks ago and have done 3 other assignments and also received good help from this portal. But this task is the last and the most difficult and counts as a project. I really want to do this and hopefully learn.

 

Link to comment
Share on other sites

Here is a VERY basic simple example.

It does NOT have any sanitizing or security done to it. It is just for getting an extremely basic understanding.

 

there are 3 files PLUS the flat file for data.

the flat file uses the pipe "|" as the separator. It has four 'fields' - date (in timestamp format), who, title and content.

 

index.php

<?PHP
/* define the blog content file name */
$filename = "myBlogContent.txt";
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>My Blog</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
<body bgcolor="#ffffff" text="#000000" link="#000000" vlink="#000000" alink="#000000">

<!-- YOUR PAGE LOGO DIV -->
<div style="position:absolute; left: 10px; top: 10px;">
My Blog
</div>

<!-- MENU DIV -->
<div style="position:absolute; left: 10px; top: 100px;">
<a href="index.php">View</a><br>
<a href="add_content.html">Add</a>
</div>

<!-- CONTENT DIV -->
<div style="position:absolute; left: 100px; top: 100px; width: 400px;">
<?PHP
/* check to see if the file exists */
if (!file_exists($filename)) {
	echo "The Blog Is Empty";
}else{
/* get the file lines into an array */
$BlogArray = file($filename);
/* count the number of blog entries */
$count = count($BlogArray);
$i=0;
while($i<$count) {
	$new_array = explode("|", $BlogArray[$i]);
	echo "Posted by: " . $new_array[1] . "<br>";
	echo "Posted on: " .  date("m/d/y h:iA", time($new_array[0])) . "<br>";
	echo 	"Title: " . $new_array[2] . "<br>";
	echo $new_array[3] . "<hr>";
	$i ++;
}
}
?>
</div>
</body>
</html>

 

add_content.html

<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>My Blog - Add Content form Page</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
<body bgcolor="#ffffff" text="#000000" link="#000000" vlink="#000000" alink="#000000">

<!-- YOUR PAGE LOGO DIV -->
<div style="position:absolute; left: 10px; top: 10px;">
My Blog
</div>

<!-- MENU DIV -->
<div style="position:absolute; left: 10px; top: 100px;">
<a href="index.php">View</a><br>
<a href="add_content.html">Add</a>
</div>

<div style="position:absolute; left: 100px; top: 100px;">
<form action="add2blog.php" method="post">
<table>
	<tr><td>Blog entry posted by (Your name): </td><td><input type="text" name="who" size="20" maxlength="20" value=""></td></tr>
	<tr><td>Title of this blog entry: </td><td><input type="text" name="title" size="40" maxlength="80" value=""></td></tr>
	<tr><td>Content: </td><td><textarea name="content" rows="5" cols="40"></textarea></td></tr>
	<tr><td clospan="2"><input type="submit" value="Submit"></td></tr>
</table>
</form>
</div>
</body>
</html>

 

add2blog.php

<?PHP
/* obtain the form data */
$who = $_POST['who'];
$title = $_POST['title'];
$content = $_POST['content'];
$content = str_replace(array("\r\n", "\r", "\n"), "<br>", $content); 

/* create timestamp variable for current date and time */
$when_ts = time(); 

/* define the blog content file name */
$filename = "myBlogContent.txt";

/* prepare the variables for adding to the file */

$new_line_content = $when_ts . "|" . $who . "|" . $title . "|" . $content . "\n";

/* open the file in the APPEND MODE */
$fh = fopen($filename, 'a') or die("can't open file");

/* add the new content */
fwrite($fh, $new_line_content); 

/* close the file */
fclose($fh); 

/* return to the add page */
header ("Location: add_content.html"); 
exit; // Closes further script execution . 
?>

 

No bells, whistles, etc.

 

Hope it helps

 

Link to comment
Share on other sites

yes, now I have created the form and have written JavaScript, but just can not get started with PHP codes. I have tried but it just goes wrong and my brain is frozen. I think.

 

because this is an assignment that I must do, so there are many requirements. one of those that I mentioned before is that I can not use MySQL or other databases. There are loads of other requirements and I'd like some advice on how to begin.

 

Would like some tips.

 

blogglogin.php (login, add post, Show visitors log, Go to the blog and (logout))

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

	<title>Blogg - Login</title>

	<link rel="stylesheet" type="text/css" href="stilmall.css" />
</head>

<body>		

	<h1>Blogglogin</h1>

	<form action="blogglogin.php" method="post">

		<p>			

			Username (admin):<br />
			<input type="text" name="username" size="20" /><br /><br />

			Password (abc123):<br />
			<input type="password" name="password" size="20" /><br /><br />
              				
			<input type="submit" name="addpost" value="add post" />

			<input type="submit" name="showstat" value="Show visitors log" />

			<input type="submit" name="blogg" value="Go to the blog (logout)" />				
		</p>
	</form>
        
        
</body>
</html> 

 

 

bloggadd.php (Blog - Add post)

 

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

	<title>Blog - Add post</title>

	<link rel="stylesheet" type="text/css" href="stilmall.css" />

	<script src="scripts/bloggadd.js" type="text/JavaScript"></script>		
</head>

<body onload="showdate()">

	<h1>Bloggadd</h1>					

	<form action="bloggadd.php" method="post">

		<p>
			Date (yyyy-mm-dd):<br />
			<input type="text" name="date" id="date" size="12" /><br /><br />

			Subject:<br />
			<input type="text" name="header" id="header" size="12" /><br /><br />				

			Content:<br />
			<textarea name="entry" id="entry" rows="20" cols="40"></textarea><br /><br />	

			<input type="submit" name="add" value="add post" onclick="return validate()"/>

			<input type="submit" name="back" value="back" />								
		</p>
	</form>		
        

</body>
</html> 

 

 

 

bloggadd.js  (bloggadd.php)

 

 

// Show current date in the date box
function showdate()
{
var now = new Date();

document.getElementById('date').value = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
loginform.header.focus();
}

// Validate Form
function validate()
{	
var date = document.getElementById('date').value;
// Heading is no requirement
var entry = document.getElementById('entry').value;	

// If the date is not in correct format
if (!/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(document.getElementById('date').value)) 
{    
  		alert('Enter the date in yyyy-mm-dd.');  	
  		document.getElementById('date').focus();
  		
  		return false;
}	

// if posts missing
if (entry.length == 0)
{
	alert("Fill in a post.");
	document.getElementById('entry').focus();

	return false;
}

return true;				
}

 

 

blogg.php  (Blogg - Show post)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

	<title>Blogg - Show post</title>

	<link rel="stylesheet" type="text/css" href="stilmall.css" />		
</head>

<body>

	<h1>Blogg</h1>

	<form action="blogg.php" method="post">
		<p>
			<input type="submit" name="this" value="Current Month" />
			<input type="submit" name="prev" value="Previous Month" />
			<input type="submit" name="next" value="Next Month" />
		</p>
	</form>

	<p>Year-Month: <br /><br />post(s):<br /><br />--------------------------------------------------<br />Date: 
<br />Subject:

<br /><br />--------------------------------------------------<br /></p>
</body>
</html> 

 

 

I know I've asked a lot before but you have to ask until you learn of the best.

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.