Hi all,
I’m a newbie in php. Does anyone could guide me in making a very simple php form that have insert, edit, delete features WITHOUT using any Mysql database. All the output will read from a textfile. Thank you.
Hi all,
I’m a newbie in php. Does anyone could guide me in making a very simple php form that have insert, edit, delete features WITHOUT using any Mysql database. All the output will read from a textfile. Thank you.
I found this article. I didnt read thru it 100% but it might point you in the right direction. It uses XML, which would be the best format for what you want to do.
[quote=Raydred;2336141]I found this article. I didnt read thru it 100% but it might point you in the right direction. It uses XML, which would be the best format for what you want to do.
http://xtech06.usefulinc.com/schedule/paper/19[/quote]
Thanks, Raydred. But I need even more simple way to do it by not using XML. Just simply txt file. Something like file_put_contents, str_replace()…that write data from a form into a txt file and display the data out of it.
I found a method is called flat file database. I’m now made the listing and create new page which running smooth but I’m stuck in delete and edit.php. Does anyone can take a look at my code below. Thanks.
$selName = $_GET[‘Name’];
$delAdd = $_GET[‘Add’];
$file = “reseller” . $delAdd . $selName .".txt";
$news = file($file);
$cnt = 0;
foreach ($news as $key => $line) {
if ($key!= $selName) { $result[] = $line; }
$cnt+1;
}
if ($key!= 0) {
$fh = fopen($file, “w+”);
foreach ($result as $joint) {
fwrite($fh, $joint);
}
fclose($fh);
}
elseif ($key == 0) {
$fh = fopen($file, “w+”);
fwrite($fh, “”);
fclose($fh);
}
What errors?
I understand that you want the .txt in the root where the file is located? Add.Name.txt
Also you could try adding it to another directory, meaning chmod to 0755
My problem is everytime i edit the form data, it wont overwrite the existing one but it will append to a new next line. Does anyone here can guide me tru this process? Thanks for many help. Here is my code so far:
[FONT=verdana][SIZE=2][COLOR=#000000]$any = “@,@”; [/COLOR][/SIZE][/FONT]
[SIZE=2][FONT=verdana][COLOR=#000000]$filename=“mydata.txt”; //sets file to edit [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]$handle = fopen($filename, ‘r+’); //File handle for $filename [/COLOR][/FONT][/SIZE]
[FONT=verdana][SIZE=2][COLOR=#000000]if(isset($_POST[‘Submit’])) { [/COLOR][/SIZE][/FONT]
[SIZE=2][FONT=verdana][COLOR=#000000]{ [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]while(1) { [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]$line = fgets($handle); //read line from file content [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]if($line == null)break; //if end of file reached then stop reading [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]anymore [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]$newcontents = trim(addslashes($_POST[‘Name’])).$any.addslashes($_POST[‘Address’]).$any.addslashes($_POST[‘Area’])."
"; [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]$str.= $line; //set file content to a string [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]} [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]} [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]$str.= $newcontents; //append new updated contents to file content [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]rewind($handle); //set pointer back to beginning. [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]ftruncate($handle, filesize($filename)); //delete everything in the file. [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]fwrite($handle, $str);//write everything back to file with the updated contents. [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]echo ‘<script>alert(“Reseller Updated !”);opener.location.reload();</script>’; [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]echo ‘<script>window.close();</script>’; exit(); [/COLOR][/FONT][/SIZE]
[SIZE=2][FONT=verdana][COLOR=#000000]fclose($handle); [/COLOR][/FONT][/SIZE][FONT=verdana][SIZE=2][COLOR=#000000]} [/COLOR][/SIZE][/FONT]
had to rewrite a lot to get it in an acceptable state. I didn’t test it but it should work…
$any = "@,@";
if(isset($_POST['Submit']))
{
$filename="mydata.txt"; //sets file to edit
$handle = fopen($filename, 'r+'); //File handle for $filename
$newcontent = trim( addslashes($_POST['Name'])) . $any.addslashes($_POST['Address']) . $any.addslashes($_POST['Area']) . "
";
$current_content = '';
while( !feof( $handle ) )
{
$line = fgets( $handle );
// if eof ( end of file then the last line shouldn't be written back!
if( !feof( $handle ) )
{
$current_content .= $line; //set file content to a string
}
}
fclose( $handle );
$current_content .= $newcontent; //append new updated contents to file content
$handle = fopen($filename, 'w+'); //File handle for $filename writing
fwrite($handle, $current_content); //write everything back to file with the updated contents.
fclose($handle);
echo '<script>alert("Reseller Updated !"); opener.location.reload(); </script>';
echo '<script>window.close();</script>';
}
Oh, I’m sorry Borrob…i do a complete test run now, and found out the previous problem is still exsist tat the data is append to a new entry instead of editing the existing one. So after i edit the form content and click ‘submit’, it will append a new line. May i know what causing this? thanks for look again for me.
The reason it is still appending the last line is because you have an additional “
” in your data file.
So when it takes the last line away it takes an empty line away.
You can change this by not appending the “
” to the new content…
Or if the last line is empty take the line before that away.
[quote=borrob;2340024]The reason it is still appending the last line is because you have an additional “
” in your data file.
So when it takes the last line away it takes an empty line away.
You can change this by not appending the “
” to the new content…
Or if the last line is empty take the line before that away.[/quote]
Thanks, borrob! It works perfectly. All the data is editable and no extra line append in database. Thank you once again for helping me out
:: Copyright KIRUPA 2024 //--