Current location: Hot Scripts Forums » Programming Languages » PHP » .txt php scripting help!


.txt php scripting help!

Reply
  #1 (permalink)  
Old 07-25-04, 02:47 PM
steveo steveo is offline
Wannabe Coder
 
Join Date: Jun 2004
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
.txt php scripting help!

I recently downloaded a script, for a guestbook and i changed it but when it adds something to a .txt file it adds it to the bottom of that file, making it show at the bottom when viewing it. Like, the newest is at the bottom, well anyway I want to change it so it inputs the information into the .txt file at the top, how would this work?

The script is below.

PHP Code:

<?php


##Things for my person use, nothing to do with my problem.
$name $_POST['name'];
$vocation $_POST['vocation'];
$level $_POST['level'];
$sex $_POST['sex'];
$comments $_POST['comments'];
$armor $_POST['armors'];
$shield $_POST['shields'];
$weapon $_POST['weapons'];
$helmet $_POST['helmets'];
$boots $_POST['boots'];
$legs $_POST['legs'];
$world $_POST['world'];
##End of things.

 
$date date("H:i, jS F");


$outputstring "the output stuff goes here, nothing todo with my problem so i've edited it out.\n";


$fp fopen("eq.txt""a");

  
flock($fp2); 
 
  if (!
$fp)
  {
    echo 
"<p><strong> Your Equipment and comments could not be added at this time. Please try again later.</strong></p></body></html>";
    exit;
  } 

  
fwrite($fp$outputstring);
  
flock($fp3); 
  
fclose($fp);
  
?>
Reply With Quote
  #2 (permalink)  
Old 07-25-04, 03:28 PM
nekeno12 nekeno12 is offline
Wannabe Coder
 
Join Date: May 2004
Location: CO
Posts: 214
Thanks: 0
Thanked 0 Times in 0 Posts
Scratch this script and store to mySql. That would be my solution.
Reply With Quote
  #3 (permalink)  
Old 07-25-04, 03:35 PM
steveo steveo is offline
Wannabe Coder
 
Join Date: Jun 2004
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
One problem, I don't know MYSQL that good, and I don't even know if it stores images or something :S

See? I don't know what I'm talking about, so for me it's better in a .txt
Reply With Quote
  #4 (permalink)  
Old 07-25-04, 03:39 PM
kvnband kvnband is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 242
Thanks: 0
Thanked 0 Times in 0 Posts
Agree with what nekeno said about mysql. But if you're dead set on text files, how about making it easier on yourself by reading from the bottom line up. That way you don't have to write your own function to store at the beginning of the file instead of appending it.

This code might help you out:
PHP Code:

$filename ="file.txt";


$myFile fopen($filename"r"); //open the file for reading, file pointer will be at the beginning of the file

$fcontents file($myFile);

$totalines count($fcontents); //how many lines of data the file contains

$limit 5;

for (
$i 0$i $limit$i++, $totalines--){

$line str_replace("<p>""<li>"$fcontents[$totalines]); //remove P tags, replace with LI tag

if($line != ""){ // if the line from the file is not blank print it
echo "$line \n";

Kevin
Reply With Quote
  #5 (permalink)  
Old 07-25-04, 03:41 PM
kvnband kvnband is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 242
Thanks: 0
Thanked 0 Times in 0 Posts
OH, and by the way, the above code was used in another script, but you should be able to easily adapt it to your needs.

OH, and mysql is really worth learning. Once your text file gets big enough, believe me, you'll wish that you had mysql to work with. Just a friendly tip
Kevin
Reply With Quote
  #6 (permalink)  
Old 07-25-04, 03:56 PM
steveo steveo is offline
Wannabe Coder
 
Join Date: Jun 2004
Posts: 130
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by kvnband
OH, and by the way, the above code was used in another script, but you should be able to easily adapt it to your needs.
NO! I'm struggling already!

>_<
Reply With Quote
  #7 (permalink)  
Old 07-25-04, 04:59 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
Well if you are dead set on writing to the top of the file instead of the bottom, which is what fopen(blah, 'a') does (a for append). Then what you have to do is read in the whole file then write over the entire thing with YournewContents . YouroldContents.

PHP Code:

//grab the current contents

$filename 'filename';

$file_contents file_get_contents($filename) or die(__LINE__.'this blows');

//add newstuff
$newstuff 'I like PHP more than my dog';

$file_contents $newstuff$file_contents;

//then write back
$fp fopen($filename,'wb') or die (__LINE__ .'why not shoot yourself?');
fwrite($fp,$file_contents);
fclose($fp); 
I have no clue if that will work, I just saw it scribbled on some TP in the men's restroom. Hope it helps.
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
php with Apache in windows eDevil PHP 3 08-08-04 12:03 AM
Edit .txt using PHP? move3rd PHP 7 03-28-04 03:10 PM
100 Web Templates & 10 PHP Scripts for sale! HostersUK.co.uk General Advertisements 0 01-10-04 12:31 AM
PHP scripting Denjira PHP 1 07-24-03 11:36 PM


All times are GMT -5. The time now is 04:14 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.