PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > using fputcsv()
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
using fputcsv()

Réponse
 
LinkBack Outils de la discussion
Vieux 08/11/2007, 16h20   #1
shenoyvikram@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut using fputcsv()

New to PHP
I am running a loop searching a csv file. When I have a match I am
trying to replace the matched line with new data.
However everytime I do the replace, there is an empty line in the
file.
- Does fputcsv insert or replace? ( I am using fseek to move to the
appropriate location in the file to write)

while( ($data = fgetcsv($fid, 1000, ",")) !== FALSE) {
if (strcmp ($data[0], $var) == 0){
fseek ($fid,$offset_counter);
fputcsv($fid, $data);
$offset_counter = ftell ($fid);
}

my file starts like this
11,11,11,11
22,22,22,22
33,33,33,33

Should end up like this
11,11,11,11
33,22,22,22
33,33,33,33

but instead looks like this
11,11,11,11
33,22,22,22

33,33,33,33

  Réponse avec citation
Vieux 08/11/2007, 21h16   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using fputcsv()

On Thu, 08 Nov 2007 17:20:35 +0100, <shenoyvikram@yahoo.com> wrote:

> New to PHP
> I am running a loop searching a csv file. When I have a match I am
> trying to replace the matched line with new data.
> However everytime I do the replace, there is an empty line in the
> file.
> - Does fputcsv insert or replace? ( I am using fseek to move to the
> appropriate location in the file to write)
>
> while( ($data = fgetcsv($fid, 1000, ",")) !== FALSE) {
> if (strcmp ($data[0], $var) == 0){
> fseek ($fid,$offset_counter);
> fputcsv($fid, $data);
> $offset_counter = ftell ($fid);
> }
>
> my file starts like this
> 11,11,11,11
> 22,22,22,22
> 33,33,33,33
>
> Should end up like this
> 11,11,11,11
> 33,22,22,22
> 33,33,33,33
>
> but instead looks like this
> 11,11,11,11
> 33,22,22,22
>
> 33,33,33,33


RTFM:
fputcsv() formats a line (passed as a fields array) as CSV and write it
(terminated by a newline) to the specified file handle.

It's because the csv line is terminated by a newline charecter. Try to
write a single in a new file: it will contain this:
"1,2,3
"
Not:
"1,2,3"
--
Rik Wasmus
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h26.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10329 seconds with 10 queries