Forum Moderators: coopster

Message Too Old, No Replies

Flat File Database

         

miopod

6:29 pm on Mar 27, 2009 (gmt 0)

10+ Year Member



Hi Guys, i hope someone can help me (i'm not a programmer and have searched everywhere but cannot seem to find quick fix. I'm trying to set up a simple text database as follows
Name Category Ext Price

Test1.com¦Real Estate¦.com¦100
Test2.net¦Real Estate¦.net¦200
travel1.org¦Travel¦.org¦200
Car2.com¦Cars¦.com¦500

I want to be able to display only let's say the real estate category. Below is the code i'm using, any help is greatly appreciated

</head>

<body>

<?php
echo '<h1>Flat File Database Sorted By <span class="sortby">'.$header.'</span></h1>

<p>To sort the columns click on the headers.</p>

<p>View the <a href="flat-file-data2.txt">database</a> or the <a href="flat-file-database-demo2.txt">PHP code</a> used.
<br />&nbsp;</p>

<table summary="List of demo fields">
<tr>
<th><a href="?">Domain</a></th>
<th><a href="?category">Category</a></th>
<th><a href="?extention">Extention</a></th>
<th><a href="?price">Price</a></th>
</tr>';

$fp = fopen('flat-file-data2.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}

while (!feof($fp)) {
$line = fgets($fp,1024); //use 2048 if very long lines
$row++;
list ($domain, $Category, $Extention, $Price) = split ('\¦', $line);
if ($sortby == 'domain') $sortkey = strtolower($domain);
if ($sortby == 'category') $sortkey = strtolower($Category);
if ($sortby == 'extention') $sortkey = strtolower($Extention);
if ($sortby == 'price') $sortkey = $Price;
$col[$row] = array($sortkey, $domain, $Category, $Extention, $Price, $car, $colour, $age);
}

fclose($fp);

sort($col);
reset ($col);

$arrays = count($col) - 1;

$loop = -1;
while ($loop < $arrays) {
$loop++;
echo '
<tr>
<td>'.$col[$loop][1].'</td>
<td>'.$col[$loop][2].'</td>
<td>'.$col[$loop][3].'</td>
<td>'.$col[$loop][4].'</td>
</tr>';
}

echo '
</table>

<p><small>1 December 2004 &middot; Last updated: '.date('j F Y', getlastmod()); ?> &middot; A <a href="http://www.example.com">Example</a> production &middot; <a href="http://www.example.com/archives/04/12/FlatFileDatabaseDemo2.php">About The Demo</a> &middot; <a href="http://www.example.com/archives/04/12/FlatFileDatabaseDemo2.php#comments">Comments</a></small></p>

</body>
</html>

[edited by: eelixduppy at 6:16 pm (utc) on Mar. 28, 2009]
[edit reason] exemplified [/edit]

dreamcatcher

8:37 am on Mar 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi miopod, a warm welcome to WebmasterWorld. :)

First question. Any reason you aren`t using a MySQL database? It would be far easier to work with.

dc

miopod

1:39 pm on Mar 31, 2009 (gmt 0)

10+ Year Member



thanks, at first i was a bit overwhelmed with mysql, but i think i finally got the hang of it for my needs. It's definately better than a flat file system

dreamcatcher

3:29 pm on Mar 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it sorted. MySQL is far more robust than flat text file. Thats not to say flat text file doesn`t have its uses.

dc