Jump to content

fwrite to create a .php page - errors


cazes

Recommended Posts

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.

Link to comment
Share on other sites

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\'];
   ?>
');

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.