Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

PHP flatfile flat-file...

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

PHP flatfile flat-file...

Postby Pinex » Tue Oct 29, 2002 5:31 am

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.
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

Postby Jay » Tue Oct 29, 2002 5:58 am

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
Jay
 

Thanks alot Jay... Anybody else?

Postby Pinex » Tue Oct 29, 2002 6:36 am

"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?
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

Postby Jay » Tue Oct 29, 2002 6:55 am

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!)
Jay
 

I know..

Postby Pinex » Tue Oct 29, 2002 7:19 am

...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?
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

Postby Jay » Tue Oct 29, 2002 7:41 am

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!
Jay
 

First bug. Hwat did I do wrong?

Postby Pinex » Tue Oct 29, 2002 8:36 am

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)
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

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

Postby Pinex » Tue Oct 29, 2002 11:38 am

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! ;]-
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

Postby DutchBen » Tue Oct 29, 2002 12:02 pm

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)
DutchBen
New php-forum User
New php-forum User
 
Posts: 36
Joined: Thu Oct 24, 2002 10:28 am
Location: Amsterdam

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

Postby Jay » Tue Oct 29, 2002 12:45 pm

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";
Jay
 

Thanks alot Jay (guru) and DuchBen!

Postby Pinex » Fri Nov 01, 2002 10:48 am

"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)
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW


Return to PHP coding => General

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron