Jump to content

How to create a monthly archive (no MySQL och any databases)?


newbe123

Recommended Posts

If everything is saved on file as in, all blog posts are kept in html files? A little more information would be helpful, or perhaps a link.

 

If you mean just backing up, then it's as simple as saving all html files into a folder each month and done. However if you're looking to archive as in, letting users have access to all of your blog posts at all times, then instead of removing a post to add a new one (assuming), just rename your old index to index2 and link to it.

Link to comment
Share on other sites

I am saving my posts on file and I want to create the monthly archive for my posts. This is what I've done so far.

 

index.php (where I want my archive to be)

 

 

<?php
session_start();

if(isset($_POST['LoutBtn']))
{
session_unset($_SESSION);
session_destroy();
}

if(isset($_POST['LoginBtn']))
{


//convert a string to all lower case letters.
//if user gives username with big letters still can login.
$user = strtolower($_POST['username']);
$pass = $_POST['password'];

if($user == 'admin' && $pass == '123')
{
$_SESSION['LogedIn'] = true;
}

elseif (empty($user) || empty($pass))
         
      
      {
	  print('<font color="#FF0000">Please fill in username and password!<br/></font>');
    
      
      }
	  
elseif ($_POST['username'] != 'admin'){
      
     
      print('<font color="#FF0000">wrong username<br/></font>');
    
      
      }
      
      elseif ($_POST['password'] != '123'){
      
   
      print('<font color="#FF0000">wrong password<br/></font>');

}
}
if (isset($_SESSION['LogedIn']))
if ($_SESSION['LogedIn'] == true)
{
echo 'Welcome You are Loged in.';
?>
    <table width="50"  align="right"  cellpadding="2" cellspacing="2">

<form method="POST" action="panel.php">
<tr>

<td><input type="submit" value="add post" name="PnlBtn" /></td>
</tr>
</form>

<form method="POST" action="stat.php">
<tr>

<td><input type="submit" name="showstat" value="visitorlog" /></td>
</tr>
</form>

</table>  
<?php	

}

?>


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

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


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

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

<table width="300" border="1" align="right" cellpadding="2" cellspacing="2">
<tr>
<td width="150">UserName:</td>
<td> <input type="text" name="username" size="20" />
</td>
</tr>
<tr>
<td width="150">Password</td>
<td><input type="password" name="password" size="20" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" name="LoginBtn" />
</td>
</tr>
<tr>
<td><input type="submit" value="Logout" name="LoutBtn" /></td>
</tr>
</table>
</form>




<!-- 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= $count -1;

while($i>=0) {	

$new_array = explode("|", $BlogArray[$i]);

echo "Posted by: " . $new_array[1] . "<br>";

echo "Posted on: " .  date("l m/d/y h:i:s", $new_array[0]) . "<br>";

echo "Title: " . $new_array[2] . "<br>";


echo $new_array[3] . "<hr>";


$i --;

}
}
?>




</div>


</body>
</html>

 

 

 

panel.php

 

<?php
session_start();
if   ($_SESSION['LogedIn'] == false)
{
$redirect = "Location: " . $_REQUEST['LoginBtn'] . "index.php";
echo header($redirect);

}

elseif ($_SESSION['LogedIn'] == true)
{
echo "welcome";
}

?>





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>



<form  action="content.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>




</body>
</html>
<?php
$page_parts = explode("/", $_SERVER['PATH_INFO']);

$page_filename = $page_parts[count($page_parts)-1];



$lastpage = $_SESSION['index.php'];

if ( strlen ($lastpage) == 0 ) $lastpage = "index.php";



$_SESSION['index.php'] = $page_filename;

?>
<form  action="<?php echo $lastpage ?>" method="post">
<table>
<tr><td clospan="2"><input type="submit" value="Tillbaka" name="back" /></td></tr>
</table>
</form>

 

 

content.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); 

header("Location: panel.php");
//exit; // Closes further script execution . 
?>

Link to comment
Share on other sites

on the first day of each month:

 

open your blog file

loop thru the array

check if the month of the date posted is NOT the current month

    IF it is NOT then add that element to new array 1

    IF it IS then add that element to new array 2

when all are looped close the blog file

save new array 2 as the blog file

save new array 1 as the archive file

 

Link to comment
Share on other sites

I guarantee this code doesn't work, completely untested, however this is pretty much doing everything for you besides actually coding the whole thing for you, litebearer gave you a pretty good idea with pseudocodeish, and I followed it:

<?php

// Open blog file
$fh = fopen("blogfile.txt", "a+");

// Seperates the blog file into each individual line...
$eachLine = explode("\n", $fh);

// Declaring these arrays here isn't necessary
// just doing for clarification
$blogArray = array();
$archiveArray = array();

// What our current month is (in 1-12 format)
$currentMonth = date("n", time());

// Loop through each line of the blog contents
foreach ($eachLine as $indLine)
{
	// Find the pieces of each line
	$lineParts = explode("|", $indLine);
	$when = $indLine[0];
	$who = $indLine[1];
	$title = $indLine[2];
	$content = $indLine[3];

	// If the current post is in the current month
	// add to the blog array, not archive array
	if (date("n", $when)==$currentMonth)
		{
			$blogArray[] = $indLine;
		} else {
			$archiveArray[] = $indLine;
		}
}

// Close out the file, etc...
fclose($fh);

?>

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.