How can I use PHP to retrieve fields from a CSV file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ericg
Forum Newbie
Posts: 1
Joined: Sat Jun 25, 2011 6:46 pm

How can I use PHP to retrieve fields from a CSV file?

Post by ericg »

I am trying to use a PHP query to retrieve data from a CSV file to then send off to a verification service. The code I have at present looks something like this:

Code: Select all

<?php

  #
  # CSV File reference
  #

$file = fopen("cmdi_php.csv","r");

while(! feof($file))
  {
  print_r(fgetcsv($file));
  }

fclose($file);

main();
function main()
{
  #
  # Sample CMDI Web Serice Client  (only works with php >= 5)
  #
  $WSDL   = "https://sample.com";
  $PARAMS = getRecord();

  $client = new SoapClient($WSDL);
  $retMsg = $client->_process_a_gift($PARAMS)->_process_a_giftResult;

  if (!isset($retMsg) || ...handle exception...) {
    echo "ERROR - retcode=$ retMsg \n";
  }
  else {
    echo "SUCCESS - retcode=$ retMsg \n";
  }
}

function getRecord()
{
  $rec = array();

  $rec['token']      = "####";
  $rec['CallerCode']    = "code";
  $rec['CallerTID']    = "A1";
  $rec['Prefix']      = "PHP";
  $rec['FirstName']    = "First";
  $rec['MiddleName']    = "Mid Name";
  $rec['LastName']    = "Last Name";
  $rec['Suffix']      = "Suffix";
  $rec['Address1']    = "7704 Leesburg Pike";
  $rec['Address2']    = "Address 2";
  $rec['City']      = "City";
  $rec['State']      = "VA";
  $rec['Zip']        = "22043";
  $rec['Plus4']      = "1111";
  $rec['Email']      = "email@email.com";
  $rec['Phone_Home']    = "703-790-8676";
  $rec['Phone_Work']    = "(703)790-9389";
  $rec['Employer']    = "Employer";
  $rec['Occupation']    = "Occupation";
  $rec['GiftDate']    = "2006-12-13";
  $rec['GiftAmount']    = '900';
  $rec['Card_Name_on']  = "John Doe";
  $rec['CardNo']      = "4444333322221111";
  $rec['Card_Exp_Month']  = "12";
  $rec['Card_Exp_Year']  = "2006";
  $rec[‘CVCode’]    = "1234";
  $rec['More_in_XML']    = "<more><flag>VIP</flag></more>";

  return $rec;
}
?>
The $rec fields need to pull from the CSV fields of the same names - how do I accomplish this??? Then I'm golden!

Thank you!
Eric G.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: How can I use PHP to retrieve fields from a CSV file?

Post by yacahuma »

i dont understand what you mean by same names. If is a CSV, all you care is the column number. if you need to create something like

$rec['name']
$rec['phone']

just create a translation array

$translate = array (0=>'name', 1=>'phone');

Code: Select all

$row = 1;
$allrecords = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        $rec = array();
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
            $rec[$translate[$c]] = $data[$c]; 
        }
       $allrecords[] = $rec;
    }
    fclose($handle);
}
I think that should work
Post Reply