Forum Moderators: coopster

Message Too Old, No Replies

how to make included file's text look good

         

morcar

8:58 pm on Aug 22, 2005 (gmt 0)

10+ Year Member



Hi all.

First off I'm not a programmer. I only work simple web pages in HTML. :D I know very little about PHP and can only seem to master the very beginning basics. Help!

Here's my problem: I have a file called projects.php. Within projects.php there is this line:

<? include("listofprojects.csv");?>

listofprojects.csv is a comma-delimited file that shows all of my client's current projects including fields like ProjectName, Builder, ModelAddress, City, State, Zip, County, & MapLink. (The client keeps this list in an Excel file & I converted it to a CSV file.)

When I use the above include line, of course all the data runs together!

What do I need to do to make it look good? Like in columns, for example? Do I have to parse it? I don't even know what parsing is! Is this a complicated sort of thing I want to do?

Please help and thank you so much,
Morcar

henry0

9:40 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am afraid that you are not going to like my reply :)

I see the CSV as a two-way vector

A) You may get info from a DB in a CSV file form
B) you can do the reverse for ex: type some input regarding a shopping cart products in XL format then convert in CSV and plug the new info in your cart products list so in one shot you might add many new products

In your case you need to create an HTML page with the CSV info or create a DB feed the DB with the CSV, query that DB then format the output

I told you not fun!

Other members might have better ideas.

mcibor

10:01 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function parse_csv($file) {
if(!($op = fopen($file.".csv"))) return false;
$csv = fread [php.net]($op, filesize [php.net]($file.".csv"));
$csv = implode [php.net]("</tr><tr>\n", explode [php.net]("\n", $csv));
$csv = implode("</td><td>\n", explode(",", $csv));
$table = "<table><tr><td>".$csv."</td></tr></table>";
return $table;
}

To use this function simply do change <? include("listofprojects.csv");?> to:
<? echo parse_csv("listofprojects");?>

To change look change the <table, <tr, <td. But remember that if you wish to have quote " then use this structure: \"
$csv = implode("</td><td width=\"10%\">\n", explode(",", $csv));
Best regards
Michal Cibor

henry0

10:11 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Michal,
Good and simple never thought in that direction
Thanks for the pointer.

Henry

mcibor

10:36 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've created a tree-structure maker like this :) (One query to db only, very fast, infinite categories, no recurency). Csv was easy-peasy <lol>

See you round!
Michal

morcar

3:33 am on Aug 23, 2005 (gmt 0)

10+ Year Member



Hi Michal, thank you. I copied this into the body of my page - that is, inside the <body> tags and I get an error. Can you troubleshoot with me? - because I don't know what this means. Here is the error I got:

Warning: fopen() expects at least 2 parameters, 1 given in /projects.php on line 22

Thank you again. All help is greatly appreciated.

dcrombie

9:22 am on Aug 23, 2005 (gmt 0)



Have you looked in the manual [php.net]?

mcibor

2:25 pm on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I forgot.
Here you are - with two parameters:
function parse_csv($file) {
if(!($op = fopen($file.".csv", "r"))) return false;
$csv = fread($op, filesize($file.".csv"));
$csv = implode("</tr><tr>\n", explode("\n", $csv));
$csv = implode("</td><td>\n", explode(",", $csv));
$table = "<table><tr><td>".$csv."</td></tr></table>";
return $table;
}

Best regards
Michal Cibor

PS. Morcar wrote he's not a programmer... My fault :)

mcibor

9:24 am on Aug 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There was a problem with the code above.
I missed some endings and beginnings. This works on my server:
function parse_csv($file) {
if(!($op = fopen($file.".csv", "r"))) return false;
$csv = rtrim(fread($op, filesize($file.".csv")), "\n");
$csv = implode("</td>\n</tr>\n<tr>\n<td>", explode("\n", $csv));
$csv = implode("</td>\n<td>", explode(",", $csv));

$table = "<table border=\"1\"><tr>\n<td>".$csv."</td>\n</tr></table>";
return $table;
}

Sorry Morcar again:)
Best regards
Michal Cibor

mcibor

10:54 am on Aug 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now I see that this could be done with two str_replace as well! (But not one!)

Michal Cibor