Jump to content

Reading and Writeing to CSV File


spamurai

Recommended Posts

Please can you give me the propper codes for Reading and Writeing to CSV file..Iv been trying to work it out for weeks now and im not getting verry far, i browsed w3school.com but nothing..My problem is i need to Keep evrything on ONE Line in the CSV file.. so if i do a Break in the Textarea before Submiting it will post in the one line not multiple lines.. I hope im not confuzing you, spellings bad i know lol...i need to read the top of the file.. and white to the top of the file, and be able to read each line and colum..Any help will be great :) PS, This is for a News System or Blog...Thankyou for your time James. :)

Link to comment
Share on other sites

Here is my code.. can you tell me what im doing wrong or fix the problem for me.. it works fine but its the Line break problem..

<?phpif (stristr(htmlentities($_SERVER['PHP_SELF']), "body_addnews.php")) {	Header("Location: ../../index.php");	die();}if ($_GET["add"]=="news") {//========| Make sure the fields are not empty |========//if (isset($_POST['msg_title']) !="" && ($_POST['msg_name']) !="" && ($_POST['msg_messege']) !="") {$msg_title = $_POST['msg_title'];$msg_name = $_POST['msg_name'];$msg_dateandtime = $_POST['msg_dateandtime'];$msg_messege = $_POST['msg_messege'];$msg_userip = $_POST['msg_userip'];//========| Start of write |========//@extract($_POST);//See if the file is CHMOD to 777 or 666if(is_writable('includes/db_files/db_news.txt')){//opne the file for writeing$fp = fopen('includes/db_files/db_news.txt','a');$content = "$msg_title||$msg_name||$msg_dateandtime||$msg_messege||$msg_userip\n";//Write the contentfwrite($fp, $content);fclose($fp);	echo "<font color=\"#0000CC\"><B><big>Success!: Your news has been Posted.</big></B></font><BR>";} else {echo "<font color=\"#990000\"><B><big>Error: File is not writable</big></B></font><BR>";}//========| End of write |========//} else {echo "<font color=\"#990000\"><B><big>Error: You need to fill the fields.</big></B></font><BR>";}}echo "<form action=\"?action=addnews&add=news\" method=\"POST\" name=\"postnews\">\n";echo "<table><td>\n";echo "<table width=\"100%\">\n";echo "<tr><td width=\"70\">News title:</td><td><input type=\"text\" name=\"msg_title\" value=\"\"></td></tr>\n";echo "<tr><td width=\"70\">Your name:</td><td><input type=\"text\" name=\"msg_name\" value=\"\"></td></tr>\n";echo "<tr><td width=\"70\">Your Messege:</td><td><textarea name=\"msg_messege\" cols=\"60\" rows=\"12\"></textarea></td></tr></td>\n";echo "</table>\n";echo "<input type=\"hidden\" name=\"msg_dateandtime\" value=\"" .date("j/ n/ Y"). "\">\n";echo "<input type=\"hidden\" name=\"msg_userip\" value=\"" .GetHostByName($REMOTE_ADDR). "\">\n"; echo "</td><tr><td align=\"right\">\n";echo "<input type=\"submit\" style=\"color: #0000CC;\" value=\"Submit News\">";echo "<input type=\"reset\" style=\"color: #990000;\" value=\"Reset News\">";echo "</td></tr></table>\n";echo "</form>";echo "<B>Current Active News</B>:<BR>";$db_newsfile = file("includes/db_files/db_news.txt");foreach($db_newsfile as $key => $val) {   $data[$key] = explode("||", $val);}for($k = 0; $k < sizeof($db_newsfile); $k++) { echo "<table width=\"100%\"><td width=\"150\"><B>Title</B>: " .$data[$k][0]. "</td><td width=\"150\"><B>posted by</b>: " .$data[$k][1]. "</td><td width=\"100\"><b>Date</B>: " .$data[$k][2]. "</td><td> --- Edit, Delete, View</td></table>\n";}?>

i think it has something to do with the textarea or somthing.. Help would be Great!! :):):)

Link to comment
Share on other sites

nope.. ok il try and make a Example..i have a form:TitleName<textarea>Messege in the textarea</textarea>Keep in mind that || splits the colums up so i can read it with $data1 $data2 etc....now if i post new of like this:<textarea>Hello my name is james blah blah blah</textarea>it will Write the content of what i wrote into the txt file as:title||name||Hello my name is james blah blah blahbut if i do it like this:<textarea>title||name||Hello my name is james blah blah blah</textarea>it will Write the content of what i wrote into the txt file as:title||name||Hello my name is james blah blah blahNow if i do post it like this:title||name||Hello my name is james blah blah blahI need it on Submit to change it all into the ONE line in the FlatFile database so i can read a line at a time in the txt fileso in the end when its all submited it looks like this in the txt file:title||name||Hello my name is james blah blah blahdo you know what i mean now? :):)

Link to comment
Share on other sites

This code:

<?php  $str = str_replace(array("\r\n","\n","\r"),"",$str);?>

Will replace all line breaks in the variable $str with empty text. So that is the function call you need. Once you get the text from $_POST, use that function call to replace the line breaks in it, and use whatever variable you have instead of $str.

Link to comment
Share on other sites

Wow, the examples on the strip_tags page aren't very good for if you want to remove all tags but a few. But it looks like the syntax goes like this, for example, if you want to remove all tags, but allow <div>, <p>, and <span>:strip_tags($str, "<div><p><span>");There is another function on the page that is also useful, where it only removes the tags you want. So strip_tags removes all tags but the ones you want, and this function removes only the tags you want:

function strip_selected_tags($text, $tags = array())   {	   $args = func_get_args();	   $text = array_shift($args);	   $tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;	   foreach ($tags as $tag){		   if( preg_match_all( '/<'.$tag.'[^>]*>([^<]*)<\/'.$tag.'>/iu', $text, $found) ){			   $text = str_replace($found[0],$found[1],$text);		   }	   }	   return preg_replace( '/(<('.join('|',$tags).')(\\n|\\r|.)*\/>)/iu', '', $text);   }

usage:

$remove_tags = array('form', 'script', 'input'); //we want to remove all form, script, and input tags$str = strip_selected_tags($str, $remove_tags);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...