Jump to content

txt file csv


shage

Recommended Posts

I'm not sure what a GET_ is. And making assumptions is never a good idea.

 

anyways, here's how i'd do it

 

<?php
function parse_csv($file, $search) {

   if (!$fArr = file($file) )
      return FALSE;

   foreach($fArr as $line)
      if (strpos($line, $search) !== FALSE)
         $ret[] = $line;

   if (is_array($ret) )
      return $ret;
   else
      return FALSE;
}

$array = parse_csv('path/to/csv.txt', 'shoes');
if ($array)
   foreach ($array as $value)
      echo $value;
else
   echo 'no lines found!';
?>

Link to comment
https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491462
Share on other sites

i have a csv that is broken into arrays one section being the description of the links, so if the user comes from the search term shoes, i would like it to get the get_ term=shoes and parse the csv for any link with shoes in the description, hope that makes more sense

It would be very helpful if you provided a few lines of data from the text file and explained exactly what you want returned. Without knowing the format of the input and output it is difficult, if not impossible, to provide a good response.

Link to comment
https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491464
Share on other sites

header('Content-type: text/xml');

echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; //remove space after ? (added for make it format in here)

echo "<rss version=\"2.0\">\n";

echo "  <channel>\n";

echo "    <title>$site</title>\n";

echo "    <link>http://www.yahoo.com/</link>\n";

echo "    <description>Feeds</description>\n";



$row = 1;

while (($data = fgetcsv($handle, 1000, "|")) !== FALSE)

{
$link = $data[0];

$description = $data[1];

$thumb= $data[2];

echo "    <item>\n";

echo "       <title>$description</title>\n";

echo "       <link>$shage2</link>\n";

echo "       <description><![CDATA[<img src=\"$thumb\"]]>></description>\n";

echo "    </item>\n";

}

fclose($handle);



echo "  </channel>\n";

echo "</rss>\n";

 

that is the code im trying to loop the results to

Link to comment
https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491506
Share on other sites

Easy...

 

add this function somewhere in your code

 

function parse_csv($file, $search) {

   if (!$fArr = file($file) )
      return FALSE;

   foreach($fArr as $line)
      if (strpos( strtolower($line), strtolower($search) ) !== FALSE)
         $ret[] = explode(',', $line);

   if (is_array($ret) )
      return $ret;
   else
      return FALSE;
}

 

and then call your rss like this

 

echo "    <description>Feeds</description>\n";

$arr = parse_csv('path/to/csv.txt', 'shoes');

foreach ($arr as $data) {

echo "    <item>\n";

echo "       <title>$data[1]</title>\n";

echo "       <link>$data[0]</link>\n";

echo "       <description><![CDATA[<img src=\"$thumb\"]]>></description>\n";

echo "    </item>\n";

}

echo "  </channel>\n";

Link to comment
https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491597
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.