Results 1 to 3 of 3

Thread: Writing and reading a file..

  1. #1
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Writing and reading a file..

    I got a form that'll create something like this to a text file everytime the form is submited, It'll write this to a single text file..

    ID|Name|Blog Post|Date

    How would i make it read something like

    Created By [Name]
    [Blog Post]
    Blog posted on [Date]
    Last edited by Jeffreyv1987; 11-13-2009 at 09:52 PM.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    If you want to write a script to do what you are asking you will need a multidimensional array. I would really discourage you from doing this. What you really want to do is to put the data into a MySQL database like Wordpress and phpbb do to store their data.

    The data can easily be retrieved with a script like

    PHP Code:
    <?php
    $conn
    =mysql_connect("host""username""password");
    mysql_select_db("database_name",$conn) or die("Unable to select database");
    $query "SELECT ID FROM maincontent ORDER BY ID desc limit 0,3";
      
    $result mysql_query($query,$conn) or die ("Couldn't execute query.");
    while (
    $list_info mysql_fetch_array($result,MYSQL_ASSOC)) {
    $tadaa$list_info['ID'];$me.="pop $tadaa pop<br>";}
    echo 
    "$me";
    ?>
    The following will open a text file and sort the contents based on what line it is on and by comma. It should run fine by itself, but just remember that this is a bad idea. It will need to get more complicated the more complicated the data your users submit.

    PHP Code:
    <STYLE TYPE="TEXT/CSS"> 
    textarea{background-color:#a9a9a9;color:black;} 
    body{ 
    background-color:tan; 

    </style> 
    <?php
    $data
    =@$_POST['data'];
    $url=@$_POST['url'];

    if (
    $url != "")  

    $handle file_get_contents("$url",NULL); 
    $data=htmlentities($handle); 
    }
    $arr=array_map(create_function('$a''return preg_split(\'[,]\', $a);'), preg_split('/\n/'$data));
    ?> 

    <form action=<?php echo $_SERVER['PHP_SELF']; ?> method="POST"> 
    copy and place your data in here or enter the url where the data is stored<br>in the space provided at the end:<br>
    the data will be sorted based on commas and newlines.<br>
    <textarea name="data" cols=55 rows=25><?php echo "$data"?></textarea><br> 
    url of .txt document to scan (optional): <br><input type="text" name="url" size=75 value=<?php echo @$_POST['url']; ?> > 
    <br><br> 
    <input type='submit' name="queryButton" value="Submit and Add"> 
    </form> 

    row 1 col 1 has a value of <?php print $arr[0][0]; echo "<br><br> 

    <pre>"
    ;
    print_r($arr); 
    echo
    "</pre>";
    ?>
    Still, it creating flatfile manipulation programs like what you are asking for is a good exercise and can be fun. The above can also be used for learning how to sort data and create multidimensional arrays.
    Last edited by james438; 11-14-2009 at 02:17 AM. Reason: added another sample script
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Alright thanks.. I went with the Mysql Database it was much easier thanks

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
  •