-
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
I'm trying to write an arroy of information to a text file. I tested it and in the text file itjust said
"Array" instead of the info inside the array. How Do i fix this? Thanks -
-
CommentAuthorgnome
- CommentTimeJun 23rd 2008
You could use a loop to write each element of the array to the file, instead of just a reference to the array. -
-
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
what is the syntax for a loop? -
- CommentAuthorfernbap
- CommentTimeJun 23rd 2008 edited by fernbap on the 23rd June 2008 at 12:43:13 EDT
considering you have an array $array:
if(!$fp = fopen("file.txt", "w") die ("could not open file for writing");
foreach( $array as $array1 ) {
fwrite($fp, $array1."\n\r");
}
fclose($fp); -
- CommentAuthorJimBamir
- CommentTimeJun 23rd 2008
That's how i do it:public static function fileWrite($file,$inhalt) {
if (!@is_file($file)) {
$rechteAendern = true; // Wird überprüft ob die Datei gerade erst erstellt wurde
} // End If (is_file)
if ($fd = fopen($file,'wb')) {
$res = fwrite($fd,$inhalt); // Schreiben der Daten in die Datei
fclose($fd); // Closen des Filestreams
if ($res===false) {
return false; // Sollte das schreiben nicht erflogreich gewesen sein wird false returned
} // End if $res
if ($rechteAendern) { // Wurde die Datei gerade erst erstellt, so werden die Rechte für die Datei geändert
self::rechteFixxen($file);
} // End If "rechte"
return true;
} // End if fopen
return false;
} // End of func fileWrite -
- CommentAuthorwfiedler
- CommentTimeJun 23rd 2008
Hi Jim,
waere cool, wenn du für unsere Freunde hier die Kommentare und Variablen in Englisch machen würdest
-
- CommentAuthorMattKern
- CommentTimeJun 23rd 2008
If it is info that you want to pull out of the file at a later date, you might want to serialize it before you write it. -
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
Sie sprechen nur Deutsch? -
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
Posted By: MattKernIf it is info that you want to pull out of
what does that mean? -
- CommentAuthorJimBamir
- CommentTimeJun 23rd 2008
Ehm ok, i'll try and translate the comments later this evening. -
- CommentAuthorwfiedler
- CommentTimeJun 23rd 2008
Sie sprechen nur Deutsch?
Do you speak German Connor? Cool!
BTW: I asked Jim to translate the comments in English. It's better for all the guys and girls here... -
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
Posted By: fernbapconsidering you have an array $array:
if(!$fp = fopen("file.txt", "w") die ("could not open file for writing");
foreach( $array as $array1 ) {
fwrite($fp, $array1."\n\r");
}
fclose($fp);
What would the synatax be in the case i wanted to add a str_replace in there... -
- CommentAuthorJimBamir
- CommentTimeJun 23rd 2008 edited by JimBamir on the 23rd June 2008 at 13:59:06 EDT
Posted By: conartistdesigns
Posted By: fernbap considering you have an array $array:
if(!$fp = fopen("file.txt", "w") die ("could not open file for writing");
foreach( $array as $array1 ) {
fwrite($fp, $array1."\n\r");
}
fclose($fp);
What would the synatax be in the case i wanted to add a str_replace in there...
I don't really get the point in doing it ô.0
€dit: Now i got it^^
That's what i found by using google - 2 secs of work ;)
Klick Me! -
- CommentAuthorMattKern
- CommentTimeJun 23rd 2008
serialize is if you want to keep an array intact for use at a later date. This is good for storing options and such.$myOptions = array('backgroundColor' => 'blue');
//turns your array into a string
$serialized = serialize($myOptions);
//paraphrasing here
fwrite($handle,$serialized);
//2 days later - still paraphrasing
$string = (some process to open your file and get the string)
//turns your string back into your array
$array = unserialize($string);
echo $array['backgroundColor'];
//prints 'blue'
Does that somewhat make sense? I am too lazy to write out all the file save/open stuff. -
- CommentAuthorfernbap
- CommentTimeJun 23rd 2008 edited by fernbap on the 23rd June 2008 at 14:13:50 EDT
there is a simpler way of making the text easily retrievable:
add a separator ("|" is a good idea).
This way, you get a text file data1|data2|data3|.....|data3
Retreiving it could not be simpler:
$array=explode("|", text); -
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008 edited by conartistdesigns on the 23rd June 2008 at 14:21:24 EDT
i'm working on this for a blogging platform so that the user types a post and it puts all the info into the text file and the first thing that gets added is<span class="blank"></span>
Then everytime they submit a new entry the script overwrites<span class="blank"></span>putting the new info at the top. from another page that info is called and displayed? how can I do this with str_replace? and what you have submitted -
- CommentAuthorMattKern
- CommentTimeJun 23rd 2008
Are you writing the spans into the file with the blog text? -
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
yes -
- CommentAuthorMattKern
- CommentTimeJun 23rd 2008 edited by MattKern on the 23rd June 2008 at 14:32:50 EDT
It is best practice to separate data from formatting.
You will save yourself alot of hassle down the road.
ie. if in 6 months you decide class=blank needs to be class=blank2, you will have to update all your data files for a formatting change.
Best to avoid this. -
- CommentAuthorfernbap
- CommentTimeJun 23rd 2008 edited by fernbap on the 23rd June 2008 at 16:55:53 EDT
$pagefile=str_replace ("<span class="blank">, "<span class="blank">.$yourtext, $pagefile); -
- CommentAuthorJimBamir
- CommentTimeJun 23rd 2008
This is my filewrite function with english Comments. You are free to use it as you like.
public static function fileWrite($file,$inhalt) {
if (!@is_file($file)) {
$rechteAendern = true; // checks if the files just created
} // End If (is_file)
if ($fd = fopen($file,'wb')) {
$res = fwrite($fd,$inhalt); // writes the data into the file
fclose($fd); // Closen des Filestreams
if ($res===false) {
return false; // if the process of writing the file was not succesfull, returns a flase
} // End if $res
if ($rechteAendern) { // if the file was just created, fixes the rights for the file (it's an external function)
self::rechteFixxen($file);
} // End If "rechte"
return true; // returns true when the writing process was successfull
} // End if fopen
return false;
} // End of func fileWrite
Regards
Jim -
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
is there an way other than mine that will add the new information to the beginning of the txt file without deleting what is already there? some mode or something? -
- CommentAuthorfernbap
- CommentTimeJun 23rd 2008
for placing text in the beginning of a file $file:
$oldfile=file_get_contents($file); //reads the full contents of the file
$fp=fopen($file, "w"); //will overwrite the file
fwrite($fp, $yourtext.$oldfile); // writes your text + previous content
fclose($fp); -
- CommentAuthorJimBamir
- CommentTimeJun 23rd 2008
Hmm it's still overwriting the old content. There has to be a more efficient possibility to do solve this problem ...
Ah it's nearly half past eleven, I'm not able to think anymore ;)
GN8 at all
greetz
amir -
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
fernbap... that worked perfectly! -
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
i'm trying to put a time stamp on the form to. with a hidden input feild and the txt always displays "date(\'j F, Y, g:i a \')" in the txt file instead of a date and time?
this was my input in the file edit-index.php:<input name="date" type="hidden" id="date" value="date('j F, Y, g:i a ')" /> -
- CommentAuthorfernbap
- CommentTimeJun 23rd 2008
hmm... you don't need to include the time in the form, just get the time at the moment you save the submited entry. -
- CommentAuthorconartistdesigns
- CommentTimeJun 23rd 2008
k. sounds easy enough -
- CommentAuthorfernbap
- CommentTimeJun 24th 2008 edited by fernbap on the 24th June 2008 at 05:47:44 EDT
The input code given above looks like a ASP code
You're wrong, it's the other way around: ASP looks like PHP
i usually use SQLite, a database manager library embebbed in PHP 5.
Virtually same syntax as MySQL, and no need of a database server, so works anywhere. -
- CommentAuthorJimBamir
- CommentTimeJun 24th 2008
Yeah it's PHP, i normally use MySql :)
greets
Amir -
-
CommentAuthormarkwest
- CommentTimeJun 24th 2008
Note that for all relatively new php versions at least there is the function file_put_contents to go with file_get_contents. Simplifies the writing of content to files quite a bit.
-Mark -
1 to 31 of 31
