A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: PHP Message Board

  1. #1
    www.stylefrogcreative.com
    Join Date
    Apr 2001
    Posts
    100

    PHP Message Board

    Hi there,

    I've been working on a PHP/Flash message board - I got everything working perfectly, the only problem was the new entries were written at the end of the file as apposed to the start. This meant that the user had to scroll down to see the most recent entries.

    I had some help on this forum before but still did quite get to the bottom of the problem, this is where we got to:

    Code:
    <?
    
    if (!isset($name) || !isset($email) || !isset($message) || empty($name) || empty($email) || empty($message)) {
    print "&result=Fail";
    print "&errorMsg=" . urlencode("Input required for all fields.");
    exit;
    }
    
    $email = strtolower($email);
    
    addentry($name, $email, $message);
    
    function addentry($name, $email, $message) {
    
    $posted = strftime("%D %I:%M %p");
    
    $message = stripslashes($message);
    
    $file = fopen('entry.txt', 'r+');
    flock($file, LOCK_EX);  // tell flock what it should do
    $old = fread($file, filesize($file));  // read in old data first
    fseek($file, 21, 0);
    
    if (!$file) {
    print "&result=Fail";
    print "&errorMsg=" . urlencode("Could not open entry.txt file. Change CHMOD levels to 666.");   // 766 makes no sense for text files 
    exit;
    }
    
    fputs($file, "<font color=\"#000000\">Name:</font> $name\n<font color=\"#000000\">Email:</font> <font color=\"#000000\"><u><A href=\"mailto:$email\">$email</A></u></font><br>\n<font color=\"#000000\">Posted:</font> $posted\n<font color=\"#000000\">Message:</font> $message\n\n");
    fputs($file, $old);  // write back old data
    // flock($file, LOCK_UN); // this would unlock the file after use
    fclose($file);  // but closing it will unlock it anyway
    
    // Send admin an email when new entry occurs
    mailAdmin($name, $email, $message);
    }
    
    function mailAdmin($name, $email, $message) {
    	$mailTo = "my@mail.co.uk";
    	$mailFrom = "From: <my@mail.co.uk>";
    	$mailSubject = "New Guestbook Entry";
    	$mailBody = "A visitor to your site has left the following information in your guestbook:\n
    	Name: $name
    	Email: $email
    	The visitor commented:
    	------------------------------
    	$message 
    	------------------------------
    	You can view the message at:
    	http://www.mysite.com";
    	mail($mailTo, $mailSubject, $mailBody, $mailFrom);
    }
    
    print "&result=okay";
    exit;
    
    ?>
    We were trying to open the file, read 21 characters in (this has to remain the same for readback in Flash), then copy all the current info, paste in the new entry, then paste in the old info afterwards.

    I think we're nearly there but all this script seems to do is simply delete the old entry and write the new one.

    Any help would be much appreciated as it's the last little hurdle on quite a big site I've been working on!

    Many thanks!
    Stylefrog

    matt@stylefrogcreative.com
    http://www.stylefrog.net
    http://www.stylefrogcreative.com

  2. #2
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Well my firts question would be why not use a database instead of a text file, but let me see if I can put this out there as a possible solution:

    have a variable that is set = to the existing text in the file.

    have another variable that is = to the new text from flash

    concat the variables as such:

    $newEntry = $textFromFlash.$oldTextFromFile

    that should put the new string in front of the old text. Then update the file.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center