Jump to content

[RESOLVED]read text file contents and explode


cinek

Recommended Posts

I need to save some data in a file seperated by e.g. ";". I want to then read the file, and use the explode method.should I read the file & save the contents into an array or just read the file and use the explode method?

$file = "config.txt";	$lines = array();	  $contents = fopen($file, "r");		while(!feof($contents))	{		$lines[] = fgets($contents, 4096);	}	fclose($contents);

Basically what I'm trying to do, is save some info in the file(manually): 1;A;B;C;D (this format) and then I need to display that info on the site - the number will correspond to which place it'll be displayed in (1 == 1st page at the top, 100/100 = last page at the bottom etc). The number should also correspond to a folder name, so for no 1 I'll have a folder named "1" etc... - how can I do this?I also need to use pagination. So that it automatically shows how many pages there'll be. I want to display 3 things (from the txt file) on the site, the rest should be on the next page etc

Link to comment
Share on other sites

I'm getting this error: Warning: explode() expects parameter 2 to be string, resource given...... on line 32this is the code:

$file = "config.txt";	  $contents = fopen($file, "r");		$explode = explode(";", $contents); // <-- LINE 32	print_r ($explode);		fclose($contents);

I tried this - but the array is not right.....this is what the output is:

Array( [0] => 1 [1] => a [2] => b [3] => c2 [4] => c [5] => d [6] => e3 [7] => e [8] => f [9] => g)1
this is the code
$file = "config.txt";	  $contents = file_get_contents($file, "r");		$explode = explode(";", $contents);	echo "<pre>";	print_r ($explode);	echo "</pre>";

and the file contents are:

1;a;b;c2;c;d;e3;e;f;g
Link to comment
Share on other sites

If you need pagination and other such features, it's probably a good idea to start learning about SQL, and MySQL in particular.Having said that, the output from your file is correct, and seems to fit your description... what kind of output do you want anyway? Want to have an array with each line being an array of all ";" separated elements? OK... the best way is to use file(), like:

$file = "config.txt";$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);$contents = array();foreach($lines as $line) {	$contents[] = explode(';', $line);}echo "<pre>";print_r ($contents);echo "</pre>";

Link to comment
Share on other sites

I need to read the array as it contains data that'll need to go onto the siteso lets say this is the file format: <no>;<name>;<image>1;cinek;someImg.jpg2;w3c;w3cImg.jpgthe <name> should be displayed on the site, with the image etc. The image is stored in a folder with the <name> fields' name so for no 1 the folder name(which contains the image) is cinek for no 2 the folder name is w3c etc. The folders are created manually.... How can I access the output data (from the file) and get it to show up the correct data in the site?btw I will not be using sql for pagination at all. I'm getting the rows in the file. (already got that :) )

Link to comment
Share on other sites

Like boen pointed out, you can use the file function to get an array of lines from the file, and then loop through those to explode each line and do whatever you want with the data. Since file returns an array of lines, I assume you can use the count function with that to know how many "pages" you need.

Link to comment
Share on other sites

@boen_robot thanks, that's the array I'm looking for. @justsomeguythanks, but I already got the pagination sorted (I think). It's displaying the number of pages fine, so now I think I need to get the data from the file to display on the page and link both.How would I do that with the file function? I presume I'd need each (let's call it) field assigned to a variable?

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...