Jump to content
xisto Community
Sign in to follow this  
Tsunami

Flat-file Cms tutorial inspired by jlhaslip

Recommended Posts

Ok, for this tutorial i am only going to show you how to add updates to your site simply by storing the information into a text file, and then displaying it with predefined formatting... OK lets get down to business...

 

Lets start out by making a PHP file and call it mycms.php

 

put this code at the top of the page. What this will do is allow us to edit the selected update when it comes time and show and hide the add an update field and validate the form..


if (id2 != '') expMenu(id2);

}

function expMenu(id) {

var itm = null;

if (document.getElementById) {

itm = document.getElementById(id);

} else if (document.all){

itm = document.all[id];

} else if (document.layers){

itm = document.layers[id];

}

 

if (!itm) {

// do nothing

}

else if (itm.style) {

if (itm.style.display == "none") { itm.style.display = ""; }

else { itm.style.display = "none"; }

}

else { itm.visibility = "show"; }

}

function ValidateForm(isMsg) {

MessageLength = document.REPLIER.Post.value.length;

errors = "";

 

if (isMsg == 1)

{

if (document.REPLIER.msg_title.value.length < 2)

{

errors = "You must enter a message title";

}

}

 

if (MessageLength < 2) {

errors = "You must enter a message to post!";

}

if (MessageMax !=0) {

if (MessageLength > MessageMax) {

errors = "The maximum allowed length is " + MessageMax + " characters. Current Characters linenums:0'><html><head><script language="javascript"> function ShowHide(id1, id2) { if (id1 != '') expMenu(id1); if (id2 != '') expMenu(id2); }function expMenu(id) { var itm = null; if (document.getElementById) { itm = document.getElementById(id); } else if (document.all){ itm = document.all[id]; } else if (document.layers){ itm = document.layers[id]; } if (!itm) { // do nothing } else if (itm.style) { if (itm.style.display == "none") { itm.style.display = ""; } else { itm.style.display = "none"; } } else { itm.visibility = "show"; } }function ValidateForm(isMsg) { MessageLength = document.REPLIER.Post.value.length; errors = ""; if (isMsg == 1) { if (document.REPLIER.msg_title.value.length < 2) { errors = "You must enter a message title"; } } if (MessageLength < 2) { errors = "You must enter a message to post!"; } if (MessageMax !=0) { if (MessageLength > MessageMax) { errors = "The maximum allowed length is " + MessageMax + " characters. Current Characters: " + MessageLength; } } if (errors != "" && Override == "") { alert(errors); return false; } else { document.REPLIER.submit.disabled = true; return true; } }function goupdates(){ uid = updateinfo.update.value; theurl = "mycms.php?action=edit&uid=" + uid; location.href = theurl;}</script></head>


now... we will have a variable called "action" to tell the page exactly what to do. this will decide what to show, like if action is set to display it will display the updates or if it is set to edit then it will edit them... ect ect... right now we want to check and see if it is set to edit so that we can edit the updates.

 

<?if(strtolower($_GET['action']) == "edit"){

ok... now we have to access our updates.txt file that we will create later on. If you have read my tutorial of flatfile membership systems you might know that i like to use the file() method and i also like to use is the explode() function... so lets see the code for this

 

$updates = file('updates.txt');foreach($updates as $Key=> $Val){    $update[$Key] = explode("|##|", $Val);}?>

Pretty simple, reads the file to an array then breaks it up into smaller arrays based on the location of |##| seperators.

 

and now comes the fun part... making the gui... im not going to go into to much detail on explaining this because all this is is nothing more than a simple form which the information is filled out using php... so here we go:


 

}

?>

 

</SELECT></td><td>Title linenums:0'><center><table align="center"><tr><td rowspan="3"> <form name="updateinfo"><SELECT NAME=update SIZE=6 width=120 onChange="goupdates();"><?for($K = sizeof($updates) - 1; $K > 0 ; $K--) { echo "<option value='" . $K . "'>" . $update[$K][2];}?></SELECT></td><td>Title:</td><td><input type="text" name="title" width="100" value="<? if($_GET['uid']) echo $update[$_GET['uid']][2]; ?>"></td></tr><tr><td>Author:</td><td><input type="text" name="author" readonly='1' width="100" value="<? if($_GET['uid']) echo $update[$_GET['uid']][0]; ?>"></td></tr> <tr><td>Date:</td><td><input type="text" name="date" width="100" value="<? if($_GET['uid']) echo $update[$_GET['uid']][1]; ?>"></td></tr><tr><td colspan="3">Body:</td></tr><tr><td colspan="3"><textarea cols="40" rows="10" name="body" id="wysiwyg"><? if($_GET['uid']){ echo $update[$_GET['uid']][3];} ?></textarea> </form><form action="mycms.php?action=delete&uid=<? echo $_GET['uid']; ?>" method="POST"> </td></tr><tr><td colspan="3"><table width="100" align="center"><tr><td><input type="submit" value="Delete" width=100"></form></td><td><td><td><a href="java script:ShowHide('new_open','new_closed') "> <input type="button" value="Add" width="100" onClick="java script:ShowHide('topic_open','topic_closed')"> </a></td></tr></table></td></tr><tr><td colspan="3"><center><input type="button" value="Delete All" width="300"></center></td></tr></table><br><br><div id='topic_open' style='display:none;z-index:2;'> <div border-style='dashed'><form action="mycms.php?action=add" method="POST" name="REPLIER" onSubmit="return ValidateForm();" ><table width="500" border="0"><tr><td width="100"><font color="#000"><b>Post Title:</b></font></td><td width="400"><input type="text" size="40" maxlength="50" name="title" value="" tabindex="1" class="forminput" /></td></tr><tr><td width="100"><font color="#000000"><b>Author:</b></font></td><td width="400"><input type="text" size="40" maxlength="50" name="user" value="" tabindex="1" class="forminput" /></td></tr><tr><td colspan="2"><center> <textarea id="postbody" cols="80" rows="20" name="Post" tabindex="3" class="textinput"></textarea></center></td></tr><tr><td colspan="2"> <?print '<input type="hidden" name="name" value="' . $name .'" readonly>';?><center> <input type="submit" name="submit" value="Post Update" tabindex="4" class="forminput"/></center> </td></tr></table></form> </div> </div></center><?}


And thats it for the gui... now lets go on to the Add page just paste this after that code above and here it is... im not gonna go to much into this either because its basically the same thing as with the membership tutorial only applied a lil differently...

 


{

$title = $_POST["title"];

$content = $_POST["Post"];

$user = $_POST["user"];

 

 

 

$openname = fopen("updates.txt", "a");

 

$month = date("m");

$day = date("d");

$year = date("y");

$date = $month . '/' . $day . '/' . $year;

fwrite($openname, $user . "|##|" . $date . "|##|" . $title . "|##|" . $content . "\r\n");

fclose($openname);

 

?>

 

<html><head><title>Please stand by...</title><meta http-equiv="refresh" content="2; url=mycms.php?action=edit" />

 

</head>

<body>

<table width="100%" height="85%" align="center">

<tr>

<td valign="middle">

<table align="center" cellpadding="4" class="tablefill">

<tr>

<td width="100%" align="center">

Thank you... your update has been added <br>

(<a href="mycsm.php?action=edit">Or click here if you do not wish to wait</a>)

</td>

</tr>

</table>

</td>

</tr>

</table>

</body>

</html>

<?

}

linenums:0'>if(strtolower($_GET['action']) == "add"){$title = $_POST["title"];$content = $_POST["Post"];$user = $_POST["user"];$openname = fopen("updates.txt", "a");$month = date("m");$day = date("d");$year = date("y");$date = $month . '/' . $day . '/' . $year;fwrite($openname, $user . "|##|" . $date . "|##|" . $title . "|##|" . $content . "\r\n");fclose($openname);?><html><head><title>Please stand by...</title><meta http-equiv="refresh" content="2; url=mycms.php?action=edit" /></head><body><table width="100%" height="85%" align="center"><tr> <td valign="middle"> <table align="center" cellpadding="4" class="tablefill"> <tr> <td width="100%" align="center"> Thank you... your update has been added <br> (<a href="mycsm.php?action=edit">Or click here if you do not wish to wait</a>) </td> </tr> </table> </td></tr></table> </body></html><?}


ok... now were almost done... just a couple more things to add and then where done...

this is the delete page... just add at the bottom... still fairly simple...

 


{

$fileh = file('updates.txt');

$file2 = fopen('updates.txt','w');

for($K = 0; $K < sizeof($fileh); $K++)

{

if ($K != $_GET['uid'])

{

fwrite($file2,$fileh[$K]);

}

}

fclose($file2);

 

?>

<html><head>

<meta http-equiv='refresh' content='2; url=mycms.php?action=edit'/>

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

 

</head>

<body>

<table width='100%' height='85%' align='center'>

<tr>

<td valign='middle'>

<table align='center' cellpadding="4" class="outside" border='1'>

<tr>

<td width="100%" align="center">

 

Please wait as we delete the update<br /><br />

(<a href='mycms.php?action=edit'>Or click here if you do not wish to wait</a>

</td>

</tr>

</table>

</td>

</tr>

</table>

</body>

</html>

<?

}

linenums:0'>if(strtolower($_GET['action']) == "delete"){$fileh = file('updates.txt');$file2 = fopen('updates.txt','w');for($K = 0; $K < sizeof($fileh); $K++){if ($K != $_GET['uid']){fwrite($file2,$fileh[$K]);}}fclose($file2);?><html><head><meta http-equiv='refresh' content='2; url=mycms.php?action=edit'/><script type="text/javascript"> </script></head><body><table width='100%' height='85%' align='center'><tr> <td valign='middle'> <table align='center' cellpadding="4" class="outside" border='1'> <tr> <td width="100%" align="center"> Please wait as we delete the update<br /><br /> (<a href='mycms.php?action=edit'>Or click here if you do not wish to wait</a> </td> </tr> </table> </td></tr></table> </body></html><?}


ok now all we need to do is to display it... so go ahead and add this bit of code

 


{

$updates = file('updates.txt');

foreach($updates as $Key=> $Val)

{

$update[$Key] = explode("|##|", $Val);

}

 

$find1[] = "|#title#|";

$find1[] = "|#author#|";

$find1[] = "|#date#|";

$find1[] = "|#body#|";

 

for($k = sizeof($updates) - 1; $k>0;$k--)

{

$replace1[0] = $update[$k][2];

$replace1[1] = $update[$k][0];

$replace1[2] = $update[$k][1];

$replace1[3] = $update[$k][3];

$content1 = str_replace($find1, $replace1, $updates[0]);

echo $content1 . "<br>";

}

}

?>

linenums:0'>if(!$_GET['action']){$updates = file('updates.txt');foreach($updates as $Key=> $Val){ $update[$Key] = explode("|##|", $Val);}$find1[] = "|#title#|";$find1[] = "|#author#|";$find1[] = "|#date#|";$find1[] = "|#body#|";for($k = sizeof($updates) - 1; $k>0;$k--){$replace1[0] = $update[$k][2];$replace1[1] = $update[$k][0];$replace1[2] = $update[$k][1];$replace1[3] = $update[$k][3];$content1 = str_replace($find1, $replace1, $updates[0]);echo $content1 . "<br>";}}?>


then save that and now we want to make a text file called updates.txt and chmod it to 777.

now we want to open the file and make the first line equal to the coding for the display placing tags that corrispond to to the data we want to enter... use |#title#| for the title, |#author#| for the autor, and |#date#| for the date and |#body#| for the body

 

make sure it all is on the one line... its easy... just code it, and then take out all the new lines so that it all is squished on to the same one.. :) and it will work

 

to put ur updates anywhere just use

include('mycms.php');

and bam! your done...

 

 

previews:

View Updates

Edit Updates

Full Source

Share this post


Link to post
Share on other sites

Hey, nice script you have here. I can see this going places. Will there be a part 2 of this, with more advanced features? I'm sure this will be very useful. Keep up the good work. :)Btw, i noticed the date isn't really used when viewing the updates: why is that?

Share this post


Link to post
Share on other sites

of course there is going to be a part 2... with editing and delete all functions and such... also i hope to make a wysiwyg editor for editing the main body with buttons to insert different information. like the post date the post author, viewers ip address and other things that you can do with php what u mean the data isnt really used?

Share this post


Link to post
Share on other sites

what u mean the data isnt really used?

When viewing the updates, there's no title attribute, or whatever, that shows when [such-and-such] was posted. Usually, in the case of updates, you inform the readers when [such-and-such] was posted. I know the code can be easily modifed to have this implemented, but just wondering. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.