Page 2 of 2
Re: PHP CSV Utilities
Posted: Thu Mar 20, 2008 11:44 am
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) {
}
Re: PHP CSV Utilities
Posted: Thu Mar 20, 2008 4:29 pm
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"
Re: PHP CSV Utilities
Posted: Thu Mar 20, 2008 6:05 pm
by Christopher
I think both. You want to give flexibility to organize the code in any order -- if possible.
Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 3:31 pm
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
Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 4:17 pm
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.
Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 4:22 pm
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...
Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 4:28 pm
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.
Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 4:32 pm
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)
Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 4:45 pm
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"
}
Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 4:48 pm
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)
Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 4:51 pm
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

Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 5:09 pm
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

Re: PHP CSV Utilities
Posted: Thu Jun 19, 2008 5:24 pm
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.