Jump to content

Add word to .txtfile after buttonclick


graat90
Go to solution Solved by graat90,

Recommended Posts

Hi there,

 

I've searched around for the same problem that i'm dealing with, but i couldn't find the exact answer yet.

 

What i want, is that if an user clicks on a button, the word in front of it has to be added at a new line (vulwoorden.txt).

 

I think i'm almost there, but i'm not able to attach the button to the generated Twitter word.

 

The meaning of this all is, that an admin is able to make his own word filter.

So if he clicks the verberg item button (hide word button), the application should not only hide this word from the html side, but also add this word to the textfile.

 

The application now opens the existing textfile and already filters out the given words. 

But i want the user to be able, to add new words to this textfile after button click!

 

This is the code so far:

	public function DisplayMostUsedWords(){
		echo "<h3>Most used words</h3>";
		asort($this->word_occurence);
		$this->word_occurence = array_reverse($this->word_occurence);
		$i=1;
		
		// filter out commonly used words
		$handle = fopen("./includes/vulwoorden.txt", "r");
		while (!feof($handle)) {
			// Get the current line that the file is reading
			$current_line = trim(fgets($handle));
			unset($this->word_occurence[$current_line]);
		}   
		fclose($handle);
		
		foreach($this->word_occurence as $word => $occurence){
			echo "<ul class=\"list-group\">
					<li class=\"list-group-item\">
						<table id=\"most-used-words\">
	                    	<thead>
	                        	<tr>
									<td>$occurence - $word</td>
									<td><a href=\"#\" style=\"float:right;\" class=\"btn-danger btn-sm\">Verberg term</a></td>
								</tr>
							</thead>
					  </table>
					</li>
				</ul>";
			$i++;
			if($i >= 10){
				break;
			}
		}

This what it's look like : post-167077-0-23461000-1389363444_thumb.png

 

I hope i explained it clear! 

 

Thanks in advance :)

Link to comment
Share on other sites

I was thinking to do something like this, but it's not the best way i think:

foreach($this->word_occurence as $word => $occurence){
			echo "<ul class=\"list-group\">
					<li class=\"list-group-item\">
						<table id=\"most-used-words\">
	                    	<thead>
	                        	<tr>
						<td>$occurence - $word</td>
						<td>"; if (isset($_POST['<a href=\"#\" style=\"float:right;\" class=\"btn-danger btn-sm\">Verberg term</a>'])) { file_put_contents('./includes/vulwoorden.txt', $word , FILE_APPEND | LOCK_EX); }"</td>
								</tr>
							</thead>
					  </table>
					</li>
				</ul>";
			$i++;
			if($i >= 10){
				break;
			}
		}
Link to comment
Share on other sites

  • Solution

This topic can be closed. I solved the problem. 

		foreach($this->word_occurence as $word => $occurence){
			$fn = "./includes/vulwoorden.txt";
			$file = fopen($fn, "a+");
			$size = filesize($fn);
			
			if(isset($_POST['toevoegen'])) fwrite($file, $_POST['toevoegen']);
			fclose($file);
			?>
				<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
					<br />
						<table>
							<tr>
							<td><?=$occurence?></td>
							<td style="padding-left: 11px;"><input style="background-color: transparent; border: none;" class="field left" readonly type="text" name="toevoegen" value="<?=$word?>"/></td>
							<td><input class="btn-danger btn-sm" type="submit" value="Verberg term"/></td>
							</tr>
						<table>
				</form>
			<?
			$i++;
			if($i >= 10){
				break;
			}
		}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.