Jump to content


Flat File Help.


3 replies to this topic

#1 Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 01 December 2008 - 10:29 PM

So I'm trying to utilize a flat-file database structure for this one project I'm working on, and i just don't know how to go about it, as I've never really done a flat-file db style script before.

Essentially all it is, is a file like so:
[SERVER]
host=localhost;
port=8130;
[IMAGE]
debugmode=false;
onlinetext=Test;
onlinetextloc=1,2,3;
offlinetext=Me;
offlinetextloc=4,5,6;
curr_listcount=0;
max_listcount=32;
....and so on....

Whats getting at me is I don't even know how to even get it do do what i want. Which is to get the stuff between [SERVER] and [IMAGE] as well as [IMAGE] and END-OF-FILE.

So far my code is, like this:
<?php
	$cfg_file = "../configs/ah.cfg";
	$handle = fopen($cfg_file, "rb");
	$page = '';
	while (!feof($handle)) {
  		$page .= fread($handle, 8192);
	}
	fclose($handle);
		$gathered_info = explode(";",$page);

		for($n=0; $n <= sizeof($gathered_info); $n++){
			$server_db[] = explode("=",$gathered_info[$n]);
		}
	
		print("<pre>");
		print_r($server_db);
		print("</pre>");
?>

Nothing fancy.. or anything. In fact all it does is spit out information in an array, of the whole file, which is where my starting point is.

My array that it spouts is:
Array
(
    [0] => Array
        (
            [0] => [SERVER]
host
            [1] => localhost
        )

    [1] => Array
        (
            [0] => 
port
            [1] => 8130
        )

    [2] => Array
        (
            [0] => 
[IMAGE]
debugmode
            [1] => false
        )

    [3] => Array
        (
            [0] => 
onlinetext
            [1] => Test
        )

    [4] => Array
        (
            [0] => 
onlinetextloc
            [1] => 1,2,3
        )

    [5] => Array
        (
            [0] => 
offlinetext
            [1] => Me
        )

    [6] => Array
        (
            [0] => 
offlinetextloc
            [1] => 4,5,6
        )

    [7] => Array
        (
            [0] => 
curr_listcount
            [1] => 0
        )

    [8] => Array
        (
            [0] => 
max_listcount
            [1] => 32
        )

    [9] => Array
        (
            [0] => 
        )

    [10] => Array
        (
            [0] => 
        )

)

I would use a mysql database, but unfortunately i would like to keep it from using a mysql server for the moment. Now at the moment all I'm doing is reading the file, but, i plan to write to it as well at a later date.

I dont need someone to fully solve this for me.. But maybe just a few solid working examples of what im trying to accomplish—ie: the separation between [server] and [image].

And yes, i did search the forum, and the tutorial database, but didnt find anything to what i want to achieve. like one that serializes and then unserializes. But then i could have missed one and I'd never know.

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 02 December 2008 - 12:28 PM

No need to make your own function, php has already done that for you:
parse_ini_file()

There is also an interesting "INI" class in the comments that reads/writes ini files, though you can probably come up with something simpler :)

#3 Jynxis

    Young Padawan

  • Members
  • Pip
  • 132 posts
  • Location:The Shadows

Posted 02 December 2008 - 04:43 PM

Thanks.

Edited by Jynxis, 02 December 2008 - 06:55 PM.


#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 02 December 2008 - 06:56 PM

View PostJynxis, on Dec 2 2008, 02:43 PM, said:

How would i be able to display information between, lets say "[SECTION 2]" and the "END-OF-FILE".
What do you mean? Strictly dump that section of the file to the browser, or display it in a parsed/formatted manner?

Make sure you have a format in mind before you make any kind of parser though. Sometimes little changes can create big headaches.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users