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