Jump to content

PHP CSV Select


nickf1195

Recommended Posts

Hey everyone,

I have a problem here that I can't seem to figure out (that would explain why i'm posting here  ;D).

 

I have this code:

 

<?php

function select_csv($file,$delimiter,$field,$query)
{
$data_array = file($file);
for ( $i = 0; $i < count($data_array); $i++ )
{
  $parts_array[$i] = explode($delimiter,$data_array[$i]);
  if(trim(strtolower($parts_array[$i][$field])) == trim(strtolower($query)))
  {
   $result_array[] = $parts_array[$i];
  }
}
return $result_array;
}
echo '<p>';  

$data = select_csv('data.csv','|','0','demo-query');
for ( $i = 0; $i < count($data); $i++ )
{
for ( $u = 0; $u < count($data[$i]); $u++ )
{
  echo $data[$i][$u].' ';
}
echo '<br>';  
}

?>

 

I have a csv file with two columns. This script accepts a query and displays every record whose first column is equal to that query (demo-query above.)

 

I'd like it to just return the content of the second column. I've been trying for hours and can't seem to figure this one out.

 

Any level of help is greatly appreciated.  ;D

 

Nick

Link to comment
https://forums.phpfreaks.com/topic/116523-php-csv-select/
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.