SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Flat file reading and writing

    Ok so I'm making this shout box for my weblog and so far everything works as planned. It can read and write to the database. But when it writes to the database it overwrites the last post with the new one. Can anyone tell me how to fix this? Shout box can be found here http://zero.cd-dm.net/zero_fenix/

    Oh and I can get it to get the persons IP address like I wanted it to can anyone help me with that to? It just leaves the field blank.

    Read flat file and post.
    PHP Code:
    function get_shout() {
        
    $filename "data/shouts.php";
        
    $handle fopen($filename"r+");
        
    $contents fread($handlefilesize($filename));
        list(
    $username$comment$unixtime) = explode("||"$contents);
        
    $date date("M j, Y"$unixtime);
        
    $time date("Y h:i a"$unixtime);
        
    $date "$date at $time";
        echo 
    "Posted by: <b>$username</b> on $date<br>"
            
    ."<div style=\"overflow:auto;height:40;width:100%;\">"
            
    ."$comment"
            
    ."</div>";
    }; 
    Add shout.
    PHP Code:
    function add_shout() {
        
    $name $_POST['name'];
        
    $comment $_POST['comment'];
        
    $comment str_replace("||""|"$comment);
        
    $comment str_replace('<'"&lt;"$comment);
        
    $comment str_replace('>'"&gt;"$comment);
        
    $comment str_replace('\"'"&quot;"$comment);
        
    $comment str_replace('\\\''"&acute;"$comment);
        
    $date time();
        
    $ipaddress $HTTP_SERVER_VARS['REMOTE_ADDR'];
        
    $content $name "||" $comment "||" $date "||" $ipaddress "||";
        
    $filename "data/shouts.php";
        
    $handle fopen($filename"r+");
        
    fwrite($handle$content);
        
    fclose($handle);
    }; 

  2. #2
    SitePoint Evangelist LemoNade's Avatar
    Join Date
    Aug 2005
    Location
    Southampton, UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the 'Add Shout', try this code:

    PHP Code:
    function add_shout() {
        
    $name $_POST['name'];
        
    $comment $_POST['comment'];
        
    $comment str_replace("||""|"$comment);
        
    $comment str_replace('<'"&lt;"$comment);
        
    $comment str_replace('>'"&gt;"$comment);
        
    $comment str_replace('\"'"&quot;"$comment);
        
    $comment str_replace('\\\''"&acute;"$comment);
        
    $date time();
        
    $ipaddress $_SERVER['REMOTE_ADDR'];
        
    $content $name "||" $comment "||" $date "||" $ipaddress "||";
        
    $filename "data/shouts.php";
        
    $handle fopen($filename"a+");
        
    fwrite($handle$content);
        
    fclose($handle);
    }; 
    I modified the fopen() line so it Appends (a+) the file instead of writing to it, where it writes from the start of the file (hence the overwriting).

    Also, I modified the $ipaddress line to use the more-supported $_SERVER['REMOTE_ADDR'].

    Hope it works

    -Will

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a bunch, that fixed alot of it (IPaddress and new line.) But now I need a way to tell it to read from the last shout rather than the first.

    I tryed changing the fopen to a+ but that didn't help.
    PHP Code:
    function get_shout() {
        
    $filename "data/shouts.php";
        
    $handle fopen($filename"a+");
        
    $contents fread($handlefilesize($filename));
        list(
    $username$comment$unixtime) = explode("||"$contents);
        
    $date date("M j, Y"$unixtime);
        
    $time date("Y h:i a"$unixtime);
        
    $date "$date at $time";
        echo 
    "Posted by: <b>$username</b> on $date<br>"
            
    ."<div style=\"overflow:auto;height:40;width:100%;\">"
            
    ."$comment"
            
    ."</div>";
    }; 
    heres what the database looks like so far also.

    PHP Code:
    <?php die("You do not belong here leave now!"); ?>
    ZeRo_FeniX||This is the semi-working shout box. 3||1132466732||||
    ZeRo_FeniX||tESTING 1 2 6||1132506117||xxx.217.128.165||
    ZeRo_FeniX||Testing 1 3 9||1132506207||xxx.217.128.165||
    ZeRo_FeniX||Shouting||1132506371||xxx.217.128.165||
    Also if someone posts multiple lines its put into the database on multiple lines, is there any way to change it to one line and just at <br /> at the ends?

  4. #4
    SitePoint Evangelist LemoNade's Avatar
    Join Date
    Aug 2005
    Location
    Southampton, UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't see a fix for the first problem right now, but for the <br /> problem just use the following:

    PHP Code:
    function add_shout() {
        
    $name $_POST['name'];
        
    $comment $_POST['comment'];
        
    $comment str_replace("||""|"$comment);
        
    $comment str_replace('<'"&lt;"$comment);
        
    $comment str_replace('>'"&gt;"$comment);
        
    $comment str_replace('\"'"&quot;"$comment);
        
    $comment str_replace('\\\''"&acute;"$comment);
        
    $comment nl2br($comment);
        
    $date time();
        
    $ipaddress $_SERVER['REMOTE_ADDR'];
        
    $content $name "||" $comment "||" $date "||" $ipaddress "||";
        
    $filename "data/shouts.php";
        
    $handle fopen($filename"a+");
        
    fwrite($handle$content);
        
    fclose($handle);
    }; 
    I've added a line there where it calls the nl2br() function on the inputted comment - This changes new lines (\n) to <br />tags.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know for most flat file scripts I'v seen they add the shout news what ever to the first line of the flat file. So I think thats what I realy need to do but I dont know how to without erasing whatever was put in befor. Unless I tell it to read the database get the data and then rewrite to it with the new shout first.


Bookmarks

Posting Permissions

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