Forum Moderators: coopster

Message Too Old, No Replies

page creation

how do i create a page on a page

         

wincode

3:14 am on Jan 28, 2010 (gmt 0)

10+ Year Member



Hello. I want to be able to create new pages from another page on my site, like admin.php
On that page, I'd have a form for the page's properties, like file name. Could anyone assist me in doing this?
I'm really new to php and want to get better.

jatar_k

3:25 pm on Jan 28, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld wincode,

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

wincode

9:39 pm on Jan 28, 2010 (gmt 0)

10+ Year Member



Hey jatar! Thank you so much for your help. I'm very confused though ):
Okay, here I'll explain my small goal.
I'm trying to make a site so that I don't have to edit it by uploading files to ftp to make small changes like adding news or links. I'd also like to be able to create a new page.
Here below is what it would look like.

<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. (:

rocknbil

11:17 pm on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's more than just "a page." You are talking about a mini-CMS.

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.

wincode

11:40 pm on Jan 28, 2010 (gmt 0)

10+ Year Member



Okay, thank you (:
I'd like to try the database, but have no idea how to start.

jatar_k

2:15 pm on Jan 29, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you might be better looking at a cms already written in php to install, use and possibly update.

You should start reading the basics of php/mysql based websites, we have this thread

[webmasterworld.com...]

wincode

2:40 am on Jan 30, 2010 (gmt 0)

10+ Year Member



I think I have an idea of how it would work..
What I would have is connect to the database and set up a 5 variables (each for a page paragraph). Then on the page where I edit my pages, I would have the editors. And what the editors would do is display what each variable has. Say one of the vairables is $page1, then I can view what's in it and change it/add to it.
Then on www.mysite.com/page1.php , I'd have an include statement to include the variable $page1.
But my problem is I don't know how to do it...

Readie

3:10 am on Jan 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not quite.

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

wincode

12:09 am on Jan 31, 2010 (gmt 0)

10+ Year Member



Ohh thanks so much! (:
Can I ask some questions to better understand the script please?

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! (:

Readie

1:09 am on Jan 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(isset($page_id)

IS SET - so if $page_id has been set it returns true

-----

$result = mysql_query($sql);

It could be, depending on the value applied to $sql, in the location you copied that from though, it is actually retrieving from the database

-----

(mysql_fetch_array($result))

Returns an array based on the SQL command after having been run through a mysql_query function.

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 :)