|
-
May 9th, 2009, 11:42 PM
#1
Thread Starter
Frenzied Member
Writing a CMS
To create a CMS in PHP, is the best way to do it to save sections of the html to either say either a db or text file. This seems a bit messy but maybe the best way, for example say i have this html and i want to have a user be able to add more images to their page by just uploading an image and clicking a button ........,
HTML Code:
HTML Code:
<table>
<tr>
<td> <img src = "i1.jpg" ></td>
</tr>
</table>
Now i could save,
HTML Code:
to say text1.txt
and
HTML Code:
HTML Code:
</tr>
</table>
to text2.txt,
and the images to say text3.txt,
HTML Code:
HTML Code:
<td> <img src = "i1.jpg" ></td>
i could then say something like,
PHP Code:
<?php
$fname = "text1.txt";
$fo = fopen($fname,r);
$fr1 = fread($fo,filesize($fname));
fclose($fo);
$fname = "text2.txt";
$fo = fopen($fname,r);
$fr2 = fread($fo,filesize($fname));
fclose($fo);
$fname = "text3.txt";
$fo = fopen($fname,r);
$fr3 = fread($fo,filesize($fname));
fclose($fo);
$f_final= $fr1.$fr3.$fr2;
echo $f_final;
?>
This would put the contents of all 3 text files into a single variable $f_final so that all i would need to do if i needed a new image added is add that images html to text3.txt.
This seems like a pretty good way of doing things, i assume this is how CMS systems are made pretty much??
So basically i could have a user frontend "addimg.html" which is for adding the image,
HTML Code:
<form method="post" action ="newimage.php">
<input tpye = "text" name = "img1">
<input type = "submit">
</form>
and then a modified version of the above php,
PHP Code:
<?php
//save new image name to text3.txt ***********
$newimgge_to_ADD = $_POST["img1"];
$fname = "text3.txt";
$fo = fopen($fname,a);
fwrite($fo,"\r\n".'<td> <img src = "'.$newimgge_to_ADD.'.jpg"> </td>');
fclose($fo);
//**********************
//Open all the text files one by one and save their contents to a variable *****
$fname = "text1.txt";
$fo = fopen($fname,r);
$fr1 = fread($fo,filesize($fname));
fclose($fo);
$fname = "text2.txt";
$fo = fopen($fname,r);
$fr2 = fread($fo,filesize($fname));
fclose($fo);
$fname = "text3.txt";
$fo = fopen($fname,r);
$fr3 = fread($fo,filesize($fname));
fclose($fo);
//******************
//Combine the contents of all 3 varables to one varable f_final **********
$f_final= $fr1.$fr3.$fr2;
//*********************
//Print out the result *********************
echo $f_final;
//**************************
?>
This would need a good bit of tweaking but i just want to know if i am on the right tracks..
-
May 10th, 2009, 12:24 AM
#2
Re: Writing a CMS
that's a terrible way to manage content. super duper simple way of doing things:
index.php:
PHP Code:
<?php //whatever page the user wants $page = (isset($_GET['page'])) ? $_GET['page'] : "main";
//header page include("header.php");
//include content if(file_exists($page . ".php")){ include($page . ".php"); }else{ //file doesn't exist, error page include("error.php"); }
//footer page include("footer.php"); ?>
header.php/footer.php/error.php can all contain actual PHP code and will be executed like a regular PHP script. then, if you requested index.php?page=main, "main.php" would be included along with the header/footer files. if you need to change images dynamically, use a database to store image URLs, rather than trying to store pieces of html in text files.
hope that makes sense and was what you might have been looking for.
-
May 10th, 2009, 01:52 AM
#3
Thread Starter
Frenzied Member
Re: Writing a CMS
I am ok with your header and footer parts as those parts are static code and never change but say i create a page for someone and they want to modify the page and the part they need to modify is burried deep in the pages html in some table or td or div or span etc.....how do i cleanly add to that section, all you said was,
PHP Code:
if(file_exists($page . ".php")){ include($page . ".php");
but the point is that somewhere in the $page....php there need to be changes made.
In the 'Welcome to Pastries' section "This is Pastries , a free, fully ...." of text i need to be able to let the user change that text using a type and click front end. I know you know what i am talking about but your answer didn't explain to me how i actually do this.
So the section i am talking about has html,
HTML Code:
<div class="post">
<h1 class="title"><a href="#">Welcome to Pastries </a></h1>
<div class="entry">
<p>This is <strong>Pastries </strong>, a free, full <---- this text
I need the best way to "get in there" and append, or change, the text...
Thanks again for your answers.
Last edited by Jmacp; May 28th, 2009 at 05:23 AM.
-
May 10th, 2009, 02:22 AM
#4
Re: Writing a CMS
what I said in my earlier pos can also apply to any text at all:
if you need to change images dynamically, use a database to store image URLs, rather than trying to store pieces of html in text files.
whatever page you're including just needs to get all of the information that can be edited from the database.
have you ever worked with MySQL?
-
May 10th, 2009, 12:10 PM
#5
Re: Writing a CMS
 Originally Posted by kows
that's a terrible way to manage content. super duper simple way of doing things:
index.php:
PHP Code:
<?php //whatever page the user wants $page = (isset($_GET['page'])) ? $_GET['page'] : "main";
//header page include("header.php");
//include content if(file_exists($page . ".php")){ include($page . ".php"); }else{ //file doesn't exist, error page include("error.php"); }
//footer page include("footer.php"); ?>
header.php/footer.php/error.php can all contain actual PHP code and will be executed like a regular PHP script. then, if you requested index.php?page=main, "main.php" would be included along with the header/footer files. if you need to change images dynamically, use a database to store image URLs, rather than trying to store pieces of html in text files.
hope that makes sense and was what you might have been looking for.
Watch out for file inclusion vulnerabilities with that code.
The only proper way to mange content is to use a template system. You can write your own simple one very easily (I demonstrated with some code here) or use a whole pre built templating language such as Smarty.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|