View Full Version : Writing and reading a file..
Jeffreyv1987
11-13-2009, 08:51 PM
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]
james438
11-13-2009, 11:01 PM
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
$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.
<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.
Jeffreyv1987
11-14-2009, 05:38 PM
Alright thanks.. I went with the Mysql Database it was much easier thanks
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.