Creating/appending/linking to document (help writing blog) Alright... I'm a beginning php coder and I think I'm in a bit over my head. I'm writing my own blog (trying, at least), and I've run into some difficulty.I have currently written a php page that will both send information from a form (url, title, text, tags, date, and an auto_incrementing entry number) to a database and create a new page displaying that information. The form's fields are named rawtitle, rawtext, and rawtags. It looks like this (after the form):PHP:---------// to create url of blog post page $specialchars=array('/','!','&','*',"'",'"','.','?','@',';'); $pretitleurl1=str_replace('','',str_replace($speci alchars,'',$rawtitle)); $pretitleurl2=str_replace(' ','-',$pretitleurl1); $titleurl=stripslashes($pretitleurl2); $newfilepath = "blog/$titleurl.php"; $url=$newfilepath; // to send to db $insert="Insert into blogentries (url,title,date,text,tags) values('$url','$rawtitle',now(),'$rawtext','$rawta gs')"; mysql_query($insert) or die("Could not insert post"); // copy and rename template for blog post page copy('blog/template.php',$newfilepath); // write to the file $fileopen = fopen($newfilepath, 'a') or die("Can't open file"); // Title $title=stripslashes($rawtitle); $stringdata=$title; fwrite($fileopen, ''); fwrite($fileopen, $stringdata); fwrite($fileopen, '
'); // Dropcap $text=stripslashes($rawtext); $stringdata = $text; $cap=substr($stringdata, 0, 1); fwrite($fileopen, ''); fwrite($fileopen, $cap); fwrite($fileopen, ''); // Other text $text=stripslashes($rawtext); $text=substr($text, 1); $stringdata = $text; fwrite($fileopen, $stringdata); // Write bottom of page $stringdata = "</div> _uacct = "UA-139461-1";urchinTracker(); "; fwrite($fileopen, $stringdata); // close page fclose($fileopen);---------Apologies if my code is unclear, this is really the first hardcore php I've attempted and I'm well aware that the code itself is extremely shoddy. Allow me to clarify what's going on:The first block wipes all the special characters from the title and saves it as the url of the page. The next block sends all info to the database.Now, here's where it gets weird, mainly because I didn't know another way to do it -- the third section copies a "template" file that holds the (beginning) of the layout of the page being created. The file is then opened and appended, adding the content from the form and the end of the formatting information.Now, this section of the code works, but I'm wondering (actually, I'm sure there is) if there is a better way to do it. I would love some help cleaning up/optimizing the code, and I would also like some direction designing a similar function to create a page for each new tag input to the form displaying all entries with that tag. Once the page is created I know how to fetch and display the entries with the given tag, but I was wondering if you could use one function to create a page for each tag for which there isn't already a page (maybe with an array?). Since php overwrites any file already in existence, I guess it's unnecessary to check for any pages already there and just create a new page for each tag. To view links or images in this forum your post count must be 1 or greater. You currently have 0 posts. |