LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Text File DB -> PHP (https://www.linuxquestions.org/questions/programming-9/text-file-db-php-59396/)

DeadlySin3 05-12-2003 11:00 AM

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

cYbORg 05-12-2003 03:12 PM

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

DeadlySin3 05-12-2003 08:48 PM

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!

Tinkster 05-12-2003 08:59 PM

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 :)

DeadlySin3 05-12-2003 09:01 PM

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 ;)

cYbORg 05-12-2003 09:43 PM

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

cYbORg 05-12-2003 09:46 PM

Oups - there should be a colon followed by an opening bracket instead of that smiley up there in the ereg()-funtion :)

DeadlySin3 05-12-2003 10:47 PM

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

cYbORg 05-13-2003 06:26 AM

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

cYbORg 05-13-2003 06:27 AM

Sorry about that smiley-link up there, I bet you know why it's there :)

DeadlySin3 05-13-2003 10:14 AM

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 :
:newbie:

heh

cYbORg 05-13-2003 12:15 PM

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


All times are GMT -5. The time now is 05:46 AM.