Fsoft Posted May 7, 2008 Share Posted May 7, 2008 Hi, it's my first thread to ask help in php Hope I will get it Well, I need help. What I want is here I will write a simple code with one text field. The field would be named as "name"; And it's function would be take the name of each boy/girl who ever enters and then that names would be written in a simple txt file on the same server! BUT to recognize them, It will write each name on each line in the txt file so it would be easy to read or do other tasks, <?php //Faisal Shah's Testing script $db = "namedb.txt"; $name = $_POST["name"]; $form = "<form method=\"post\" action=\"name.php\"> Name here <input name=\"name\" type=\"text\" size=\"20\"> <br> <input name=\"submit\" type=\"submit\" value=\"enter_name\"> </form>"; $message_a_entre = "Hi, My name is $name"; if($_POST["submit"] == enter_name) { $f_handle = fopen($db, 'a'); fwrite($f_handle,$message_a_entre."\n"); fclose($f_handle); } else { echo $form; } ?> Well, I haven't tested the code, But it seems to be ok, And it gives you an idea, that what I am doing at this time! Now This will write the text/content of text field "name" into the db file that is namedb.txt Now If I made a read.php to read this all content. I would make a simple file saying ; <?php //read.php by Faisal Shah @include("namedb.txt"); ?> I know it's simple And If i do it, It will show me entire entries/name entered in the text file Like this Stephan Bill Gates Faisal Jimmy Sonu Oxyhost etc etc.. BUT I want that It give me like this ; Stephan (delete) Bill Gates (delete) Faisal (delete) Jimmy (delete) Sonu (delete) oxyhost (delete) So it must give a small text saying delete at the front of each entry. So If I want to delete a name/entry I just click the delete at the front of entry and it explode the entire line/entry! Well, I am searching for this answer form last 2 months BUT I hope that here some one would able to help me.. BUT BUT BUT please in simple way, SIMPLE code so i can understand Thanks in Advance.. LOOKING forward come on guys! Yours Faithfully, FAISAL SHAH! Link to comment https://forums.phpfreaks.com/topic/104643-php-file-content-deleting-help/ Share on other sites More sharing options...
Caesar Posted May 8, 2008 Share Posted May 8, 2008 Not tested (And not the approach I would personally take) but... <?php if(isset($_GET['action']) && $_GET['action'] == 'delete') { $handle = fopen('names.txt', "w"); $new = str_replace($_GET['name'], "", file_get_contents('names.txt')); fwrite($handle, $new); fclose($handle); } else { $names = explode("\n", file_get_contents('names.txt')); echo'<table>'; foreach($names as $name) { echo'<tr><td><a href="index.php?action=delete&name=$name" target="_parent">$name</a></td></tr>'; } echo'</table>'; } ?> Link to comment https://forums.phpfreaks.com/topic/104643-php-file-content-deleting-help/#findComment-535645 Share on other sites More sharing options...
Fadion Posted May 8, 2008 Share Posted May 8, 2008 I went into this just to experiment with the arrays, so my approach is way longer than that of Caesar's. <?php $db = 'db.txt'; if(isset($_GET['delete'])){ $content = file($db); foreach($content as &$val){ if(trim(str_replace("\r\n", '', $val)) == $_GET['delete']){ $content = array_diff($content, array($val)); } } $content = implode("", $content); $handle = fopen($db, 'w'); fwrite($handle, $content); fclose($handle); } $content = file($db); foreach($content as $val){ echo $val . " <a href='index.php?delete=" . $val . "'>Delete</a><br />"; } ?> Tested and works actually, but i wont go and make such a file database, instead id go for a real database. U have no indexing here, no autoincrement, no statements to easy operate, practically u build some slow file handling functions to emulate a database. Link to comment https://forums.phpfreaks.com/topic/104643-php-file-content-deleting-help/#findComment-535647 Share on other sites More sharing options...
Fsoft Posted May 10, 2008 Author Share Posted May 10, 2008 Thanks dear GuiltyGear! YOUR code worked and did the job for me BUT IF you dont mind. I want to understand the code and learn BUT I am unable to do so Can you please tell me some details about this code? In the code the area that is green is I understood not the other one thanks a lot <?php $db = 'namedb.txt'; if(isset($_GET['delete'])){ $content = file($db); foreach($content as &$val){ if(trim(str_replace("\r\n", '', $val)) == $_GET['delete']){ $content = array_diff($content, array($val)); } } $content = implode("", $content); $handle = fopen($db, 'w'); fwrite($handle, $content); fclose($handle); } $content = file($db); foreach($content as $val){ echo $val . " <a href='$_SERVER[script_name]?delete=" . $val . "'>Delete</a><br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/104643-php-file-content-deleting-help/#findComment-537652 Share on other sites More sharing options...
DarkWater Posted May 10, 2008 Share Posted May 10, 2008 Don't use a flatfile system, use a relational database like MySQL. It's faster and MUCH easier to work with. >_> Link to comment https://forums.phpfreaks.com/topic/104643-php-file-content-deleting-help/#findComment-537655 Share on other sites More sharing options...
Fadion Posted May 10, 2008 Share Posted May 10, 2008 Yeah as said, u should use a database, but if u want to stick with this method im explaining the code shortly: <?php $db = 'namedb.txt'; //the text file path if(isset($_GET['delete'])){ //if a delete link is clicked $content = file($db); //return each line of the text in an array element foreach($content as &$val){ //loop through the array if(trim(str_replace("\r\n", '', $val)) == $_GET['delete']){ //see if the text line is equal to the 'delete' url variable. I trimmed to remove left and right spaces and used str_replace() to remove line breaks $content = array_diff($content, array($val)); //delete that element from the array } } $content = implode("", $content); //implode all array elements to a string $handle = fopen($db, 'w'); //open the file for writing. file_put_contents() can be used instead of fopen() fwrite() and fclose() if ure in php5, but i prefer using fopen() fwrite($handle, $content); //write the contents to the file fclose($handle); //close the file connection } $content = file($db); //return each line of the text in an array element foreach($content as $val){ //loop through the array echo $val . " <a href='$_SERVER[script_name]?delete=" . $val . "'>Delete[/url]"; //show the line vaules with the delete link } ?> Link to comment https://forums.phpfreaks.com/topic/104643-php-file-content-deleting-help/#findComment-537698 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.