Jump to content

Writing CSV file to my server


kreut

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/243835-writing-csv-file-to-my-server/
Share on other sites

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

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

 

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%;"

"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.