Solved

line break problem importing and exporting csv file in/out of database.........

Posted on 2004-08-26
3
1,035 Views
Last Modified: 2008-01-09
Hi, I've got the following script to import data into the database from a textfile....

<?php
include 'connectdb.php'; $countrow=0;
$n=0;
$array = file("import_members.csv");
foreach($array as $row) {
 $record = explode(",",$row);

$sub=$record[0]; $id=$record[1];  $gamertag=$record[2]; $firstname=$record[3]; $lastname=$record[4];$email=$record[5];

   mysql_query("INSERT INTO members (mem_sub, mem_id, mem_gamertag, mem_firstname, mem_lastname, mem_email) VALUES('$sub','$id','$gamertag','$firstname','$lastname','$email')")
   or die (mysql_error());
   $n=$n+1;
}
echo $n." Records were imported to members database successfully......";
?>

And this is how I export data from the databse to another csv file.....

<?
$n=0;
//Connect to database
include 'connectdb.php';

$result=mysql_query("SELECT * FROM members");
$fp = fopen( "exceldata/members.csv" , "w" );
//select all records
while ($row = mysql_fetch_array($result))
{
 $n=$n+1;
 $inputString=$row['mem_email']."\n";
  fwrite( $fp, $inputString );
}
 echo "<br> members.csv updated";
fclose( $fp );
?>

Now the problem is that I'm getting double line breaks in my csv file.....I'm trying to figure out what causes this and I'm confused...Here are a few of observations.......
If I manually enter/modify records, using a form or phpMyAdmin, the output file is fine....
If I open the outpu file with notepad, instead of excl or wordpad, all the rows are on one line...

been fooling around for hours but Just can't  seem to make it work the way i want.........






0
Comment
Question by:skylabel
3 Comments
 
LVL 1

Author Comment

by:skylabel
ID: 11910231
the output file is fine when i omit the ."\n" linebreak
but then if add records manually on top of that, it stays on the same line.....so my output fie would be something like (just to clarify what i mean the first 3 records were imported while the 4h was manually added)

1, john, doe, blaba
2, rank, franky, blala
3, bill, williams, blabla, 4, jane, jameson, blabla
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 11910249
The problem comes from $array = file("import_members.csv");
the lines in that array contain the \n at the end of the last entry

So Try this line
$record = explode(",",trim($row));
for
$record = explode(",",$row);

Trim removes the whitespaces at the end and the beginning line.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 11910284
When you just want to remove the linebreaks at the end of the last element, you can use:



I add this comment because I do not know if your first field might or might not contain leading spaces (or your last field the same with trailing spaces/tabs), which will surely be deleted when using trim().
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question