cazes Posted March 3, 2012 Share Posted March 3, 2012 Hello, I am a C++ coder who is new to php so please excuse my n00bness. I have a form which I am then passing into a MySQL database via a php script. I am also using fwrite to try and create a .php site out of the variables entered into the form. Everything is working fine and I have managed to do this with a .html site but when I try to pass php into fwrite it doesn't work. example code $newhtml = fopen("backup/$v_uniquename.php", "w"); fwrite($newhtml, ' <html code here> '); works like a charm but when I use something like (just an example) $newhtml = fopen("backup/$v_uniquename.php", "w"); fwrite($newhtml, ' <?php while($row = mysql_fetch_array($result_date)){ $date = $row['1']; $team1 = $row['2']; $team2 = $row['3']; ?> '); I get errors and I am assuming it is because I am passing using ' ' within the php code within a fwrite function. Is this correct? and is there a work around way to make a php page using this method or something similar. Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/ Share on other sites More sharing options...
scootstah Posted March 3, 2012 Share Posted March 3, 2012 What are the errors? Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323606 Share on other sites More sharing options...
cazes Posted March 3, 2012 Author Share Posted March 3, 2012 What are the errors? Parse error: syntax error, unexpected T_LNUMBER and it happens on everyline within my php code that contains a ' thanks for help Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323611 Share on other sites More sharing options...
scootstah Posted March 3, 2012 Share Posted March 3, 2012 You'll need to escape the single quotes. So this should work: fwrite($newhtml, ' <?php while($row = mysql_fetch_array($result_date)){ $date = $row[\'1\']; $team1 = $row[\'2\']; $team2 = $row[\'3\']; ?> '); Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323613 Share on other sites More sharing options...
cazes Posted March 4, 2012 Author Share Posted March 4, 2012 You'll need to escape the single quotes. So this should work: fwrite($newhtml, ' <?php while($row = mysql_fetch_array($result_date)){ $date = $row[\'1\']; $team1 = $row[\'2\']; $team2 = $row[\'3\']; ?> '); Thanks mate worked perfectly - if you ever need C++ help let me know. Thanks again!! Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323616 Share on other sites More sharing options...
cazes Posted March 4, 2012 Author Share Posted March 4, 2012 I have now run into another problem with fwrite, I believe it has to do with scope of my functions and I am sure there is a simple work around. As above I am using a form to input data, something like $v_date=$_POST['date']; then I am shooting it off to my MySQL database with something like $query="insert into table(date) values('$v_date') mysql_query($query) then using fwrite to create a php file and I have everything working fine up until I want to reference the user input from the form ($v_date) in my fwrite .php output file by using ".$v_date." I would assume it has to do with scope, as the php file can not reference the user input once it is created with fwrite. Is there another way to do this without calling the data from MySQL database? Basically I want to send the users input to the MySQL database and to the .php output of fwrite Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323627 Share on other sites More sharing options...
scootstah Posted March 4, 2012 Share Posted March 4, 2012 You could maybe use a session to keep the data. Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323631 Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2012 Share Posted March 4, 2012 You shouldn't be writing php code to a file (unless it's something like a configuration file) and certainly NOT just the while(){} loop code snippet you have shown in this thread. What overall goal are you trying to accomplish? It sounds like you are doing something the hardest way possible. Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323635 Share on other sites More sharing options...
cazes Posted March 4, 2012 Author Share Posted March 4, 2012 Possibly - I am not really much of a php coder more into C++ where things make sense I am trying to make a form where data can be entered and then it uses that information to make a variable php website based upon the information entered. The data is also sent to a mysql database (which I have working fine). I dont want to go into sessions they have more problems then they are worth IMO. I am thinking of using a variable to create the php file via fwrite and then use some kind of pathinfo function to recall that and uses that for a database search which can load the dynamic information based upon the file name. Long way around and I am sure there is a much more elegant method. Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323646 Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2012 Share Posted March 4, 2012 Without enough of your code that would reproduce the problem you are having, seeing what exactly you are trying to write to a .php file, how you are then trying to use that .php file, and what end result you expect to produce and output as a web page, your statements concerning data, variables, functions, and scope don't mean anything to us (and some of us have had a LOT of experience deciphering/guessing what vague posts are referring to.) However, making a dynamically produced php based web site, such as this forum for example, does not mean dynamically writing php code to a file(s) and then requesting that file(s). It means having a data driven site where the static php code, that you have completely written and tested, receives/retrieves/generates data, produces the desired output, and then outputs the result back to the browser as a (valid) web page that contains html, javascript, media, and content. Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323652 Share on other sites More sharing options...
cazes Posted March 4, 2012 Author Share Posted March 4, 2012 Here is just some pseudo code I have written up db connect $v_date=$_POST['date']; send $v_date and $v_uniquename to database $newhtml = fopen("vod/$v_uniquename.php", "w"); fwrite($newhtml, ' <html> <body> html code in here <? php <table> <tr> <td> <FONT COLOR=\'FFFFFF\'>\'$v_date\'</font> </td> </tr> </table> ?> </body> </html>' ); So essentially the data entered into the form field $v_uniquename will become the name of the file and $v_date will become the data within a table on that page. so v_uniquename.php will look something like <html> <body> html code in here <? php <table> <tr> <td> <FONT COLOR=\'FFFFFF\'>1/1/2012</font> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/258216-fwrite-to-create-a-php-page-errors/#findComment-1323661 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.