0

hi there
do you know if it is possible to select data from a comma separated file?
i need only name and message from this and show it in plain html or css
number$sex$state$name$surname$zip$street$city$e-mail$ip$message$

there are many lines in the csv

3
Contributors
18
Replies
19
Views
7 Years
Discussion Span
Last Post by Jooks
0

Do you mean like the following?

$data=explode(',',file_get_contents('path/to/file.txt'));
for ($i=0;$i<count($data);$i++) {
echo $data[$i];
echo '<br>';
}
0

yeah that gives me all the content from the file but i only wnat specific data.

for instance the "John" and "hey, i need som help, please!!"

from

104001$Male$California$John$Doe$DC 20005
$42 Washington street$Seattle$johndoe@e-mail.com$xxx.xxx.xx.10$hey, i need som help, please!!$
104000$Female$California$Betty$Doe$DC 20005
$44 Washington street$Seattle$bettydoe@e-mail.com$xxx.xxx.xx.10$please call me..$

etc.

is this possible?

0
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>

this still doesn't help me in what i want. it returns all the content from the file.

i only want specific data from the file and to be able to chosse freely

the code above might be a spoon but i'm not eating soup :-)
sorry if i misunderstand anything... but i think i need it spelled out if there is an answer to this in the links..

Edited by mike_2000_17: Fixed formatting

0

you have an an array $data made up of a single record row
you know the elements in the array $data[0] to data[10]

echo "hello $data[3] , In reply to your $data[10] we can /* bla bla bla bla */";

CSV handling is not elegant in finding a particular row, except by stepping through, it is not a random accessible database
have always fwritten the last row number to another file on fputcsv
selecting a single line, first, last, or any by line number, is a piece of cake
the code example in your prior post shows stepping through the record lines by 1 row $row, and $row++

Edited by almostbob: n/a

0

now i think i see what you mean.
$data[$c]

for instance $data[3] etc.

but how do i get it out only once and not in long arrays?
sorry - i'm a rookie

0

IF you are selecting a single record, then have an input form somewhere thay you can input the information you are searching on with the same field names and order as your csv file
then when you step through the file

if(isset(search[0])) {if (data[0]=search[0]) {echo "hello $data[3] , In reply to your $data[10] ";} }
if(isset(search[1])) {if (data[1]=search[1]) {echo "hello $data[3] , In reply to your $data[10] ";} }
//etc

with apologies for really crappy code sample, its has been ages since I thought about it, its so much easier without thinking

Edited by almostbob: n/a

0

ups, i read your post after i posted my last

my only problem now is the thing you mention about rows. i only want data from the first row. right now everything comes out in alle the rows... :-(

0

i'm not sure i understand the input form solution but this is under all circumstances not possible in the setup

i need to get it from the knowledge of the number of the data $data[3]

but is it possible to just have it out from one row - the top row?

0
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";

        for ($c=0; $c < $num; $c) {
            echo $data[3] . "<br />\n";
        }
    }
    fclose($handle);
}

?>

if i write this it only comes out with the number 3 data in the first line
but it will go on for ever
do you now how to just have it out once?

0

while loops until a fail occurs, looping on the same line does not fail

<?php if (($handle = fopen("test.csv", "r")) !== FALSE)  {
 $data = fgetcsv($handle, 1000, ",")  
 echo "$data[3] ,  $data[10]" /* bla bla bla bla */ ;
 fclose($handle);
} 
else echo 'failure opening data file'; ?>

Edited by almostbob: n/a

0

it's probably just me cant find the error.
it gives me
Parse error: syntax error, unexpected T_ECHO on line 3

but the parse error isn't " instead of '
what is it?

Edited by Jooks: n/a

0

it's probably just me cant find the error.
it gives me
Parse error: syntax error, unexpected T_ECHO on line 3

but the parse error isn't " instead of '
what is it?

Yes there is a bug and is fixed in the following:

<?php if (($handle = fopen("test.csv", "r")) !== FALSE)  {
 $data = fgetcsv($handle, 1000, ",");
 echo "$data[3] ,  $data[10]"; /* bla bla bla bla */
 fclose($handle);
} 
else echo 'failure opening data file'; ?>
0

Thankyou Cwarn23, just came back to find the footprints where I stepped in it, have been repaired
/* bla bla bla */ :(

0

ah ok, of course.
now it works. thanks!

now I only need to pick from the second line in my cvs file cos' my first line is just row names

do you know how this is done?

0

run the select twice, do nothing with the first result
fgetcsv gets the next line each time it is called

Edited by almostbob: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.