PHP CSV Utilities

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP CSV Utilities

Post by Christopher »

I am not sure all of the things that the sniffer does, but it seems like there are probably several independent things that could be asked for. But if the sniffer trys to guess about a condition then certainly when the programmer knows the condition exists they don't need the sniffer.

Code: Select all

try {
    $filename = './data/orders.csv';
    $sniffer = new Csv_Sniffer_Headerrow($filename);
    $reader = new Csv_Reader($filename);
 
    $reader->hasHeaderRow($sniffer->hasHeaderRow());
    // or if the programmer knows then
    $reader->hasHeaderRow(true);
 
    // and be able to do things like returning assoc arrays with headers as keys like DB librarys do, or as an object
    $reader->useHeadersForAssocArrays(true);
 
    while ($row = $reader->getRow()) {
        // do something with rows
    }
    // can get headers whenever convenient in the code, not just before the loop
    $header = $reader->getHeaders();
} catch (// bla bla bla) {
}
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: PHP CSV Utilities

Post by Luke »

Or I could add a hasHeader() method do my Csv_Dialect class...

Code: Select all

$filename = './data/orders.csv';
$sniffer = new new Csv_Sniffer($filename);
$reader = new Csv_Reader($filename, $sniffer);
if ($reader->hasHeader()) { // internally checks $dialect->hasHeader()
    // do something
}
 
$headers = $reader->getHeaders();
$data = $reader->toArray(true); // true for "use headers as keys"
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP CSV Utilities

Post by Christopher »

I think both. You want to give flexibility to organize the code in any order -- if possible.
(#10850)
webwired
Forum Newbie
Posts: 6
Joined: Thu Jun 19, 2008 3:27 pm

Re: PHP CSV Utilities

Post by webwired »

I was really looking forward to using your code, but when I tried to use writeRows($data), it kept telling me that I had to pass it an array, that I had in fact passed it a string... which is completely nuts... is that a bug? using php 5.2.5

Here's a bit of the code that I was using...

Code: Select all

# Here's the snippet in which calls the function in which your class is being instantiated
$count_duplicate_results = mysql_query("SELECT unique_field, products_name FROM duplicate_unique_fields WHERE duplicate = 'duplicate' ORDER BY unique_field");
$duplicates_count = mysql_num_rows($count_duplicate_results);
    while ($row = mysql_fetch_assoc($count_duplicate_results)) {
        createCsvFile($row);    
    }
    
# Here's the function
function createCsvFile($rows) {
    
// Just some added code to make sure that I wasn't crazy, that it really was an array that I was receiving    
echo '<pre>';
print_r ($rows);
echo '</pre>';
 
$writer = new Csv_Writer('temp_files/test.csv');
$writer->writeRows($rows);
$writer->close();
    
}
And here's the error I am receiving...

Code: Select all

Catchable fatal error: Argument 1 passed to Csv_Writer::writeRow() must be an array, string given, called in /home/webwired/public_html/Csv/Writer.php on line 124 and defined in /home/webwired/public_html/Csv/Writer.php on line 109
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: PHP CSV Utilities

Post by Zoxive »

I haven't even looked at the code (I barley looked at the project :( ), but I would suggest looking at the documentation.

If I understand correctly array() should be the only thing passed. Because rows have more then one column of information.

Code: Select all

$row = array('234', 'Barney Rubble', '1135 Bedrock Blvd.', '09/16/3029 BC');
$writer->writeRow($row);
http://code.google.com/p/php-csv-utils/ ... umentation

How ever the project looks pretty useful, and most likely will come to use down the line with its simplicity.
webwired
Forum Newbie
Posts: 6
Joined: Thu Jun 19, 2008 3:27 pm

Re: PHP CSV Utilities

Post by webwired »

Well, you should of looked at something before responding then, your post is a waste of everyone's time, especially mine...
I was only sending an array, and I'm not using the single row function, I'm using the multiple row function, which does a foreach with the single row function...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: PHP CSV Utilities

Post by John Cartwright »

The error is occuring because there is typehinting in the writerow method,

Code: Select all

public function writeRow(Array $row) {
Something weird has occured between the time you set $rows and pass it to writeRow(). var_dump is more useful than print_r, also. Please post the results.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: PHP CSV Utilities

Post by Zoxive »

webwired wrote:Well, you should of looked at something before responding then, your post is a waste of everyone's time, especially mine...
I was only sending an array, and I'm not using the single row function, I'm using the multiple row function, which does a foreach with the single row function...
Yes you are, but you are only handing the function a one dimension array, writeRows uses multi dimension.

Which is weird why he classes it as a Mixed variable input for that function, when it expects arrays only.
(The function itself is fine, maybe a force of array, but the input should be array only)
webwired
Forum Newbie
Posts: 6
Joined: Thu Jun 19, 2008 3:27 pm

Re: PHP CSV Utilities

Post by webwired »

I did put the code directly into the while loop of the array and tried it that way quite a while back, got the same results as passing the array to the function...

Did the var_dump, here's the results, ...

Code: Select all

array(2) {
  ["unique_field"]=>
  string(14) "Mike-MOBRSNV-5"
  ["products_name"]=>
  string(47) "US Milspec 2 Pocket Shirt, Battle Rip, Navy, XL"
}
array(2) {
  ["unique_field"]=>
  string(14) "Mike-MOBRSNV-5"
  ["products_name"]=>
  string(47) "US Milspec 2 Pocket Shirt, Battle Rip, Navy, XL"
}
array(2) {
  ["unique_field"]=>
  string(14) "Mike-MOBRSNV-5"
  ["products_name"]=>
  string(47) "US Milspec 2 Pocket Shirt, Battle Rip, Navy, XL"
}
array(2) {
  ["unique_field"]=>
  string(9) "MOACUP-4L"
  ["products_name"]=>
  string(48) "US Milspec Pants, Army Combat Uniform, Lge. Long"
}
array(2) {
  ["unique_field"]=>
  string(9) "MOACUP-4L"
  ["products_name"]=>
  string(48) "US Milspec Pants, Army Combat Uniform, Lge. Long"
}
array(2) {
  ["unique_field"]=>
  string(8) "MORPOD-2"
  ["products_name"]=>
  string(38) "US Milspec Pants, Ripstop, O.D., Small"
}
array(2) {
  ["unique_field"]=>
  string(8) "MORPOD-2"
  ["products_name"]=>
  string(38) "US Milspec Pants, Ripstop, O.D., Small"
}
webwired
Forum Newbie
Posts: 6
Joined: Thu Jun 19, 2008 3:27 pm

Re: PHP CSV Utilities

Post by webwired »

If that's the logic that's being used, then either I'm way over my head or my brain don't function in that manner... :(
Zoxive wrote:
webwired wrote:Well, you should of looked at something before responding then, your post is a waste of everyone's time, especially mine...
I was only sending an array, and I'm not using the single row function, I'm using the multiple row function, which does a foreach with the single row function...
Yes you are, but you are only handing the function a one dimension array, writeRows uses multi dimension.

Which is weird why he classes it as a Mixed variable input for that function, when it expects arrays only.
(The function itself is fine, maybe a force of array, but the input should be array only)
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: PHP CSV Utilities

Post by Zoxive »

Those are single dimension arrays, change it to writeRow(), or have it pass the whole array to writeRows() after the loop, and it should work fine.

What your doing right now, is calling that custom function every loop, which passes just that "Row" of data from the mysql string. And like i said before that data is only a single dimension array.


Also, insulting ones helping (trying) doesn't help out either :lol:
webwired
Forum Newbie
Posts: 6
Joined: Thu Jun 19, 2008 3:27 pm

Re: PHP CSV Utilities

Post by webwired »

Ok, first of all, I would like to apologize for my previous rudeness towards you and also thank you for your assistance...

Second, I tried to pass the whole array to writeRows() after the loop, and got this error...
Warning: Invalid argument supplied for foreach() in /home/webwired/public_html/Csv/Writer.php on line 123
Third, I put the actual code into the while loop, using writeRow() and didn't get an error, in fact, created the csv file, just not a very good one... LOL...
Zoxive wrote:Those are single dimension arrays, change it to writeRow(), or have it pass the whole array to writeRows() after the loop, and it should work fine.

What your doing right now, is calling that custom function every loop, which passes just that "Row" of data from the mysql string. And like i said before that data is only a single dimension array.


Also, insulting ones helping (trying) doesn't help out either :lol:
webwired
Forum Newbie
Posts: 6
Joined: Thu Jun 19, 2008 3:27 pm

Re: PHP CSV Utilities

Post by webwired »

Ok, final code for anyone that may of had the same issue...

Code: Select all

 
$writer = new Csv_Writer('test.csv'); 
$results = mysql_query("SELECT field1, field2 FROM table WHERE 1");
    while ($row = mysql_fetch_assoc($results)) { 
$writer->writeRow($row);                                     
    }
$writer->close(); 
 
And a big thank you to Zoxive and Jcart as well.
Post Reply