Page 1 of 1

php form that will create a new webpage

Posted: Thu Oct 14, 2010 5:59 pm
by guitars4god7
I am working on my first database driven site that has a form that allows users to submit essentially a quote to a database. My php file is working perfectly and submitting the required information but I would like to be able to create a new html file into a directory each time a user submits a new quote. Each html page will be based off a template so only the quote they add will change. Then there will be a webpage for each quote ie http://www.domain.com/quotes/712. I have figured out how to create the file and how to submit one line into the file but I have had no luck placing a couple hundred lines of html into it. Any help would be greatly appreciated!! Chance

Code: Select all

$q = "select MAX(id) from likes";
$result = mysql_query($q);
$data = mysql_fetch_array($result);
$idnumber1 = $data[0];


$myFile = "quotes/".$idnumber1.".html";
$fh = fopen($myFile, 'w') or die("can't open file");
$str = "This is a test";
fwrite($fh, $str);
fclose($fh);

Re: php form that will create a new webpage

Posted: Thu Oct 14, 2010 6:22 pm
by requinix
No offense but you're doing it wrong.

All based on a template, right? Something you could easily create with PHP if you had the information, right?
Use URL rewriting to turn /quotes/123 into (for example) /quotes.php?id=123. Then write PHP to fill in the template.

Re: php form that will create a new webpage

Posted: Thu Oct 14, 2010 6:38 pm
by guitars4god7
I don't take offense at all and figured I was doing it wrong anyways so I could surely use the advice.

I did some quick searches for url rewriting with php and couldn't find anything helpful. Would you be able to give a link or something to an example of how to do it? If I have something to go by I can surely figure out how to implement it. Thanks for your help

Re: php form that will create a new webpage

Posted: Thu Oct 14, 2010 6:40 pm
by John Cartwright
Search under mod rewrite, which has a ton of information out there.

Re: php form that will create a new webpage

Posted: Fri Oct 15, 2010 11:38 am
by guitars4god7
Ok so I figure a simple .htaccess file like this should do the trick but its still not working... what am I missing? Also what would be the php variable to use to do a mysql query with to find the appropriate quote for the page they requested?

RewriteEngine on
RewriteRule /quotes/([0-9]+) /quotes/quotes.php?id=$1

Re: php form that will create a new webpage

Posted: Fri Oct 15, 2010 10:06 pm
by requinix
mod_rewrite can be weird, at times. Try

Code: Select all

RewriteEngine on
RewriteBase /
RewriteRule quotes/([0-9]+) quotes/quotes.php?id=$1 [L]
(The [L] means that's the last rule to execute. Most people assume it will be anyways and write rules accordingly.)

Re: php form that will create a new webpage

Posted: Sat Oct 16, 2010 12:54 am
by guitars4god7
Not exactly right but it got me on the right track so atleast I am making progress. This is the code that worked:

Code: Select all

RewriteEngine on
RewriteRule ^quotes/([0-9]+)$ quotes/quotes.php?id=$1 [L]
Now whenever someone types http://www.domain.com/quotes/5 it gives that template which is perfect! I just finished editing the php in the template to display the appropriate quote depending on what id number is entered. Thanks for your help everyone. Problem solved :D