Jump to content

txt file csv


shage

Recommended Posts

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

Link to comment
Share on other sites

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
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
Share on other sites

Also, in the function I provided above, if you'd like to make it case insensitive, change this line

 

      if (strpos($line, $search) !== FALSE)

 

to this

 

      if (strpos(strtolower($line), strtolower($search)) !== FALSE)

Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.