Location: PHPKode > scripts > WPhp > wphp/WPhpMergeFile.php
<?php
/*
$Id: WPhpMergeFile.php,v 1.3 2005/04/19 16:19:22 josheli Exp $
Example: see wphp-merge-demo.php
*/
include_once 'WPhpDocument.php';

class WPhpMergeFile extends WPhpDocument 
{

  var $records = array();
  var $fields = array();
  var $_currRec = 0;

  function WPhpMergeFile($field_names = array()) 
  {
    $this->_data = '';
    if(!empty($field_names))
    {
      $this->setFieldNames($field_names);
    }
  }

  function setFieldNames($field_names) 
  {
    if(empty($field_names) || !is_array($field_names))
    {
    	return;
    }
    foreach($field_names as $field_name) 
    {
      $this->addFieldName($field_name);
    }  	
  }

  function addFieldName($field_name) 
  {
    if(!in_array($field_name, $this->fields))
    {
    	$this->fields[] = $field_name;
    }
  }

  function addFieldData($field_name, $field_data, $insert_at = 0) 
  {
    if(!in_array($field_name, $this->fields))
    {
    	return false;
    }

    $insert_at = (int) $insert_at;

    if($insert_at > 0)
    {
    	$record_number = $insert_at;
    }
    else 
    {
      $record_number = $this->_currRec;
    }
    $this->records[$record_number][$field_name] = $field_data;
  }

  function addRecord($record, $insert_at = 0) 
  {
    if(!is_array($record))
    {
      return;
    }

    $insert_at = (int) $insert_at;

    if($insert_at > 0)
    {
    	$record_number = $insert_at;
    }
    else 
    {
      ++$this->_currRec;
      $record_number = $this->_currRec;
    }
    
    foreach($record as $name => $data) 
    {
      $this->addFieldData($name, $data, $record_number);
    }
  }

  function modifyRecord($record, $record_number) 
  {
    $this->addRecord($record, $record_number);
  }

  /*
  override the parent method so we can insert the field names
  */
  function getData() 
  {
    return $this->getFieldNameData().$this->getFieldData();
  }

  function getFieldData() 
  {
    $data = '';

    foreach($this->records as $rec_num => $record)
    {
      foreach($this->fields as $field) 
      {
        $data .= $this->_convertText($record[$field]).MERGE_ENDFIELD.WP_HARD_EOL;
      }
      $data .= MERGE_ENDRECORD.WP_HARD_EOL;
    }

    return $data;
  }

  function getFieldNameData() 
  {
    return MERGE_FIELDNAMES_ON . implode(';', array_map(array(&$this, '_convertText'), $this->fields)) . MERGE_FIELDNAMES_OFF . MERGE_ENDRECORD . WP_HARD_EOP;
  }

}


?>
Return current item: WPhp