Page 1 of 1

PHP flatfile flat-file...

Posted: Tue Oct 29, 2002 5:31 am
by Pinex
Help!

I have a simple text file with product information built up something like this:

|1234|Product name|description|other|other...||||||
|1235|Product name2|description2|other2|other2||||||
|39|Product name3|description3|other3|other3||||||

I don't have, and I dont want a MySql database for this product list.
(Is ther an other common name for flafile?)

I want to know how to use PHP to display products from this list in my webpage.

There is alot of php flatfile script out there, but they have loads of functions I don't nead, and I get confused trying to ounderstand what they actually do.

Is there a nice script in php hwo does this in a simple way?
...Or could anyone tell me how to make it?

Thanks alot.

Posted: Tue Oct 29, 2002 5:58 am
by Jay
Simple enough

Code: Select all

$contents = file("file.txt"); # Reads file into an array
while(list($num,$line) = each($contents)) { # Loops through each line
$data = explode("|",$line); # Separates data into an array
print $data[0]; # Print 1st element
} # Close loop

Thanks alot Jay... Anybody else?

Posted: Tue Oct 29, 2002 6:36 am
by Pinex
"Ouhgh!!! " (right in the belly, cungfu style).
I forgot to tell that I'm actually a newbe in PHP, but i like a good challenge.
I'll try to solve the riddle ;]-


Is there a better name for flatfile?

Posted: Tue Oct 29, 2002 6:55 am
by Jay
Not that I know of, and I'm actually surprised people use them given how much easier it is to use a database (and believe me, I know 'cos I built one of my first sites using that method and then quickly moved over to DB when the data started to mount up causing errors!)

I know..

Posted: Tue Oct 29, 2002 7:19 am
by Pinex
...but I don't have access to a MySql on the server I'm using.
Is there another kind of databases thats not using MySql and that dont have to be installed on the server?

Posted: Tue Oct 29, 2002 7:41 am
by Jay
Well, since MySQL is free, it's hard for a hosting company to justify not using it. I'd move if I were you. The advantages far far far outweigh any possible disadvantage. I personally don't see any disadvantages. MySQL is faster, more robust, user friendly, powerful, etc etc etc!

First bug. Hwat did I do wrong?

Posted: Tue Oct 29, 2002 8:36 am
by Pinex
I made:

wiev.php
----------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>test</title>
</head>
<body>
<?php include("test.php"); ?>
</body>
</html>
----------------------------

test.php
----------------------------
<?
$contents = file("file.txt");
while(list($num,$line) = each($contents)) {
$data = explode("|",$line);
print $data[0];
}
?>
----------------------------

file.txt
----------------------------
|1234|Product name|description|other|other...||||||
|1235|Product name2|description2|other2|other2||||||
|39|Product name3|description3|other3|other3||||||
----------------------------

I uploaded all 3 to one an same folder, CHMOD file.txt to 666 (tryed 777), an got a blank page in Opera running the wiev.php

Any idea?

Pinex (still newbe)

"It's alive!" But I'd love som tips N trix.

Posted: Tue Oct 29, 2002 11:38 am
by Pinex
I finally solved the riddle...
I took the code you (Jay) gave me, and did my homeworck...
I got som ideas from the http://www.php.net/manual/en/function.list.php , and made this:

<?php
$contents = file("file.txt");
while(list($num,$line) = each($contents)) {
$data = explode("|",$line);
$info = array($data[1], $data[2], $data[3], $data[4], $data[5], $data[6]);
list($prn, $name, $descr, $url, $m1, $m2) = $info;
print "$prn $name $sescr $url $m1 $m2<br>\n";
}
?>

Do you hve any tips N trix before I start putting alot of HTML tables in to the print ""?

Thanks again! ;]-

Posted: Tue Oct 29, 2002 12:02 pm
by DutchBen
Php lets you jump in and out very easy. If your buillding tables i always like to keep my HTML and php separated as much as possible. So i would

<?
$contents = file("file.txt");
while(list($num,$line) = each($contents)) {
$data = explode("|",$line);
$info = array($data[1], $data[2], $data[3], $data[4], $data[5], $data[6]);
list($prn, $name, $descr, $url, $m1, $m2) = $info;
?>

<tr>
<td><?=$prn?></td>
<td><?=$name?></td>
..... etc.

<?}?>

Where '=' is a shorthand notation for echo (which does the same thing print() does)

Re: "It's alive!" But I'd love som tips N trix.

Posted: Tue Oct 29, 2002 12:45 pm
by Jay
Pinex wrote:I finally solved the riddle...
I took the code you (Jay) gave me, and did my homeworck...
I got som ideas from the http://www.php.net/manual/en/function.list.php , and made this:

<?php
$contents = file("file.txt");
while(list($num,$line) = each($contents)) {
$data = explode("|",$line);
$info = array($data[1], $data[2], $data[3], $data[4], $data[5], $data[6]);
list($prn, $name, $descr, $url, $m1, $m2) = $info;
print "$prn $name $sescr $url $m1 $m2<br>\n";
}
?>

Do you hve any tips N trix before I start putting alot of HTML tables in to the print ""?

Thanks again! ;]-

Let me re-write your code for you - it's hideous!!!

Code: Select all

<?php
$contents = file("file.txt");
while(list($num,$line) = each($contents)) {
list(,$prn, $name, $descr, $url, $m1, $m2) = explode("|",$line);
print "$prn $name $sescr $url $m1 $m2<br>\n";
}
?>

My best tip would be to use a templating class! However, for a newbie the best thing is to open the table before the loop, on each loop generate a row, and then close the table after the loop has ended, like this:

Code: Select all

print "<table settings>\n";
$contents = file("file.txt");
while(list($num,$line) = each($contents)) {
    $data = explode("|",$line);
    print "\t<tr>\n";
    array_shift($data); # Remove element[0]
    foreach($data as $element) print "\t\t<td>$element</td>\n";
    print "\t</tr>\n";
}
print "</table>\n";

Thanks alot Jay (guru) and DuchBen!

Posted: Fri Nov 01, 2002 10:48 am
by Pinex
"It's alive"!

The scriplets U guys gave me is just the way I like it! Tiny powerful code.. No extras.
That's why I newer use WYSIWYG, I actually use Homesite 4.5.2, it enlightens the code in a beautiful way.

I definitely know where to go with my php sorrows in the future! ;]-

Pinex (Not so totally PHPnewbe anymore)