Forum Moderators: coopster

Message Too Old, No Replies

Flatfile comments script?

         

leeuk

12:04 pm on Sep 14, 2007 (gmt 0)

10+ Year Member



Hello there.

I do alot of work from other computers, and find myself constantly needing to jot things down, which i later access at home.

I'm in need of a script that processes a form like so:

Name: ________
Comment: _______

Submit.

I have no problem insterting the data. The problem arises when i want to delete a specific entry.

Could anyone assist me with a way of doing so?

Thanks in advance, Lee.

vincevincevince

12:23 pm on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assume you read the data into one variable?

$data=file_get_contents("notes.txt");

Next, explode it into lines:

$lines=explode("\n",$data);

Looping through for output:

foreach ($lines as $lineno => $text)
{
print "<a href=\"javascript://\" onClick=\"if (confirm('Delete me?')) top.location='$_SERVER[PHP_SELF]?delete=$lineno'\">$text</a><br>";
}

Now, just add a clause:


if ($_GET[delete])
{
foreach ($lines as $lineno => $text)
{
if ($lineno!=intval($_GET[delete]) $newdata[]=$text;
}
file_put_contents("notes.txt",implode("\n",$newdata);
}

If you put your deletion clause after your file_get_contents but before your display loop, you can add the line:

$data=$newdata;

Otherwise, after deleting you need to reload $data...

leeuk

3:01 pm on Sep 14, 2007 (gmt 0)

10+ Year Member



Doesn't seem to work =\

I have the following:

<?
$data=file_get_contents("notes.txt");
$lines=explode("\n",$data);

foreach ($lines as $lineno => $text)
{
print "<a href=\"javascript://\" onClick=\"if (confirm('Delete me?'))

top.location='$_SERVER[PHP_SELF]?delete=$lineno'\">$text</a><br>";
}

if ($_GET[delete])
{
foreach ($lines as $lineno => $text)
{
if ($lineno!=intval($_GET[delete]) $newdata[]=$text;
}
file_put_contents("notes.txt",implode("\n",$newdata);
}
?>

Thanks for the reply.

vincevincevince

3:05 pm on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (confirm('Delete me?'))

top.location='$_SERVER[

You'll want to remove the newlines in that for a start....

if (confirm('Delete me?')) top.location='$_SERVER[

Also, I missed a couple of brackets... not sure how!


foreach ($lines as $lineno => $text)
{
if ($lineno!=intval($_GET[delete]) $newdata[]=$text;
}
file_put_contents("notes.txt",implode("\n",$newdata);

Should be:

foreach ($lines as $lineno => $text)
{
if ($lineno!=intval($_GET[delete])) $newdata[]=$text;
}
file_put_contents("notes.txt",implode("\n",$newdata));

leeuk

3:20 pm on Sep 14, 2007 (gmt 0)

10+ Year Member



Works like a charm! Thanks =]

eelixduppy

3:30 pm on Sep 14, 2007 (gmt 0)



Glad you found your solution, leeuk. Welcome to WebmasterWorld :)

leeuk

3:37 pm on Sep 14, 2007 (gmt 0)

10+ Year Member



I'm new to php. I find it next to impossible to write php code, but i can understand it fairly easily.

Anyone else have / had this problem?

Thanks for the welcome =]

eelixduppy

3:43 pm on Sep 14, 2007 (gmt 0)



>> Anyone else have / had this problem?

Most people with general programming knowledge should be able to follow the code for the most part. Writing is different because you have to understand the syntax and functions used within that specific type of language. If you ever need help interpreting something then [php.net...] is your best tool.