Jump to content

Fread With Text Dividers


walapu

Recommended Posts

Hello so I have a txt file filled with data like so

If you try and take a cat apart to see how it works, the first  thing you have in your hands is a non-working cat. -- Douglas Adams%%O'Toole's Corollary of Finagle's Law: The perversity of the  Universe tends towards a maximum.%%Hofstadter's Law: It always takes longer than you expect, even  when you take into account Hofstadter's Law. %%

I need to be able to read the lines between the %% signs and have it so when more lines are added I do not need to re-edit the code again. So for instance an example of why this is needed, is so I can either reject or accept a line. When it is rejected it is deleted from the file. When it is accepted it is deleted from the file and added to another. You get my drift? Sorry if I am kinda hard to understand.

Link to comment
Share on other sites

fread is old fashioned. Try it this way instead:$file = $something;$data_string = file_get_contents($file);$data_array = explode ("%%", $data_string);$data_array now contains an array of all your entries, not including the "%%" markers. So $data_array[0] would contain the Douglas Adams quote, and so on. If you want to remove an item from the array, use array_splice(). To turn your array back into a string for writing, use implode().

Link to comment
Share on other sites

Thanks for your help. One thing, could you give me an example on how to write the string to another file and to delete it from the original? It needs to be so if I have 15 quotes it will just expand with the more I add. Basically what I am going for is an option with each quote either to accept it, or reject it.

Link to comment
Share on other sites

K but one thing I dont understand is how would I use file_put_contents for each array? Even when my file grows?for instance I cant do this

$data_string = file_get_contents(waiting_quotes.txt);$data_array = explode ("%%", $data_string);if (isset($_POST['accept_button0']){file_put_contents(quotes.txt, $data_array[0] . "\n%%\n");}if (isset($_POST['accept_button1']){file_put_contents(quotes.txt, $data_array[1] . "\n%%\n");}if (isset($_POST['accept_button2']){file_put_contents(quotes.txt, $data_array[2] . "\n%%\n");}if (isset($_POST['accept_button3']){file_put_contents(quotes.txt, $data_array[3] . "\n%%\n");}if (isset($_POST['accept_button4']){file_put_contents(quotes.txt, $data_array[4] . "\n%%\n");}if (isset($_POST['accept_button5']){file_put_contents(quotes.txt, $data_array[5] . "\n%%\n");}

because I would have to edit my code each time the file has more than 6 quotesalso how would I delete a array including the %% from the waiting_quotes.txt? I cant use array_splice() because that is just in the array not the file :)

Link to comment
Share on other sites

Learning about some functions is okay, but what you really need is a new approach to this whole project. Ideally, you'd use a database, but I don't want your head to explode. So we'll stick with files.The idea here is to let PHP do most of the work for us. That's why I wanted to read the file items into an array. That way, we can loop through the array and create exactly as many form elements as we need. We use the same process for adding quotes from the waiting file to the master file. All we need to do is keep track of the index.Anyway, give this a try. Write back if something doesn't work or you don't understand something. I really want you to study this so you'll learn something.

<?php	$source = 'waiting_quotes.txt';	$destination = 'quotes.txt';	/*		If the user has submitted the form with 1+ quotes checked,		grab each quote by its index number and append it to $str	*/	if (isset($_POST['qq'][0]) ) {		$str = '';		$data = file_get_contents($source);		if ($data) {			$quotes = explode('%%', $data);			$len = count($quotes);			for ($i = 0; $i < $len; ++$i) {				if (in_array((string)$i, $_POST['qq'])) {					$quotes[$i] = trim($quotes[$i]);					$str .= "\n%%\n" . $quotes[$i];				}			}		}		// append $str to the destination file		$F = fopen($destination, 'a');		if ($F) {			fwrite($F, $str);			fclose($F);			file_put_contents($source, '');			// Add a location header here to redirect the browser ???		} else {			echo 'Could not open ' . $destination;			exit;		}	} // else print the form?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"   "http://www.w3.org/TR/html4/strict.dtd"><html>	<head>		<meta http-equiv="content-type" content="text/html;charset=UTF-8">		<title></title>		<style type="text/css">			/*				We'll put the quotes and checkboxes in a table. Each				row contains 1 quote and 1 checkbox. Odd numbered quotes				have a blue background. Evens are pink.			*/			table {				border: solid 1px #000;				border-collapse: collapse;				margin-bottom: 10px;			}			tr {				border-bottom: solid 1px #000;			}			td {				padding: 10px; 			}			td.quote {				width: 400px;			}			td.check {				vertical-align: middle;			}			td.blue {				background-color: #ddf;			}			td.pink {				background-color: #fdd;			}		</style>	</head>	<body>		<form action="" method="post">			<table><?php	function is_odd($n) {		return $n & 1; // 0 = even, 1 = odd	}	$str = "There are no new quotes to evaluate.";	$data = file_get_contents($source);	if ($data) {		$str = '';		$quotes = explode('%%', $data);		$len = count($quotes);		for ($i = 0; $i < $len; ++$i) {			$bg = is_odd($i) ? 'pink' : 'blue';			$quotes[$i] = trim($quotes[$i]);			/*				Build the table rows. The checkboxes are named qq[] so that				PHP will create an array called qq that contains an index value				for each box that's checked. For example: the user checks boxes				0 and 2 (first and third boxes). PHP will find the following:				$_POST['qq'][0] returns 0				$_POST['qq'][1] returns 2			*/			$str .= "<tr><td class='quote $bg'>{$quotes[$i]}</td><td class='check'>";			$str .= "<input type='checkbox' name='qq[]' value='$i'> add</td><tr>\n";		}	}	echo $str;?>			</table>					<?php if ($data) echo '<input type="submit" name="submit" value="submit">'; ?>		</form>	</body></html>

Link to comment
Share on other sites

I dont understand how it removes the quote from the source file. Could you explain it?
It doesn't. It could, but I didn't think that was what you wanted. Here's what it does do:When you check some boxes and hit the submit button, the form sends some numbers back to the script. Those numbers correspond to the position in the series of quotes. So 0 means the first quote, 1 means the second, and so on.Then we open the source file and split it into an array. Each array element holds one quote. The array indexes match the original sequence of quotes, so the numbers we sent back with the form also correspond to the array indexes.Now we loop through the entire array, beginning with this line: for ($i = 0; $i < $len; ++$i) . If an array index matches one of the numbers that the form sent back, we add the quote (and "\n%%\n") to a string. We do that until we've added all the quotes that were selected. If a quote was NOT selected, we just skip it. It does not get added to the string. When the loop is finished, we open the destination file and append the string. Then we delete the source file. (That last part can easily be changed.)Oh, man! I meant to show you exactly what the file structure is that the script is expecting. Both files should look like this:
aaaaaa aaaaa aaaaaaaaaa aaa a aaaaaaa aaaaa aaaaaa aaaaa aaaaaaaaaa aaa a aaaaaaa aaaaa aaaaaa aaaaa aaaaaaaaaa aaa a aaaaaaa aaaaa aaaaaa aaaaa aaaaaaaaaa aaa a aaaaaaa aaaaa aaaaaa aaaaa aaaaaaaaaa aaa a aaaaaaa aaaaa%%bbbbb bb bbbbbbb bbb bbbbb b b bbbbbbbbbbb bbbbb bb bbbbbbb bbb bbbbb b b bbbbbbbbbbb bbbbb bb bbbbbbb bbb bbbbb b b bbbbbbbbbbb%%ccccccc cc ccccccc ccccc cccccc cccccc ccccccc cc ccccccc ccccc cccccc cccccc ccccccc cc ccccccc ccccc cccccc cccccc ccccccc cc ccccccc ccccc cccccc cccccc ccccccc cc ccccccc ccccc cccccc cccccc ccccccc cc ccccccc ccccc cccccc cccccc ccccccc cc ccccccc ccccc cccccc cccccc

That means its important for the files NOT to start with the "%%\n" sequence. If that's a hassle, I could revise the code, though. Or you could. :)

Link to comment
Share on other sites

1.When I add the quote to the destination I would like it to be removed from the source. Also I would like to add a decline button that just removes the quote from the source file.2.In the code I have 2 different names for the check boxes. I would like to have the same names but 2 different values. So that way I can use the radio button.Thanks so much for all your help :)I have recently started php so sorry for all the questions lolThis is what I have so far

<?php	$source = 'waiting_quotes.txt';	$destination = '../quotes.txt';	//Accept Button	//If the user has submitted the form with 1+ quotes checked,	//grab each quote by its index number and append it to $str	if (isset($_POST['accept'][0]) ) {		$str = '';		$data = file_get_contents($source);		if ($data) {			$quotes = explode('%%', $data);			$len = count($quotes);			for ($i = 0; $i < $len; ++$i) {				if (in_array((string)$i, $_POST['accept'])) {					$quotes[$i] = trim($quotes[$i]);					$str .= $quotes[$i] . "\n%%\n";				}			}		}		// append $str to the destination file		$F = fopen($destination, 'a');		if ($F) {			fwrite($F, $str);			fclose($F);			file_put_contents($source, '');			// Add a location header here to redirect the browser ???		} else {			echo 'Could not open ' . $destination;			exit;		}	} // else print the form//Decline Button	if (isset($_POST['decline'][0]) ) {		$data = file_get_contents($source);		if ($data) {			$len = count($quotes);			for ($i = 0; $i < $len; ++$i) {				if (in_array((string)$i, $_POST['decline'])) {				   //Needs Work				}			}	}	} // else print the form?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">	<head>		<meta http-equiv="content-type" content="text/html;charset=UTF-8">		<title>Quote Evaluation Form</title>		<style type="text/css">			/*				We'll put the quotes and checkboxes in a table. Each				row contains 1 quote and 2 checkbox's. Odd numbered quotes				have a blue background. Evens are pink.			*/			table {				border: solid 1px #000;				border-collapse: collapse;				margin-bottom: 10px;			}			tr {				border-bottom: solid 1px #000;			}			td {				padding: 10px;			}			td.quote {				width: 400px;			}			td.check {				vertical-align: middle;			}			td.blue {				background-color: #ddf;			}			td.pink {				background-color: #fdd;			}		</style>	</head>	<body>	<form action="" method="post">	 <table><?php	function is_odd($n) {		return $n & 1; // 0 = even, 1 = odd	}	$str = "There are no new quotes to evaluate.";	$data = file_get_contents($source);	if ($data) {		$str = '';		$quotes = explode('%%', $data);		$len = count($quotes);		for ($i = 0; $i < $len; ++$i) {			$bg = is_odd($i) ? 'pink' : 'blue';			$quotes[$i] = trim($quotes[$i]);			/*				Build the table rows. The checkboxes are named qq[] so that				PHP will create an array called qq that contains an index value				for each box that's checked. For example: the user checks boxes				0 and 2 (first and third boxes). PHP will find the following:				$_POST['qq'][0] returns 0				$_POST['qq'][1] returns 2			*/			$str .= "<tr><td class='quote $bg'>{$quotes[$i]}</td><td class='check'>";			$str .= "<input type='checkbox' name='accept[]' value='$i'> Accept";			$str .= "<input type='checkbox' name='decline[]' value='$i'> Decline</td><tr>\n";		}	}	echo $str;?>				 </table>			 <?php if ($data) echo '<input type="submit" name="submit" value="Submit">'; ?>	</form>	</body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...