wmguk Posted November 22, 2008 Share Posted November 22, 2008 Hi guys, I'm creating a CMS system and I need a one click page creator - What i wanted to do was click a button, and fill out a form with title, keywords etc etc, then it takes all the info I entered and inserts it into a template, and then saves and names the new page to the specified name... how can I set it so that inserts the proper code in to a templated page and creates a new .php page? Link to comment https://forums.phpfreaks.com/topic/133762-dynamic-page-creation-using-a-template/ Share on other sites More sharing options...
wmguk Posted November 22, 2008 Author Share Posted November 22, 2008 I've attached a demo creation page... once they click create they are directed to this page: <?php include ("../scripts/connection.php"); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db($db, $con); $name = $_POST['name']; $displayname = $_POST['displayname']; $team = $_POST['team']; $header = $_POST['header']; $footer = $_POST['footer']; $title = $_POST['title']; $description = $_POST['description']; $keywords = $_POST['keywords']; $content = $_POST['content']; mysql_query("INSERT INTO cmssystem (name, displayname, team, header, footer, title, description, keywords, content, active) VALUES ('$name', '$displayname', '$team', '$header', '$footer', '$description', '$keywords', '$content', 'no');"); } ?> <META HTTP-EQUIV="Refresh" CONTENT="0; URL=mainpage.php"> my template page is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo $title; ?></title> <meta name="keywords" content="<?php echo $keywords; ?>"> <meta name="description" content="<?php echo $description; ?> "> <link href="../----.css" rel="stylesheet" type="text/css" /> <!-- TemplateBeginEditable name="head" --> <!-- TemplateEndEditable --> <script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script> <link href="../SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" /> <link rel="shortcut icon" href="http://www.------.co.uk/favicon.ico"> <link rel="icon" href="http://www.-------.co.uk/favicon.ico" type="image/x-icon" /> </head> <body> <!-- TemplateBeginEditable name="bodycode" --><!-- TemplateEndEditable --> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td colspan="2" align="center"><img src="../images/demotop.gif" width="800" height="122" border="0"/></td></tr><tr><td colspan="2" align="right" bgcolor="#006699"> <ul id="MenuBar1" class="MenuBarHorizontal"> <li><a <?PHP if($active=='1'){ echo "class='activelnk'"; } else { } ?>href="../index.php">Home</a></li> <li><a <?PHP if($active=='2'){ echo "class='activelnk'"; } else { } ?>href="../vacancies.php">Vacancies</a></li> <li><a <?PHP if($active=='3'){ echo "class='activelnk'"; } else { } ?>href="../candidate.php">Candidate Area</a></li> <li><a <?PHP if($active=='4'){ echo "class='activelnk'"; } else { } ?>href="../client.php">Client Area</a></li> <li><a <?PHP if($active=='5'){ echo "class='activelnk'"; } else { } ?>href="../theteam.php" class="MenuBarItemSubmenu">The Team</a> <ul> <?php while($row = mysql_fetch_array($resultcms)) { ?> <li><a href="../team/<?php echo $row['name']; ?>.php"><?php echo $row['displayname']; ?></a></li> <?php } ?> </ul></li> <li><a <?PHP if($active=='6'){ echo "class='activelnk'"; } else { } ?>href="../contact.php">Contact Us</a> </li> </ul></td> </tr> <tr> <td colspan="2"><table width="800" border="0" cellspacing="5" cellpadding="0"> <tr> <td align="left" valign="top"> <!-- TemplateBeginEditable name="main" --> <? include 'admin/scripts/connection.php'; $sql="SELECT * FROM cmssystem WHERE name = 'candidatearea'"; $result=mysql_query($sql); while($row = mysql_fetch_array($result)) { echo $row['content']; } mysql_close($con); ?> <!-- TemplateEndEditable --></td> </tr> </table></td> </tr> <tr> <td colspan="2" bgcolor="#006699"> <table width="800" border="0" cellspacing="3" cellpadding="0"> <tr> <td align="left"><td> <td align="right" class="adminheader"></td> </tr> </table> </td> </tr> <tr> <td align="left" valign="top" class="general"> <?php while($row = mysql_fetch_array($resultextra)) { ?> <a href="../<?php echo $row['name']; ?>.php"><?php echo $row['displayname']; ?></a> | <?php } ?> </td> </tr> </table> </body> </html> [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/133762-dynamic-page-creation-using-a-template/#findComment-696109 Share on other sites More sharing options...
wmguk Posted November 23, 2008 Author Share Posted November 23, 2008 any thoughts of how to create this? Link to comment https://forums.phpfreaks.com/topic/133762-dynamic-page-creation-using-a-template/#findComment-696923 Share on other sites More sharing options...
wmguk Posted November 23, 2008 Author Share Posted November 23, 2008 I found this: $fp = fopen("test.php", "x+"); $str = "<B>This is the text for the php file created through php programming</B>"; fwrite($fp, $str); fclose($fp); and found that "x+" means create the file and open for reading and writing... (however this does not create that file!) Link to comment https://forums.phpfreaks.com/topic/133762-dynamic-page-creation-using-a-template/#findComment-696934 Share on other sites More sharing options...
elite_prodigy Posted November 23, 2008 Share Posted November 23, 2008 It looks like you're already on the right track mate. Just put that into a mysql database. Here is a structure that I would recomend for duynamic page creation: navigation ---id* ---display pages ---id* ---title ---navID# pgContent ---id* ---keywords ---content ---navID# * => primary key # => foreign reference Here is how I would do it. (Pseudo code) Create your navigation bar: Select everything from `navigation`. Fetch an array and output the list in a ?id=x format. Ex. $url = '<a href="http://www.site.com/index.php?id='.$id.'">'.$display.'</a>'; Get the page title: Use the get super global to get the value of id from the URL. Select `title` from `pages` where `navID` is equal to the id field you called from the URL. Echo this value between the title tags in the header. Get the body of the page: If you're in the same file that you used to call the tile of the page then you can re-use the I'd field here, or you can re-get it. Select `content` and `keywords` where `id` is equal to navID. Store this data. Echo this data to the area you wanted to place it in. You can add more fields as well, like allowing for customization of header and footer text. You could easily display this information in a multitude of formats and styles simply by including everything into a single header and including that into all the different styles you have formatted. Then you simply add a field to the page creation form which lists the various styles available. The user selects the style and you can use a switch structure to render that and even add that as a table or field in your DB which would make this happen everytime the page is rendered. I hope this helped, you could use one database table and simplify this process but then your database wouldn't be normalized to the third form as I believe I have done with my example database. I was not going to re-type the code you pretty much already have so you can use my pseudo code to modify your code. It shouldn't take more than a half hour. Link to comment https://forums.phpfreaks.com/topic/133762-dynamic-page-creation-using-a-template/#findComment-696944 Share on other sites More sharing options...
wmguk Posted November 23, 2008 Author Share Posted November 23, 2008 Hi, Thanks for all of that I've managed to create the templated code, and the database etc all im having trouble with is actually creating the home.php or contactus.php page etc... $name = "aboutus" $fp = fopen($name . ".php", "x+"); $str = "<B>This is the text for the php file created through php programming</B>"; fwrite($fp, $str); fclose($fp); but when I run the code above it doesnt create the file called aboutus.php I dont understand why Link to comment https://forums.phpfreaks.com/topic/133762-dynamic-page-creation-using-a-template/#findComment-696946 Share on other sites More sharing options...
elite_prodigy Posted November 23, 2008 Share Posted November 23, 2008 Here's the trick, use my method above, and add an About Us entry into the database. Be sure that the `id` field in `navigation` is the same as `navID` field in the other two fields. So if `id` in `navigation` is 1 then `navID` should also be 1. You host may be blocking the file or it may be being created in a directory you are not looking in. Don't even bother with it, just use the MySQL DB to render the page, less chance of things dissapearing. And if you use fopen(), you will have to re-write the HTML in the file as well, each time it is edited. Link to comment https://forums.phpfreaks.com/topic/133762-dynamic-page-creation-using-a-template/#findComment-696953 Share on other sites More sharing options...
wmguk Posted November 23, 2008 Author Share Posted November 23, 2008 Hi, The site is already designed and working, and I have everything working as it should, all im trying to do is create a page with all the html layout etc already inserted but the editing only changes the text. each page has a script of: <? include 'admin/scripts/connection.php'; $sql="SELECT * FROM cmssystem WHERE name='WHATEVERTHEPAGEISCALLED'; $result=mysql_query($sql); while($row = mysql_fetch_array($result)) { echo $row['content']; } mysql_close($con); ?> Which is in the middle of the page, meaning I all the amending etc is done with out changing any html etc... for some reason I just cannot get it to create a php file.... I'm trying $filename = "script.php"; $code = "TEST TEST TEST"; $create = fopen($filename, 'w') or die("The file could not be created, please try again later"); fwrite($create, $code); fclose($create); in $filename i've also tried http://www.domain.co.uk/script.php and /var/www/vhosts/domain.co.uk/httpdocs/script.php but it still will not create a new file... Link to comment https://forums.phpfreaks.com/topic/133762-dynamic-page-creation-using-a-template/#findComment-696975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.