LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-12-2003, 12:00 PM   #1
DeadlySin3
LQ Newbie
 
Registered: May 2003
Location: Co
Distribution: Debian 3.0r2.Sarge
Posts: 25

Rep: Reputation: 15
Text File DB -> PHP


Alright, question.

I'm writing a web log program in PHP; it writes data to a .txt file. I would like to be able to add editing capabilities; but i'm sort of unsure how to take the info from the .txt file and have it displayed in form boxes so it can be edited, then re-written.

I went out and bought three books on PHP and none of them really cover the explode and implode functions in detail : which i understand it's essential to use the implode function to do what i want here. Any advice or better yet.. help ?
--------------------------------------------

I'm still working on that same project - that was a post I made to another board, though i don't think it gets read much. Hopefully I can get a little more help here hehe.

One answer I did get though sort of helped, but i'm still not clear on the subject:
----------------------
Code:
 # you can read an entire file into an array using file 
 $allthefilelines = file("myfile.txt"); 
 
 # now, to get the first line of text 
 $firstline = $allthefilelines[0]; 
 
 # to get the second line of text 
 $secondline = $allthefilelines[1]; 
 
 # etc 
 
 # to get how many lines of text 
 $totallines = count($allthefilelines); 
 
 # if each line is stored like this 
 # name|email address|web address|comment 
 # then you could split each line like this 
 list($name,$email,$web,$comment) = explode("|",$firstline); 
 
 # if you wanted to print out everything, for a silly example 
 $allthelines = file("myfile.txt"); 
 for($i=0;$i<count($allthelines);$i++) 
 { 
   $currentline = $allthelines[$i]; 
   list($name,$email,$web,$comment) = explode("|",$currentline); 
   print $name."<br>".$email."<br>".$web."<br>".$comment."<br><br>"; 
 } 
 
 # no need to worry about implode and arrays if you just want to 
 # concatenate a few fields - just use the concatenation operator (period) 
 # so if you make a change to a comment then you can reconstruct the line 
 # just like this - this example makes comment all lowercase 
 list($name,$email,$web,$comment) = explode("|",$currentline); 
 $comment = strtolower($comment); 
 $newline = $name."|".$email."|".$web."|".$comment; 
 
 # however, explode and implode work with arrays so to use them instead 
 # of the list and variable names as I did above you could use the following instead 
 $fields = explode("|",$currentline); 
 $fields[3] = strtolower($fields[3]); 
 $newline = implode("|",$fields);
Anyone help a newbie out? heh
 
Old 05-12-2003, 04:12 PM   #2
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Rep: Reputation: 0
What excactly is yout problem? How is your data stored and what should your view (in your textarea) look like? It would be helpful if you posted some example. Please let me know how your log file's content should look like and how the web-editable version should look like.
Thx so far, Gary
 
Old 05-12-2003, 09:48 PM   #3
DeadlySin3
LQ Newbie
 
Registered: May 2003
Location: Co
Distribution: Debian 3.0r2.Sarge
Posts: 25

Original Poster
Rep: Reputation: 15
Alright - i'm storing the posted data in lines of html. right now i've slimmed the project down quite a bit until I can figure out how to take the needed post and edit in another web form.

Here's whats being written to the text file
Code:
$dbEntry = "<a href=\"mailto:$url\">$name</a>: $comment\n";
And actually, this project here isn't the one I was working on when I originally posted: I just figured for simplicity sake, i'd throw together a quick tagboard - and try from there. That way I don't have so much code to dig through to correct errors and what not. I've written a tagboard in PHP before (deadlysin3.net/tag) and the data storage is pretty much the same.

Anyway, the other board i'm doing the testing from is here http://www.deadlysin3.net/scriptTest

and you can see the code i've written here http://www.deadlysin3.net/scriptTest/code/

The way the web-editable form looks really isn't a big deal. What i'd like though, is two text boxes: one for users name, one for url and one text area that the comments could be put into.

If you need anymore information please let me know..I really appreciate the help, Thanks!
 
Old 05-12-2003, 09:59 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
OT: #3

Just out of curiosity:

Which one is #3? :)

Or, where is the "official canon" of them
to be found?

Pride, Envy, Greed, Wrath, Gluttony, Sloth or Lust? :)

Cheers,
Tink

P.S.: Mine would probably be Pride,
but I'm working on it :)
 
Old 05-12-2003, 10:01 PM   #5
DeadlySin3
LQ Newbie
 
Registered: May 2003
Location: Co
Distribution: Debian 3.0r2.Sarge
Posts: 25

Original Poster
Rep: Reputation: 15
Well lol.. I don't think there is any given order for any of the deadly sins though I could be badly mistaken. I've always considered lust to be the third though
 
Old 05-12-2003, 10:43 PM   #6
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Rep: Reputation: 0
So you want to parse a HTML-file. Well, that's not really a big deal. What you need is searching for patterns. If your HTML-file consequently has this format, you can simply extract its data line by line into an linear array containing the three strings.
Well, here's a piece of
Code:
$fd = file("blah.txt");
print "<form action="update.php">
foreach($fd as $link){
	eregi("mailto:(.*)\">(.*)</a>: (.*), $link, $strings);
	print "\t<br>\n\t<table>\n";
	print "\t\t<tr>\n";
	print "\t\t\t<td>eMail:</td>\n\t\t\t<td><input type=\"text\" name=\"eMail[]\" value=\"" . $strings[0] . "\"></td>\n";
	print "\t\t</tr>\n";
	print "\t\t<tr>\n";
	print "\t\t\t<td>Name:</td>\n\t\t\t<td><input type=\"text\" name=\"Name[]\" value=\"" . $strings[1] . "\">/td>\n";
	print "\t\t</tr>\n";
	print "\t\t<tr>\n";
	print "\t\t\t<td>Comment:</td>\n\t\t\t<td><textarea name=\"comment[]\">$strings[1] . "</textarea></td>\n";
	print "\t\t</tr>\n";
	print "\t</table>\n";
}
print "\t<input type=\"submit\" value=\"Update stuff\">\n</form>\n";
Think that might help you out.

I suggest you know what to write into "update.php"...
It's just opening the HTML-text-file for write-access, a loop (like "for($i=0;$i<count($eMail);$i++){/*...*/}) and in that loop just the printing of the concated HTML-link. Remember: $i then is the index of all entrys in the arrays. That means you can keep you loop being as simply as follows:
Code:
// $fd is the opened file
for($i=0; $i < count($eMail); $i++){
	$link = "<a href=\"mailto:$eMail[$i]\">$Name[$i]</a>: $comment[$i]\n";
	fwrite($fd, $link);
}
All right, think, I'm done for now. You now might be able to write your stuff.
Tip: instead of any output of "update.php" just insert the PHP-code
Code:
header("Location: index.php"); // or whatever
and you will be redirected immediately

So far, Gary
 
Old 05-12-2003, 10:46 PM   #7
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Rep: Reputation: 0
Oups - there should be a colon followed by an opening bracket instead of that smiley up there in the ereg()-funtion
 
Old 05-12-2003, 11:47 PM   #8
DeadlySin3
LQ Newbie
 
Registered: May 2003
Location: Co
Distribution: Debian 3.0r2.Sarge
Posts: 25

Original Poster
Rep: Reputation: 15
hehe

Yeah I figured that

using your code
Code:
<?php
$db = "database.inc";

$fd = file("$db");
print "<form action=\"update.php\">
foreach($fd as $link){
        eregi("mailto:(.*)\">(.*)</a>: (.*), $link, $strings);
        print "\t<br>\n\t<table>\n";
        print "\t\t<tr>\n";
        print "\t\t\t<td>eMail:</td>\n\t\t\t<td><input type=\"text\" name=\"eMail[]\" value=\"" . $strings[0] . "\"></td>\n";
        print "\t\t</tr>\n";
        print "\t\t<tr>\n";
        print "\t\t\t<td>Name:</td>\n\t\t\t<td><input type=\"text\" name=\"Name[]\" value=\"" . $strings[1] . "\">/td>\n";
        print "\t\t</tr>\n";
        print "\t\t<tr>\n";
        print "\t\t\t<td>Comment:</td>\n\t\t\t<td><textarea name=\"comment[]\">$strings[1] . "</textarea></td>\n";
        print "\t\t</tr>\n";
        print "\t</table>\n";
}
print "\t<input type=\"submit\" value=\"Update stuff\">\n</form>\n";

?>
I get an error saying "Parse error: parse error, unexpected T_STRING in /home/deadly/www/scriptTest/hmm.php on line 7"

i've no idea what the problem is ;(

me and regular expressions just don't get along

Last edited by DeadlySin3; 05-12-2003 at 11:51 PM.
 
Old 05-13-2003, 07:26 AM   #9
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Rep: Reputation: 0
Well I just tried it out myself and found the bug. It's not the reular expression (uff - I used regex a long time ago and still got it ). It's been in the line before. The string did not end. Change it to
Code:
print "<form action=\"update.php\">\n";
and it's gonna run. Here's my code:
PHP Code:
$db "database.inc";

$fd file("$db");
print 
"<form action=\"update.php\">\n";
foreach(
$fd as $link){
        
$strings = array();
        
eregi("mailto:(.*)\">(.*)</a>: (.*)\n"$link$strings);
        print 
"\t<br>\n\t<table>\n";
        print 
"\t\t<tr>\n";
        print 
"\t\t\t<td>eMail:</td>\n\t\t\t<td><input type=\"text\" name=\"eMail[]\" value=\"" $strings[1] . "\"></td>\n";
        print 
"\t\t</tr>\n";
        print 
"\t\t<tr>\n";
        print 
"\t\t\t<td>Name:</td>\n\t\t\t<td><input type=\"text\" name=\"Name[]\" value=\"" $strings[2] . "\"></td>\n";
        print 
"\t\t</tr>\n";
        print 
"\t\t<tr>\n";
        print 
"\t\t\t<td>Comment:</td>\n\t\t\t<td><textarea name=\"comment[]\">" $strings[3] . "</textarea></td>\n";
        print 
"\t\t</tr>\n";
        print 
"\t</table>\n";
}
print 
"\t<input type=\"submit\" value=\"Update stuff\">\n</form>\n"
Well, I just thought about that update-script and fastly typed one:
PHP Code:
$db "database.inc";
$fd fopen("$db""w+");
for(
$i=0$i count($eMail); $i++)
    
fwrite($fd"<a href=\"mailto:" $eMail[$i] . "\">" $Name[$i] . "</a>: " $comment[$i]);
fclose($fd);
header("Location: hmm.php"); 
Changing entries works fine, but I think you can do special stuff like deleting and appending entries by now.

So far, Gary
 
Old 05-13-2003, 07:27 AM   #10
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Rep: Reputation: 0
Sorry about that smiley-link up there, I bet you know why it's there
 
Old 05-13-2003, 11:14 AM   #11
DeadlySin3
LQ Newbie
 
Registered: May 2003
Location: Co
Distribution: Debian 3.0r2.Sarge
Posts: 25

Original Poster
Rep: Reputation: 15
ahhhh!

I think i've got the general idea now - I usually need to see a working example before I see what needs to be done.

I really appreciate your help!!

This smiley suits me so well :


heh
 
Old 05-13-2003, 01:15 PM   #12
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Rep: Reputation: 0
That's why you came here: to learn things
I think you need the possibilities to be shown so you know them. To select the needed method comes after. We're not the kind of coders saying "we always have a solution, but it never matches the problem

So have fun! Ehm: tell us when you've finished your stuff and I'm sure, we're gonna post some crap

So far, Gary

Last edited by cYbORg; 05-13-2003 at 01:18 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 12:28 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 08:15 PM
inserting the data thru php in a text file suchi_s Programming 5 02-02-2005 04:28 AM
php script can not write text file lemotion Linux - Newbie 5 04-20-2004 11:14 PM
PHP & MySQL getting info from text file neon Programming 1 10-15-2003 01:34 AM


All times are GMT -5. The time now is 09:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration