Reading Data From .CSV Files

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Pieman
New php-forum User
New php-forum User
Posts: 2
Joined: Sat Jun 21, 2003 9:35 pm

Reading Data From .CSV Files

Postby Pieman » Sat Jun 21, 2003 9:50 pm

I am trying to make queries from a CSV file. I can do a full list but not a partial list. $data[1] is the field I am using as a link and $country is submitted. What I want to do is just pull out all of a particular country into a list. Below is my umpteenth attempt at the Partial List , but I keep getting a full one. I know I could use MySQL but life is too easy that way...

<?php
$fp=fopen("./database.csv","r");
$x = fgetcsv ($fp, 1000, ",");
while (!feof($fp)) {
$data = fgetcsv ($fp, 1000, ",");{
if ($x[11]=$scountry);{
print $data[1];
print $data[11];
print "<br>";
}
}
}
fclose($fp)?>

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Sat Jun 21, 2003 10:17 pm

getcvs works unperfect... sorry.

try to use explode or regular expresion for this.

User avatar
pootergeist
New php-forum User
New php-forum User
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK
Contact:

Postby pootergeist » Sun Jun 22, 2003 8:32 am

Code: Select all

<?php
$fp=fopen("./database.csv","r");
while($data = fgetcsv ($fp, 1024, ","))
  {
  echo ($data[11] == $country) ? $data[1]. ' from ' .$data[11]. '<br />' : '' ;
  }
fclose($fp);
?>


you were using an assignment = in your if clause ( if(a=b) should be if(a==b)) and muddling a few vars around.

try the above anyway.

subnote: be sure to set $country to some value somewhere

Pieman
New php-forum User
New php-forum User
Posts: 2
Joined: Sat Jun 21, 2003 9:35 pm

Postby Pieman » Mon Jun 23, 2003 12:32 am

Thanks, I got around it with more or less the same conclusion. Here is what I did

<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#990000">
<?php
if (!$fp = fopen("./database.csv","r"))
{
echo "Sorry, cannot open the file";
}
else
{
while(!feof($fp)) {
$data = fgetcsv ($fp, 1000, ",");
if ($data[11]==$scountry)
print "<tr><td width=\"50%\"><a href=\"datasheet.php?pass=".$data[0]." \">".$data[1]."</td><td>".$data[13]."</tr>";
}
fclose($fp);
}
?>
</table>


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron