Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Creating/appending/linking to document (help writing blog)
Old 09-09-2007, 04:33 AM Creating/appending/linking to document (help writing blog)
Junior Talker

Posts: 1
Trades: 0
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 Code:
// to create url of blog post page
                    
                        $specialchars=array('/','!','&','*',"'",'"','.','?','@',';');
                        
        $pretitleurl1=str_replace('','',str_replace($specialchars,'',$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','$rawtags')";
            
                        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, '<h1>');
                            
                        fwrite($fileopen, $stringdata);
                            
                        fwrite($fileopen, '</h1>');
                        
                        // Dropcap
                        
                        $text=stripslashes($rawtext);
                        
                        $stringdata = $text;
                        
                        $cap=substr($stringdata, 0, 1);
                        
                        fwrite($fileopen, '<span class="dropcap">');
                        
                        fwrite($fileopen, $cap);
                        
                        fwrite($fileopen, '</span>');
                        
                        // Other text
                        
                        $text=stripslashes($rawtext);
                        
                        $text=substr($text, 1);
                        
                        $stringdata = $text;

                        fwrite($fileopen, $stringdata);
                        
                        // Write bottom of page
                        
                        $stringdata = 

                            "</div>
                            
                            <?php include ('../resources/PHP/footer.php'); ?>
                            
                            </body>
                            </html>";

                        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.
evhan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-12-2007, 12:00 PM Re: Creating/appending/linking to document (help writing blog)
Skilled Talker

Posts: 62
Name: Tom Wright
Location: Brighton, UK
Trades: 0
Right. Well my initial advice would be to not attempt to build something as complicated as a blogging system from scratch - there are loads of great options out there that are highly customisable.
__________________
My site:
Please login or register to view this content. Registration is FREE
tomythius is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Creating/appending/linking to document (help writing blog)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15967 seconds with 12 queries