Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4565

Re: list() Struktur auslagern

Message-ID <60348188.5pjnEbAIS9@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-01-22 07:00 +0100
Subject Re: list() Struktur auslagern
Newsgroups comp.lang.php
References <4f16f4b4$1_1@news.tiscalinet.ch>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


Maximilian Koch wrote:

> ich m�chte aus einer CSV datei die inhalte auslesen, diese k�nnen sich
> jedoch mal �ndern in der reihenfolge. und es wird danach umst�ndlich an
> mehreren orten im PHP Script die sache zu �ndern..

fgetcsv() und auch Dein Ansatz liefern Dir ein Array.  Du kannst *einmalig* 
den Wert eines Array-Elements einer Variablen zuweisen, und dann im Script 
die Variable verwenden.  Alternativ kannst Du *einmalig* Konstanten oder 
Eigenschaften definieren, welche als Wert den jeweiligen Array-Index haben 
(ich empfehle hier einen objektorientierten Ansatz mit Klassenkonstanten, 
oder Eigenschaften, wenn es flexibler sein muss), und dann verwendest Du die 
Konstante oder Eigenschaftsreferenz als Array-Index.  Zum Beispiel so 
(ungetestet, aber in dieser Form bewährt):

class Import
{
  protected $_fields = array(
    0 => 'country',
    1 => 'sku_ch'
  );

  public function import($file)
  {
    $mapper = ArticleMapper::getInstance();

    $mapper->startTransaction();

    while (($data = fgetcsv($file)) !== false)
    {
      for ($i = count($data); $i--;)
      {
        $data[$this->_fields[i]] = $data[i];
      }

      $article = new Article($data);

      try
      {
        $mapper->save($article);
      }
      catch (Exception $e)
      {
        $mapper->rollBack();
        break;
      }
    }

    if ($data === false && !$mapper->commit())
    {
      $mapper->rollBack();
    }
  }
}

> g�be es nicht eine m�glichkeit diese auszulegen..

"auszulegen"?  Bitte erst lesen, dann denken, dann posten.

BTW: de.comp.lang.php existiert.

BTW2: Deine Umlaute sind kapott, lies bitte vor weiteren Postings 
<http://oe-faq.de/>.


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

list() Struktur auslagern "Maximilian Koch" <xlarge@vtxmail.ch> - 2012-01-18 17:34 +0100
  Re: list() Struktur auslagern Erwin Moller <erwinmollerusenet@xs4all.nl> - 2012-01-18 20:44 +0100
    Re: list() Struktur auslagern Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-22 07:19 +0100
  Re: list() Struktur auslagern Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-22 07:00 +0100

csiph-web