Jump to content

selectiong data


user4fun

Recommended Posts

$website = $_GET['website'];$file_handle = fopen("co_file_in/'$website'.txt", "r");while (!feof($file_handle)) { $string = fgets($file_handle, 30);echo $string; }fclose($file_handle);

for some reason that is not working, i think the problem is displaying the $website wth the fopen statement,also, can i pick a certain line from the txt file, say mabe line 10thank you

Link to comment
Share on other sites

fopen("co_file_in/'$website'.txt", "r");Do you want single quotes in the filename? Because they are around the variable.If you want to look at the 10th line, you need to use explode to break up the file by lines (explode on the \n character), and then go to the 10th element in the array, that will be the 10th line.

Link to comment
Share on other sites

Why not just use file() instead of fopen? it automatically puts the contents of a file into an array(with each item of the array being 1 line in the file).

Link to comment
Share on other sites

Using file() :

$website = $_GET['website'];$line = file("co_file_in/$website.txt"); //Stores an array of the file's linesecho $line[9]; //Echoes the tenth line (but with line 1 = subscript 0)

Using explode() :

$website = $_GET['website'];$filename = "co_file_in/$website.txt";$file_handle = fopen($filename, "r");$string = fread($file_handle, filesize($filename)); //Gets the entire file in one variable$line = explode("\n", $string); //Splits the string into an array, each value has one lineecho $line[9]; //Echoes the tenth line (but with line 1 = subscript 0)fclose($file_handle);

The file() method is better, as it is obviously more efficient :)

Link to comment
Share on other sites

Synook took the words right out of my mouth. Although i must say if you are going to use the fopen method, you'd be better off using preg_split as different OS-es use different new line characters. Some use \n, others use \r\n, and even more use \r. So you can't just assume that \n is the answer(unless you have specifically used \n in every portion of your code then nevermind this small rant).

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...