Slowie Posted February 8, 2012 Share Posted February 8, 2012 hey guys its me again. what i want is for me to be able to edit php using variables and post it as a new file. now to attchieve this i am simply copying the current template into the php file and telling it to grap what variables it needs. i have successfully done this with a .css file but when im doing it with a php file its erroring at <?php as the file is already in php. what i need to know is can i have the php file look at the <?php as just text to write to a file? my code is below $dir = "../Header/"; $file = "" . $branchid . ".php"; $dir = "$dir"."$file"; touch($dir); $Handle = fopen($dir, 'w'); $Data = " <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?php //We count the number of new messages the user has $nb_new_pm = mysql_fetch_array(mysql_query('select count(*) as nb_new_pm from pm where ((user1="'.$_SESSION['user_id'].'" and user1read="no") or (user2="'.$_SESSION['user_id'].'" and user2read="no")) and id2="1"')); //The number of new messages is in the variable $nb_new_pm $nb_new_pm = $nb_new_pm['nb_new_pm']; ?> <html xmlns="http://www.w3.org/1999/xhtml"> <style type="text/css"> /* Remove margins from the 'html' and 'body' tags, and ensure the page takes up full screen height */ html, body { margin:0; padding:0;} /* Set the position and dimensions of the background image. */ #page-background {position:fixed; top:0; left:0; width:100%; height:100%;} /* Specify the position and layering for the content that needs to appear in front of the background image. Must have a higher z-index value than the background image. Also add some padding to compensate for removing the margin from the 'html' and 'body' tags. */ #content {position:relative; z-index:1; padding:10px;} </style> <head> <title>Apollo - Data Management System</title> <!--Links--> <link rel="icon" type="image/ico" href="favicon.ico"></link> <link rel="stylesheet" media="all" type="text/css" href="/styles.css" /> </head> <body> <div id="Headerlogo"> <img src="/images/PaulKemp/PaulBanner.png" width="100%" alt="Smile"></div> <div class="wrapper1"> <div class="wrapper"> <div class="nav-wrapper"> <div class="nav"> <ul id="navigation"> <li class="active"> <a href="/Main/myaccount.php"> <span class="menu-left"></span> <span class="menu-mid">Main Menu</span> <span class="menu-right"></span> </a> </li> <li class=""> <a href="/PersonnelMessages/list_pm.php"> <span class="menu-left"></span> <span class="menu-mid"><b>Messages(<?php echo $nb_new_pm; ?> unread)</b></span> <span class="menu-right"></span> </a> <div class="sub"> <ul> <li> <a href="/PersonnelMessages/new_pm.php">New Personal Message</a> </li> </ul> <div class="btm-bg"></div> </div> </li> <?php if (checkMasterAdmin()) { ?> <li class=""> <a href="#"> <span class="menu-left"></span> <span class="menu-mid">Admin</span> <span class="menu-right"></span> </a> <div class="sub"> <ul> <li> <a href="/Admin/UploadServices.php">New KPI</a> </li> <li> <a href="/Admin/MonthReview.php">New Month Review</a> </li> <li> <a href="/Admin/CreateUser.php">New User</a> </li> <li> <a href="/Admin/EditUsers.php">Edit User</a> </li> <li> <a href="/Admin/HolidayRequests.php">Authorise Holidays</a> </li> <li> <a href="/Backup/BackupSite.php">Backup</a> </li> <li> <a href="/PersonnelMessages/CompanyAnnouncement.php">Company Announcements</a> </li> <li> <a href="/PersonnelMessages/DeleteAnnouncements.php">Delete Announcements</a> </li> </ul> <div class="btm-bg"></div> </div> </li> <?php } ?> <?php if (checkAdmin()) { ?> <li class=""> <a href="#"> <span class="menu-left"></span> <span class="menu-mid">Admin</span> <span class="menu-right"></span> </a> <div class="sub"> <ul> <li> <a href="/Admin/UploadServices.php">New KPI</a> </li> <li> <a href="/Admin//MonthReview.php">New Month Review</a> </li> <li> <a href="/Admin/CreateUser.php">New User</a> </li> <li> <a href="/Admin/EditUsers.php">Edit User</a> </li> <li> <a href="/Admin/HolidayRequests.php">Authorise Holidays</a> </li> <li> <a href="/Backup/BackupSite.php">Backup</a> </li> <li> <a href="/PersonnelMessages/CompanyAnnouncement.php">Company Announcements</a> </li> <li> <a href="/PersonnelMessages/DeleteAnnouncements.php">Delete Announcements</a> </li> </ul> <div class="btm-bg"></div> </div> </li> <?php } ?> <?php if (checkBranchOwner()) { ?> <li class=""> <a href="#"> <span class="menu-left"></span> <span class="menu-mid">Admin</span> <span class="menu-right"></span> </a> <div class="sub"> <ul> <li> <a href="/Admin/UploadServices.php">New KPI</a> </li> <li> <a href="/Admin//MonthReview.php">New Month Review</a> </li> <li> <a href="/Admin/CreateUser.php">New User</a> </li> <li> <a href="/Admin/EditUsers.php">Edit User</a> </li> <li> <a href="/Admin/HolidayRequests.php">Authorise Holidays</a> </li> <li> <a href="/Backup/BackupSite.php">Backup</a> </li> <li> <a href="/PersonnelMessages/CompanyAnnouncement.php">Company Announcements</a> </li> <li> <a href="/PersonnelMessages/DeleteAnnouncements.php">Delete Announcements</a> </li> </ul> <div class="btm-bg"></div> </div> </li> <?php } ?> <li class=""> <a href="#"> <span class="menu-left"></span> <span class="menu-mid">Staff</span> <span class="menu-right"></span> </a> <div class="sub"> <ul> <li> <a href="/Staff/ChangePassword.php">Change Password</a> </li> <li> <a href="/Staff/mysettings.php">Change Details</a> </li> <li> <a href="/Staff/HolidaysLeft.php">Holidays Left</a> </li> <li> <a href="/Staff/profile.php">My Profile</a> </li> <li> <a href="/Staff/MyServices.php">My Services</a> </li> <li> <a href="/Viewmonthreports.php">Month Review</a> </li> </ul> <div class="btm-bg"></div> </div> </li> <li class=""> <a href="#"> <span class="menu-left"></span> <span class="menu-mid">Referrers</span> <span class="menu-right"></span> </a> <div class="sub"> <ul> <li> <a href="/Referrers/Newreferrers.php">New Referrers</a> </li> <li> <a href="/Referrers/SendReferrers.php">Send Referrers</a> </li> </ul> <div class="btm-bg"></div> </div> </li> <li class=""> <a href="#"> <span class="menu-left"></span> <span class="menu-mid">Holidays</span> <span class="menu-right"></span> </a> <div class="sub"> <ul> <li> <a href="/Holidays/RequestHoliday.php">Book Off Holiday</a> </li> <?php if (checkAdmin()) { ?> <li> <a href="/Admin/HolidayAmendments.php">Amend Holidays</a> </li> <li> <a href="/Admin/CancelledHolidays.php">Show Cancelled Holidays</a> </li> </li> <li> <a href="/reports/holidayreports.php">holiday report</a> </li> <?php } ?> <?php if (checkMasterAdmin()) { ?> <li> <a href="/Admin/HolidayAmendments.php">Amend Holidays</a> </li> <li> <a href="/Admin/CancelledHolidays.php">Show Cancelled Holidays</a> </li> <li> <a href="/reports/holidayreports.php">holiday report</a> </li> <?php } ?> </ul> <div class="btm-bg"></div> </div> </li> <li class=""> <a href="/logout.php"> <span class="menu-left"></span> <span class="menu-mid">Logout</span> <span class="menu-right"></span> </a> </li> </ul> </div> </div> </div> </div> <br /> "; fwrite($Handle, $Data); $Data = "Bilbo Jones\n"; fwrite($Handle, $Data); print "Data Written"; fclose($Handle); Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/ Share on other sites More sharing options...
digibucc Posted February 8, 2012 Share Posted February 8, 2012 you shouldn't have so much html in your php file. i would have an external file that is the template for what you want to create, and you fopen that into a string and then str_replace markers with values in your script. for example, instead of: <span class="menu-mid"><b>Messages(<?php echo $nb_new_pm; ?> unread)</b></span> you'd have something like: <span class="menu-mid"><b>Messages(/*-msgunread-*/ unread)</b></span> in a template file. then you open that template file, and replace the placeholders with the data <?php $data = fopen(template.htm, "r") // with the appropriate checks of course str_replace( '/*-msgunread-*/', $nb_new_pm, $data); //etc, str_replace your vars $handle = fopen(dest_file.htm, 'w'); // with the appropriate checks of course fwrite($handle, $data); // with the appropriate checks of course ?> and then write the new file. if it's php, make it php - but you are writing a lot of html and if you write the vars into a static file why make it php? Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1315872 Share on other sites More sharing options...
Slowie Posted February 8, 2012 Author Share Posted February 8, 2012 ok ive made this work in one way by having it include the php file instead but how would i edit the data in the newly created file? $header = include("../Header/headerj.php"); $dir = "../Header/"; $file = "" . $name . ".php"; $dir = "$dir"."$file"; touch($dir); $Handle = fopen($dir, 'w'); $Data = "" . $header . ""; fwrite($Handle, $Data); print "Data Written"; fclose($Handle); if i include this file and change some of the things in the original php file whats being included to variables would it pull the variables from the current php file? Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1315874 Share on other sites More sharing options...
Slowie Posted February 8, 2012 Author Share Posted February 8, 2012 on your advice i did this $banner = "/images/" . $name . "/Banner.png"; $data = fopen("../Header/Template.php", "r"); // with the appropriate checks of course str_replace( '/*-headerbanner-*/', $banner, $data); //etc, str_replace your vars $handle = fopen("../Header/" . $name . ".php", 'w'); // with the appropriate checks of course fwrite($handle, $data); // with the appropriate checks of course print "Data Written"; fclose($handle); however the file generated only has "Resource id #12" inside. whats happening with it? Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1315888 Share on other sites More sharing options...
digibucc Posted February 8, 2012 Share Posted February 8, 2012 i'm sorry, change <?php $data = fopen("../Header/Template.php", "r"); // with the appropriate checks of course str_replace( '/*-headerbanner-*/', $banner, $data); ?> to: <?php $template = fopen("../Header/Template.php", "r"); // with the appropriate checks of course $data = str_replace( '/*-headerbanner-*/', $banner, $template); ?> and wrap your code in <?php ?> tags, as it makes it easier to read. Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1315896 Share on other sites More sharing options...
Slowie Posted February 8, 2012 Author Share Posted February 8, 2012 ah thank you ive tried this and yet still im getting the error my code is <?php $banner = "/images/" . $name . "/Banner.png"; $template = fopen("../Header/Template.php", "r"); // with the appropriate checks of course $data = str_replace( '/*-headerbanner-*/', $banner, $template); //etc, str_replace your vars $handle = fopen("../Header/" . $name . ".php", 'w'); // with the appropriate checks of course fwrite($handle, $data); // with the appropriate checks of course print "Data Written"; fclose($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1315937 Share on other sites More sharing options...
digibucc Posted February 8, 2012 Share Posted February 8, 2012 try this: <?php $banner = "/images/" . $name . "/Banner.png"; $template = file_get_contents('../Header/Template.php'); $data = str_replace( '/*-headerbanner-*/', $banner, $template); //etc, str_replace your vars file_put_contents('../Header/'. $name. '.php', $data); print "Data Written"; ?> but if the folder structure is always "/images/" . $name . "/Banner.png"; i would put it in the template as "/images//*-headerbanner-*//Banner.png" and then: <?php $template = file_get_contents('../Header/Template.php'); $data = str_replace( '/*-headerbanner-*/', $name, $template); //etc, str_replace your vars file_put_contents('../Header/'. $name. '.php', $data); print "Data Written"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1315962 Share on other sites More sharing options...
Slowie Posted February 9, 2012 Author Share Posted February 9, 2012 i could kick myself for this but its my error because i was referencing a file what wasnt in that directory. spotted it this morning after i had a few pints last night and a sleep. it now works thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1316077 Share on other sites More sharing options...
trq Posted February 9, 2012 Share Posted February 9, 2012 Why are you trying to do this in the first place? It makes little sense to have multiple copies of essentially the same file. Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1316087 Share on other sites More sharing options...
Slowie Posted February 9, 2012 Author Share Posted February 9, 2012 ive made a website where the user might want to add a branch. the website has a login and each user has a branch id associated with it. when they log in it loads a specific css file and header file. what ive done is when the admin user wants to add a new brnch i didnt want a phone call saying could you add it in so ive made a page where they enter the branch name and select a colour then upload a banner and the page created everything like the header file the css file and the images for that branch. it means little interaction from the user and little chance for me to miss something. Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1316093 Share on other sites More sharing options...
trq Posted February 9, 2012 Share Posted February 9, 2012 Your missing the entire point of a 'dynamic website'. Do you think we need to copy files around every time we add a new board to these forums? Better still, do you think a new page is created every time someone asks a new question? No. Store your data in a database and you can use the same page over and over. Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1316095 Share on other sites More sharing options...
Slowie Posted February 9, 2012 Author Share Posted February 9, 2012 im working on that atm but for a quick setup i needed to do this as multiple branches and branch setup wernt asked of originally. my plan for the future is to use a single header file. this was the only copy needed in the future is the css file as each branch will have different colours. thank you for the advice though it is much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/256679-using-php-to-copy-and-edit-another-php-file/#findComment-1316103 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.