Jump to content

dynamically create new web page


bruckerrlb

Recommended Posts

Hello!

I am writing to get advice, i'm making a cms, and I have a good portion done, I have it set up where the admin can log in and create a new post which goes to the database. I am just wondering how I can make this post a new web page? I have been trying to study how they do it in wordpress but with no avail. Anyone have any suggestions? Thanks

Link to comment
Share on other sites

If i was used to wordpress im sure i could give a better answer. Anyway u mean that when the admin adds data to the database, he also dynamically creates a page like 'newpage.php' which contains the added data? If so u can consider fopen(), which not only opens files but if it doesnt find one, creates it. Then use fwrite() to add the info. Thats what i can think about.

Link to comment
Share on other sites

You setup a single page that queries different data from your database based on the url you pass it. eg;

 

<?php

 // connect to db.
 if (isset($_GET['id'])) {
   $id = mysql_real_escape_string($_GET['id']);
   // query based on id from url.
   $sql = "SELECT data FROM tbl WHERE id = '$id' LIMIT 1";
 } else {
   // some default query.
   $sql = "SELECT data FROM tbl ORDER BY id DESC LIMIT 1";
 }
 if ($result = mysql_query($sql)) {
   if (mysql_num_rows($result)) {
     $row = mysql_fetch_assoc($result);
     echo $row['data']
   } else {
     echo "No record found matching $id";
   }
 }

?>

 

You then simply call page.php?id=12 etc etc...    

Link to comment
Share on other sites

The solution of thorpe may be the simple and more efficient way. But if u really need to create a new page u could use smth like this:

 

<?php
$file = fopen('file.php', 'w+');
$str = 
"<html>
<head>
<title>My new page</title>
</head>
<body>
This is where i put data from database
</body>
</html>
";
fwrite($file, $str);
fclose($file);
?>

 

It creates 'file.php', inserts some basic html tags and prints a text. Just my 2 cents.

Link to comment
Share on other sites

Thanks for all of your fast responses!!! I didn't consider the fwrite, thanks for sending me the code to query the user, I have something like that already to update and delete posts, but yours defanitly looks more structured. I will defanitly have to give this a try! Thank you!

Link to comment
Share on other sites

Just wondering but could I do something like this?

 

[color=red]<?php
$file = fopen('file.php', 'w+');
$str = 
"
?>[/color]
[color=green]<?php include('dbconnect.php'); ?>
<?php include('header.php'); ?>
<?php include('body.php'); ?>
<?php 
$selectit = "SELECT * FROM content WHERE id = ($_GET['id'])";
$content = $row["content"];
echo ('$content');
?>[/color]
[color=red]<?php
";
fwrite($file, $str);
fclose($file);
?>
[/color]

 

Right now this is a crazy idea in my head, I will have to try it

Link to comment
Share on other sites

I know I know, i'm new at this, so sometimes my ideas are a little what's the word, wrong, but this is what i've come up with

 

<?php

		include('dbconnect.php');
?>


<?php
if($_GET["cmd"]=="makenewpage")
		{

$id = $_GET['id'];
$sql = "SELECT FROM content WHERE id=$id";
$content = $row['content'];
$pagename = $row['pagename'];
$file = fopen("'$pagename'.php", "w+");
$str = 
"$content
";
fwrite($file, $str);
fclose($file);
?>


Link to comment
Share on other sites

Just made a test to include 2 files in the created file and it worked greatly. But as i said u need to use wisely quotes. Ex:

 

str=
"
<body>
include('header.inc.php');
$query = mysql_query(\"SELECT * FROM table WHERE user='$user'\");
</body>
"

 

I escaped double quotes so that php parses those as " and not as string limiters. Guess in this way u wont have problems, but still the idea to write php inside a string is pretty crazy lol.

Link to comment
Share on other sites

Your last code revised:

 

<?php
if($_GET["cmd"]=="makenewpage"){

$id = mysql_real_escape_string($_GET['id']);
$sql = @mysql_query("SELECT * FROM content WHERE id='$id'") or die(mysql_error());
$row = mysql_fetch_array($sql);
$content = $row['content'];
$pagename = $row['pagename'];
$file = fopen("$pagename.php", "w+");
$str = $content;
fwrite($file, $str);
fclose($file);
?>

Link to comment
Share on other sites

I made up a function for easy of use:

 

<?php
function createFile($file, $content, $title){
$handle = fopen($file, 'w+');
$html = "<html><head><title>$title</title></head><body>";
$html .= $content;
$html .= "</body></html>";
fwrite($handle, $html);
fclose($handle);
return true;
}

if(createFile('file.php', 'This is my new page', 'My Page')){
     echo "The file was successfully created";
}
?>

Link to comment
Share on other sites

in case I have a client who wants to be able to post new pages

 

You will often have clients that want to post new pages, rarely however have I ever actually created new physical pages. CMS's generally generate psuedo pages on the fly, its a much simpler approuch. Store your data in a database, and build the application to format and display it.

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.