PHP flatfile flat-file...
Moderators: egami, macek, gesf
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.
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.
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
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!)
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!
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)
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)
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! ;]-
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! ;]-
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)
<?
$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)
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";
"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)
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)