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.
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.
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) {
}
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 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();
}
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
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...
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.
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)
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...
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)
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
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