Forum Moderators: coopster

Message Too Old, No Replies

Trying to learn CSV

         

Sypher_5

7:45 pm on Jan 21, 2004 (gmt 0)

10+ Year Member



I've got this far with opening a using CSV & understand what it's doing & now cant figure out where to go from here to do the following.

<?php

$filename = "data1.txt"; //here's the filename

$id = fopen($filename, "r"); //open the file
while ($data = fgetcsv($id, filesize($filename))) //start a loop
$table[] = $data; //put each line into its own entry in the $table array
fclose($id); //close file

echo "<table>\n";

foreach($table as $row)
{
echo "<tr>";
foreach($row as $data)
echo "<td style='border:1px solid #000;'>$data</td>";
echo "</tr>\n";
}

echo "</table>\n";

?>

Now say I want to get each of these & include them in a tables in certain places as I call them. What I need is to be able to get the data as varibles eg:$alias, $date & $data. Then for each row I want a new table. The CSV file layout is Alias¦Date¦Data

So say the first two rows where,

333¦1902¦qwerty
444¦1949¦ytrewq

I would want the info in tables like so,

This is a 333 It was made 1902 & here's the qwerty.
This is a 444 It was made 1949 & here's the ytrewq.

After that I'm going to want to be able to choose which row I start on and how many entries to get.

hakre

9:11 pm on Jan 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi Sypher_5,

that's not too complicated. the point you need to know is, that $table and $row are both arrays. you can access each value in a row by using a number. the first field (in your example alias) can be accessed by number 0 (zero), the second is 1 (one)...

for example you can change the output at the end of your script to something like this, and i highlighted in bold where the numbers are placed:

echo "<table>\n"; 

foreach($table as $row)
{
echo "<tr>";
echo '<td style="border:1px solid #000;">'This is a '.$row[[b]0[/b]].' It was made '.$row[[b]1[/b]].' & here's the '.$row[[b]2[/b]].'</td>';
echo "</tr>\n";
}

echo "</table>\n";

another example:

echo "<table>\n"; 

foreach($table as $row)
{
echo "<tr><th>Alias</th><th>Date</th></tr>\n";
echo "<tr>";
echo '<td>'.$row[[b]0[/b]].'</td>';
echo '<td>'.$row[[b]1[/b]].'</td>';
echo "</tr>\n";
echo "<tr>";
echo '<td colspan="2" style="border:1px solid #000;">'.$row[[b]2[/b]].'</td>';
echo "</tr>\n";
}

echo "</table>\n";

coopster

9:35 pm on Jan 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You also need to separate the values when you read them in since your
separator
is not the default of comma; you haven't done that yet:

while ($data = fgetcsv($id, filesize($filename), "¦")) //start a loop

Sypher_5

10:37 pm on Jan 21, 2004 (gmt 0)

10+ Year Member



Thanks Guys, lots of help. :)

knnknn

3:58 pm on Jan 24, 2004 (gmt 0)

10+ Year Member



Something like
333¦1902¦qwerty
is very easy to parse.

But what if I want to parse data which is separated like this:

<tr><td>333</td><td>1902</td><td>qwerty</td><tr>

Or where each item is multi-line and html?

<tr>
<td>
<b>3<br>
33
</td>

<td>
1902
</td>

<td>
qwerty
</td>
<tr>

coopster

4:35 pm on Jan 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would probably go with a Regular Expression Function (Perl-Compatible) PCRE, such as preg_match_all [php.net].

There are examples on that page in the PHP manual for parsing HTML, and a few discussions on this forum that are relative.
HTML extraction [webmasterworld.com]
html parsing [webmasterworld.com]
Help with fread() - Limit size of file being read? [webmasterworld.com]
Extracting parts of an html page [webmasterworld.com]

Sypher_5

7:13 am on Jan 25, 2004 (gmt 0)

10+ Year Member



Hey cool but your first answers sorted it out.

Now I'm working on writting to the file ( which I have working ), but still trying to figure out how to choose a line and edit just that line.

coopster

9:42 pm on Jan 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Maybe this thread will help...
reading from .txt and changing specified var's in .txt [webmasterworld.com]