# Creating a new page automatically with php



## twmprys (Jun 4, 2009)

I'm using fopen and fwrite to automatically create a new page when a new member joins (the idea is that they will be able to edit the contents of that page). It's OK until I start to add MySQL commands to go into the content of the new page. Then it doesn't work. Anybody have any ideas?


----------



## MMJ (Oct 15, 2006)

MySQL commands?

Code example?


----------



## twmprys (Jun 4, 2009)

This code successfully creates a new page:
<?php 
$content = <<<EOL

New page

EOL; 
$file = "page.php"; 
$open = fopen($file, "w"); 
fwrite($open, $content); 
fclose($open); 
?>

But this doesn't: 
<?php 
$content = <<<EOL

New page 
<?php 
mysql_connect("localhost", "...", "..."); 
mysql_select_db("..."); 
$result = mysql_query("SELECT * FROM ... ORDER BY ID") 
or die(mysql_error());

while($row = mysql_fetch_array( $result )) { 
echo $row['name']; 
}

EOL; 
$file = "page.php"; 
$open = fopen($file, "w"); 
fwrite($open, $content); 
fclose($open); 
?>

Is it something to do with the fwrite function? Or my server?


----------



## MMJ (Oct 15, 2006)

it's a little obvious why that won't work

fixed:

```
<?php
$content = <<<EOL

New page
EOL;

mysql_connect("localhost", "...", "...");
mysql_select_db("...");
$result = mysql_query("SELECT * FROM ... ORDER BY ID")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
echo $row['name'];
}

$content .= <<<EOL

EOL;
$file = "page.php";
$open = fopen($file, "w");
fwrite($open, $content);
fclose($open);
?>
```
and, please use code tags next time


----------



## twmprys (Jun 4, 2009)

Thanks very much for replying. I've tried what you suggest. However, the php page produced is blank which suggests something is wrong with the php. But I cannot download the page to find out what is wrong. Nor can I change the permissions on the page to allow me to do so.

By the way I don't know how to use "code tags".


----------



## twmprys (Jun 4, 2009)

No, sorry, actually what is happening is that a page IS created, but the MySQL instructions are not transferred to the newly created page - they just show on the original page.

What I want to happen is that a new page is created and that this should display information from a table. Hope I'm making myself understood.


----------



## MMJ (Oct 15, 2006)

try:


```
<?php
$content = <<<EOL

New page
EOL;

mysql_connect("localhost", "...", "...");
mysql_select_db("...");
$result = mysql_query("SELECT * FROM ... ORDER BY ID")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
$content .= $row['name'];
}

$content .= <<<EOL

EOL;
$file = "page.php";
$open = fopen($file, "w");
fwrite($open, $content);
fclose($open);
?>
```
code tags for this forum are: [code][/code] or [php][/php]

put your code between the opening and closing tag.


----------



## twmprys (Jun 4, 2009)

Thanks again for your suggestion. That certainly succeeds in taking a one-off snapshot of the table contents and printing to a new page. What I was hoping to do was create a page which hold the php/MySQl instructions and display any new data added to the table. Is this impossible? Should I use a different php command to fwrite/fopen?


----------



## MMJ (Oct 15, 2006)

```
<?php
$content = 
'

New page

<?php
mysql_connect("localhost", "...", "...");
mysql_select_db("...");
$result = mysql_query("SELECT * FROM ... ORDER BY ID")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
echo $row[\'name\'];
}
?>

';
$file = "page.php";
$open = fopen($file, "w");
fwrite($open, $content);
fclose($open);
?>
```


----------



## twmprys (Jun 4, 2009)

That works! Thanks very much, MMJ. What I was trying to do was use heredocs to send a more complicated set of page instructions than this into a new page - I see you've done it using escapes. Is heredocs not the way to do it, then?


----------



## MMJ (Oct 15, 2006)

heredocs parses variables (like double quotes).


----------



## twmprys (Jun 4, 2009)

So if I put all the htm, ccs, javascript etc within heredocs (it would be very tedious to have to hunt out all the single quotes), and when I come to some php, I just concatenate it within single quotes (with escapes for single quote inside the php) - that should work?


----------



## MMJ (Oct 15, 2006)

You can just put an escape (back-slash, \) before the variable and php won't try to parse it.


```
<?php
$content = <<<EOL

New page

<?php
mysql_connect("localhost", "...", "...");
mysql_select_db("...");
\$result = mysql_query("SELECT * FROM ... ORDER BY ID")
or die(mysql_error());

while(\$row = mysql_fetch_array( \$result )) {
echo \$row['name'];
}
?>

EOL;
$file = "page.php";
$open = fopen($file, "w");
fwrite($open, $content);
fclose($open);
?>
```


----------



## twmprys (Jun 4, 2009)

Thanks so much for your help. What I really want to do is create a new member's page at the same time as a new username is inserted into the database (from a form). I've tried putting the fopen and fwrite commands after the insert command, as below, but a new page is not created. Any idea of what I'm doing wrong?


```
<?php
 mysql_connect("localhost", "...", "...");
    mysql_select_db("...");

 $submit = $_POST["newmember"]; 
if (isset($submit)) {

//Check if username is already in use
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
$check2 = mysql_num_rows($check);
if ($check2 == 1) {
die('Username already in use. [URL=membersadmin.php][U]Please try again.[/U][/URL]');
}

     $query ="INSERT INTO users (name,email,contact,username)";
    $query.=" VALUES ('$name','$email','$contact','$username')";
    $result=mysql_query($query);

//Create new members page
$page = '$username' . '.php';
$open = fopen($page, "w");
fwrite($open,$username);
fclose($open);

	header("location: thispage.php");
	    }
```


----------



## MMJ (Oct 15, 2006)

Variables aren't parsed in single quotes.


----------



## twmprys (Jun 4, 2009)

Ah, of course! Don't believe them when they tell you there's no difference between double and single quotes in php!

Thanks for your help.


----------



## MMJ (Oct 15, 2006)

there is that difference and also in parsing special escape characters \n, \t, \r, \x00. single quotes just ignores them.

anytime


----------

