Current location: Hot Scripts Forums » Programming Languages » PHP » reading info from a file (csv / xls) and displaying info


reading info from a file (csv / xls) and displaying info

Reply
  #1 (permalink)  
Old 12-29-03, 04:59 AM
lordmerlin lordmerlin is offline
Newbie Coder
 
Join Date: Jul 2003
Location: South Africa
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
reading info from a file (csv / xls) and displaying info

Hi

I have files, either in .txt of .xls format, containing info taken from a device, and delimeted with a coma, which I would like to be able to display in a table format. The problem I have is, the device can take any number of readings, and the operators doesn't always take the same amount of readings. If the operator takes 5 readings, the device creats 5 fields, if he take 28, it creates 28 fields. Now, I want to be able to count the amount of fields, seperated by comma, and then create a table, and display the info.

Can someone please help me with this, as I'm still fairly new to PHP, and the turorials page didn't turn up much

tia
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-29-03, 05:38 AM
zero's Avatar
zero zero is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by lordmerlin
Hi
The problem I have is, the device can take any number of readings, and the operators doesn't always take the same amount of readings.
Your device ?i
if you create table
id
numberofreading
file
...
you used code :
<?PHP
$str = SELECT numberofreading
FROM yourtable
WHERE file="Name_file";
$res=mysql_query($str) or die("");
echo "number of readings " ;
echo $rec

?>
--- bye---
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-29-03, 07:07 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

I guess tia meant something like this:

Code:
Amy,1980-01-26,604-111-2222,Vancouver
Bob,1979-06-25,604-222-3333,Montreal
Cathy,1967-11-21,250-333-4444,Toronto
This is a CSV file, having 4 columns and 3 rows.

Assuming what tia meant was that, though this time there are 4 colmns, there might be 5, 6, or 28 columns next time, and wants to have as many <td></td>'s as there are columns in the CSV file.

If that's the case, you can explode the first line into an array to get the column count (please read the comment as it may be helpful for you to understand what's going on):

PHP Code:

<?php


// Get the file contents into an array. file() takes each line of the specified file and assign it to an array value. You will get an array var $a_lines.
$a_lines file('/path/to/yourfile.csv');

// Get one line from the array for explosion.
// explode() takes a string and splits it into array values, delimited by the delimiteer of your choice - it's the comma here.
$a_columns explode(','$a_lines[0]);

// Now count how many "columns" are in there.
$num_columns count($a_columns);

// Just to make sure, fire it off here.
// You should get 4 when tried with the above CSV file.
echo $num_columns;

?>
It seemed that you know how to handle once you have the column number, so I'll cut it here. Hope this is what you wanted.

Good night,
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 12-30-03, 05:12 PM
lordmerlin lordmerlin is offline
Newbie Coder
 
Join Date: Jul 2003
Location: South Africa
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
This is a great help, thanx.

Just one last one to it. How do I access the values the amount of columns change, I won't be able to access them like this:

PHP Code:

print $a_columns[1]; print $a_columns[2]; 

What do I need to todo to access anyone of these?
From here I would need to insert the values into a database. But I need to to check if the amount of columns equal the amount of columns in the database, and if the column names are the same. They have a format of
Block 1FE; Block 1 NFE; BLock 2FE; Block 2NFE, and so on, until the last one that was taken, be it say Block 25NFE.

The main problem is, is a reading was skipped, then it shouldn't be able to insert the code into the table.

tia

Quote:
Originally Posted by blaw
Hi there,

I guess tia meant something like this:

Code:
Amy,1980-01-26,604-111-2222,Vancouver
Bob,1979-06-25,604-222-3333,Montreal
Cathy,1967-11-21,250-333-4444,Toronto
This is a CSV file, having 4 columns and 3 rows.

Assuming what tia meant was that, though this time there are 4 colmns, there might be 5, 6, or 28 columns next time, and wants to have as many <td></td>'s as there are columns in the CSV file.

If that's the case, you can explode the first line into an array to get the column count (please read the comment as it may be helpful for you to understand what's going on):

PHP Code:

<?php


// Get the file contents into an array. file() takes each line of the specified file and assign it to an array value. You will get an array var $a_lines.
$a_lines file('/path/to/yourfile.csv');

// Get one line from the array for explosion.
// explode() takes a string and splits it into array values, delimited by the delimiteer of your choice - it's the comma here.
$a_columns explode(','$a_lines[0]);

// Now count how many "columns" are in there.
$num_columns count($a_columns);

// Just to make sure, fire it off here.
// You should get 4 when tried with the above CSV file.
echo $num_columns;

?>
It seemed that you know how to handle once you have the column number, so I'll cut it here. Hope this is what you wanted.

Good night,
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 12-30-03, 11:00 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

Do you mean the second row may contain a different number of columns from the first one? All rows in a CSV file should contain the same number of columns, though.

Anyway, if you want the second row (in the file), then you fetch it:

PHP Code:

$num_seond_row explode(','$a_lines[1]); 

If the third, change $a_lines[1] to $a_lines[2], and so on. If you want them all, you can loop it.

In regards to the database insertion, I do not quite understand what you are trying to do.

[quote=tia]
I would like to be able to display in a table format
[quote]

I thought you wanted to display, not store, the values in HTML, not database, table...

[EDITED]
Typo found, really stupid one... =)
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 12-31-03, 01:04 AM
lordmerlin lordmerlin is offline
Newbie Coder
 
Join Date: Jul 2003
Location: South Africa
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
No, the amount of lines stay the same per .csv file, however, this may be different everytime (i.e. every reading might give a different amount of lines), but this is not the problem.

I want to be able to access a value in the array, say for instance a_columns[3], or a_columns[5], and also then be able to add these values into a table, which isn't a big problem, cause I can create the amount of rows taken from $num_columns.

I just want to know what to do to make the following:

PHP Code:

$block1fe $a_column[0];

$block1nfe $a_column[1];
$block2fe $a_column[2]; 
I think this would have to be done with a loop, in order to take the amount of rows, and increment the
PHP Code:

$a_column[n
part. In this instance the file has 16 columns, so I would need upto
PHP Code:

$a_column[15
Does this clarify it more? Cause from here I can insert it into the dabase, after some checks are made, in the HTML form
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 12-31-03, 02:53 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi again,

Since you asked, I'll answer how, but you are using PHP to do what MySQL can do much faster. You might want to check this:

MySQL: 6.4.8 LOAD DATA INFILE Syntax
http://www.mysql.com/doc/en/LOAD_DATA.html

Okay then, here's the code. Please read the comments for detail.

PHP Code:

 <?php 


// Get the file contents into an array. file() takes each line of the specified file and assign it to an array value. You will get an array var $a_lines. 
$a_lines file('/path/to/yourfile.csv'); 

// Instead of one line, we'll get ALL lines.
// The trick here is to have the loop counter $i as a counter as a counter for the lines.
// Array starts with [0], so $i = 0.
// Less than, NOT less-than-or-equalt-to, for the above reason as the cut-off condition.
for ($i 0$i count($a_lines); $i++) {
    
// Now we assign each line's exploded values to an array $a_columns[N] where N = $i, which is in turn part of one big array $a_columns.
    // So to speak, one big array $a_columns contains N number of arrays.
    // $i (= line number - 1) is used here to identify each sub-array (containing exploded values of (N -1)th line).
    
$a_columns[$i] = explode(','$a_lines[$i]);
}

// You can still count the columns by specifying one of the sub-arrays. I used [0] (the first line) because it should always be there.
$num_columns count($a_columns[0]);

// Just to make sure, fire them off here.
echo '<pre>';
echo 
'Number of Columns: '.$num_columns.'<br /><br />Your array $a_columns contains: <br /><br />';
print_r($a_columns);
echo 
'<br /><br />Here is the second column value from the first line ($a_columns[0][1]): '.$a_columns[0][1];
echo 
'</pre>';
?>
From the above code, when I ran it with the above CSV file (my first post), I got this:

Code:
Number of Columns: 4

Your array $a_columns contains: 

Array
(
    [0] => Array
        (
            [0] => Amy
            [1] => 1980-01-26
            [2] => 604-111-2222
            [3] => Vancouver

        )

    [1] => Array
        (
            [0] => Bob
            [1] => 1979-06-25
            [2] => 604-222-3333
            [3] => Montreal

        )

    [2] => Array
        (
            [0] => Cathy
            [1] => 1967-11-21
            [2] => 250-333-4444
            [3] => Toronto
        )

)


Here is the second column value from the first line ($a_columns[0][1]): 1980-01-26
By the way, the array should be renamed because it now contains more than just "columns". =)
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-20-04, 04:39 PM
KING KING is offline
New Member
 
Join Date: Mar 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
i need help

i want to take input " Roll Numer" and want to show Result "phone number name and address" in next page.

file name is database.cvs OR database.txt

"0393","Farrukh Hussain","1D 7/1, CLIFTON KARACHI."
Roll number , Name , Address
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 09:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.