bruckerrlb Posted August 9, 2007 Share Posted August 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/ Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319826 Share on other sites More sharing options...
SirChick Posted August 10, 2007 Share Posted August 10, 2007 well the "post"(what the user typed) could be echo'd inside a html template already set up on a page.. if the page is already there i guess.. but if you creating only when the user does such a thing .. then there must be a script to create a new page.. Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319830 Share on other sites More sharing options...
trq Posted August 10, 2007 Share Posted August 10, 2007 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...   Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319833 Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319837 Share on other sites More sharing options...
bruckerrlb Posted August 10, 2007 Author Share Posted August 10, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319838 Share on other sites More sharing options...
bruckerrlb Posted August 10, 2007 Author Share Posted August 10, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319846 Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 Echoing php inside a php string?! I guess it should work as long as u use quotes and double quotes wisely. Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319851 Share on other sites More sharing options...
bruckerrlb Posted August 10, 2007 Author Share Posted August 10, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319854 Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319856 Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319859 Share on other sites More sharing options...
trq Posted August 10, 2007 Share Posted August 10, 2007 Why are you so intent on actually creating new files? Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319865 Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319869 Share on other sites More sharing options...
bruckerrlb Posted August 10, 2007 Author Share Posted August 10, 2007 Thank you very much! i'm testing out this function now, and to answer the question about why I want to do this, I just want to learn, well I need to learn, in case I have a client who wants to be able to post new pages, and well I just like to practice and learn Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319883 Share on other sites More sharing options...
trq Posted August 10, 2007 Share Posted August 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319897 Share on other sites More sharing options...
bruckerrlb Posted August 10, 2007 Author Share Posted August 10, 2007 Thanks for the tip, I am a huge fan of already built cm systems, joomla, love it, and wordpress, also a huge fan, I was just wondering how this works, but now that 'm working on it, it seems to be a little out of my league for now Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319920 Share on other sites More sharing options...
trq Posted August 10, 2007 Share Posted August 10, 2007 Nah... a cms can be as simple or as complex as need be. Just start simple and add features as you need them. The concept really isn't too difficult. Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319923 Share on other sites More sharing options...
Fadion Posted August 10, 2007 Share Posted August 10, 2007 Yep u can create a nice and simple cms which gets the work done and after some experience u can add stuff. Joomla is the work of an entire community, so dont think of smth such complex for now. Quote Link to comment https://forums.phpfreaks.com/topic/64173-dynamically-create-new-web-page/#findComment-319926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.