Forum Moderators: coopster
the first thing to do would be to make the form and figure out all the properties of the new page you want to control.
Once you have all the input then you need to process the form submission. You can look at this thread
[webmasterworld.com...]
which covers some of the basics. You can also look up "php form handling".
after getting the data from the form, error checking it and putting it into variables you can the build your html page into a variable the create the physical file and save your data in that file.
All fairly straight forward actually but will take a little time.
make the form and have the script in the form action just show the data entered like so
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
once you have the form all setup then you can start error checking the various parts to make sure the required ones are there and nothing funky has crept through
then you can start putting the entered data together into a variable and the probably outputting that variable to see what you've created.
Once what is stored in that variable looks like a real file then you can figure out how to create files using fopen and fwrite.
You could also just use an existing cms or find a cms written in php and look at how it does it. :D
<html>
<body bgcolor=FFFFFF><a href="http://www.MYSITE.com/home.php">HOME</a> - <a href="http://www.MYSITE.com/news.php">NEWS</a> - <a href="http://www.MYSITE.com/coollinks.php">COOL LINKS</a> -
</br>
This is the homepage.
</body>
</html>
I want to have a page, where I can edit what it says in home.php in the part where it says "This is the homepage". The same thing goes for news.php and coollinks.php.
But I'd also want to be able to add a new page, but I'll do that later when I'm able to do this.
Thanks a lot for your help. (:
The three immediately apparent approaches are:
- Store data in a database, and the pages are not really pages. With a combination of mod_rewrite and a display script, requests for "pagename.html" are rewritten to a display script, which parses out the requested file name, associates it with the proper database entry, extracts the data from the DB, and inserts it into a site template. The address bar remains "pagename.html" but the output comes from the database, dynamically. This is by far the most efficient, maintainable, and portable approach.
- Same as above, but actually write out the pages. Every time you add or update a page, it stores the info in the database, but ends by actually writing out the file "pagename.html"
- An old school, no database approach is to just write the pages on submit. You enter page content in a form, then open a template, like
<html>
<body>
<a href="http://www.MYSITE.com/home.php">HOME</a> - <a href="http://www.MYSITE.com/news.php">NEWS</a> - <a href="http://www.MYSITE.com/coollinks.php">COOL LINKS</a>
[CONTENT]
</body>
</html>
And where you see [CONTENT] you replace that line with what you submit. You'd then write the whole thing to disk:
- submit data, cleanse it, store it in a variable (call it $content or something)
- open template, store in a variable, call it $final
do this: $final = preg_replace('/\[CONTENT\]/',$content,$final);
- write $final to the file name you submitted in the form.
Obviously there's lots of stuff you need to deal with in any of the three above - editing, deleting, how you manage adding the pages to some navigation structure, what to do about images and other embedded objects, what to do about existing file names, directory structure . . . lots going on in even a mini-CMS.
You should start reading the basics of php/mysql based websites, we have this thread
[webmasterworld.com...]
What you would do is have 3 columns in the mysql database
page_id
page_title
page_content
page_id is auto incrementing
Then you can have your index page set up like this:
<?php
mysql_connect('localhost','username','password') or die('Could not connect to server');
mysql_select_db('my_database');
$page_id = $_GET['pageid']
if(isset($page_id) && $page_id != "") {
$sql = 'SELECT FROM site_pages WHERE page_id="' . $page_id . '"';
$result = mysql_query($sql);
if(mysql_fetch_array($result)) {
$page_title = mysql_result($result,0,"page_title");
$page_content = mysql_result($result,0,"page_content");/* use a full doctype, declare charsheet etc - this is just for example */
echo '<html><head><title>' . $page_title . ' | Site Name</title></head>
<body>
<p><b>' . $page_title . '</b></p>
<p>' . $page_content . '</p>
</body></html>;
} else {
echo 'Home page stuff';
}
} else {
echo 'Home page stuff';
}
?>
If you're wondering why the home page stuff is listed twice, it's because the first instance of it being listed is if an invalid page id is set, and the second instance is if no page id is set.
Because of this, it's probably better to write your homepage to a variable above this so you don't have contrast between the two.
And remember, you can write HTML inside a database, and when echoing it in the format above it will register as HTML and display properly. If you don't want to do that, then what you could do is use the function that rocknbil posted in this thread:
[webmasterworld.com...]
(speaking of btw rock, I did manage to modify your function to work for single lines where needed :) bit of a crash course in regex for me)
You can also generate your navigation menu in this way:
$nav_sql = 'SELECT page_id, page_title FROM site_pages';
$nav_result = mysql_query($nav_sql);
$nav_rows = mysql_num_rows($nav_sql);
$navigation = '<p><b>Navigation</b></p>
<p><a href="/index.php">Home</a>';for($i = 0; $i < $nav_rows; $i++) {
$page_id = mysql_result($nav_result,$i,"page_id");
$page_title = mysql_result($nav_result,$i,"page_title");
$navigation .= '<br /><a href="/index.php?pageid=' . $page_id . '">' . $page_title . '</a>';
}
$navigation .= '</p>';
echo $navigation;
Declaring a variable as $var .= 'stuff'; is a method of appending more data onto an existing variable.
So,
$stuff = 'a';
$stuff .= 'b';
echo $stuff; will output:
ab
1. What does (isset($page_id) mean? And what does it do?
2. $result = mysql_query($sql); <<< Does that line update the database table?
3. if(mysql_fetch_array($result)) <<< What does that line mean?
Thanks a billion for your help! I really appreciate it! (:
(isset($page_id) -----
$result = mysql_query($sql); -----
(mysql_fetch_array($result)) Using it in an if statement in the fashion that I did, however, is simply used to make sure there is something in the database to be retrieved.
-----
Further reading:
isset() - [uk.php.net...]
mysql_query() - [uk.php.net...]
mysql_fetch_array() - [php.net...]
You can find out the answers to a lot of these questions at php.net :)