kreut Posted August 4, 2011 Share Posted August 4, 2011 Hello, I'm trying to write a CSV file to my server but I neither get a file nor any error. The code is as follows: $fp = fopen('filetest.csv', 'w') or die("can't open file"); //write to a file foreach ($list as $fields) { $new_array =array(); foreach ($fields as $key => $value) { array_push($new_array,$value); } echo print_r($new_array); fputcsv($fp, $new_array); } I stuck the echo print_r($new_array); in there so that you could see what my array looks like. Below is part of the ouput: Array ( [0] => Name [1] => Types of Real Numbers [2] => Variables and Equations [3] => The Number Line and Graphs [4] => Functions ) 1Array ( [0] => Student, Perfect [1] => 100% [2] => 100% [3] => 100% [4] => 100% ) Any help would be appreciated. Thank you, Eric Quote Link to comment Share on other sites More sharing options...
requinix Posted August 4, 2011 Share Posted August 4, 2011 Stick error_reporting(-1); ini_set("display_errors", true); before your code and see if there are any error messages. Also, why are you copying $fields into $new_array? foreach ($list as $fields) fputcsv($fp, $fields); Quote Link to comment Share on other sites More sharing options...
kreut Posted August 4, 2011 Author Share Posted August 4, 2011 Thanks for the quick response! I had already tried printing errors, but I fear that there weren't any (though I tried again for good measure). Also, I created a new array of arrays because the original data was of the form: $array[j] ...in other words, I wanted to separate out the data by rows. Maybe this is where the problem is? -Eric Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 4, 2011 Share Posted August 4, 2011 can you post an example of what you want your .csv file to look like? Quote Link to comment Share on other sites More sharing options...
kreut Posted August 4, 2011 Author Share Posted August 4, 2011 Something like this would be great: Name,Types of Real Numbers| Variables and Equations| The Number Line and Graphs| Functions Student, Perfect| 100%|100%|100%| 100% The ultimate goal is to create this file, then download to a users computer so that they can import it into excel for example. -Eric Quote Link to comment Share on other sites More sharing options...
xyph Posted August 4, 2011 Share Posted August 4, 2011 try var_dump( fputcsv($fp, $new_array) ); Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 4, 2011 Share Posted August 4, 2011 in that case, why not create the excel file for him ? (the less uses have to do anything, the more they'll like your website) Quote Link to comment Share on other sites More sharing options...
xyph Posted August 4, 2011 Share Posted August 4, 2011 Excel is CSV compatible, CSV is a way easier to work with and is compatible on more platforms. Quote Link to comment Share on other sites More sharing options...
kreut Posted August 4, 2011 Author Share Posted August 4, 2011 Still no file..no error...but now I have this output: int(470) int(109) int(126) int(123) int(123) Was this part of error checking? PS I definitely would like the CSV output to keep possibilities open. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 4, 2011 Share Posted August 4, 2011 something like: $output = ''; foreach($new_array as $line){ $output .= implode("|",$line)."\n"; } fputcsv($fp, $output); Quote Link to comment Share on other sites More sharing options...
kreut Posted August 4, 2011 Author Share Posted August 4, 2011 Hello! Thanks to everyone's suggestions, I've almost found my solution. Basically, I took my array and transformed it into a ";" delimeter format. As you can see from the output below, it ALMOST works. I say almost, because the first column on each row is a little "wacky": notice that Name has no quotes around it and the there's a quote at the end of each line as opposed to being part of the next line. The net result is that in excel, all columns look great, but the first column looks like: Name Student, Perfect" Brown, Justina " school, home" school, home" Any additional thoughts on how to fix the first column would be appreciated. Thanks again.... -Eric $fp = fopen('filetest.csv', 'w') or die("can't open file"); //write to a file $output = ''; //start with blank output foreach ($list as $fields) { $new_array =array(); foreach ($fields as $key => $value) { array_push($new_array,$value); } foreach($new_array as $line){ $output .=$line.'|'; /separate each element in the array } $output .="\n"; /start a new line } $output = explode("|",$output); fputcsv($fp, $output,';'); //use the semicolon delimeter Name;"Types of Real Numbers";"Variables and Equations";"The Number Line and Graphs";Functions;"Review of The Language of Algebra";"Quiz #1 ";"Tables, Equations, and Graphs";"The Slope and y-intercept of a line";"Standard Form of a Line";"Linear Inequalities";"Solving Linear Systems of Equations";"Parallel and Perpendicular Lines";"Review of Linear Relationships";"Quiz #2";"Distance From the Origin";"Distance Between Two Points";"Absolute Value Inequalities";Total;" Student, Perfect";100%;100%;100%;100%;100%;100%;100%;100%;100%;100%;100%;100%;100%;100%;100%;100%;100%;100%;" Brown, Justina ";0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;" school, home";0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;" school, home";0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;0.00%;" " Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.