<?php
/*
$Id: wphp-merge-demo.php,v 1.3 2005/04/19 16:19:22 josheli Exp $
Demo file using WPhp to dynamically generate a WordPerfect merge data file
*/
include 'WPhpMergeFile.php';
function get_data_from_db()
{
$data[] = array('first name'=>'George', 'last name'=>'Bush', 'address'=>'1600 Penn. Ave.', 'city'=>'Washington', 'state'=>'D.C.');
$data[] = array('first name'=>'Bugs', 'last name'=>'Bunny', 'address'=>'Rabbit Hole Road', 'city'=>'Garden City', 'state'=>'New Jersey');
$data[] = array('first name'=>'Jody', 'last name'=>'Davis', 'address'=>'1000 Home Plate Blvd.', 'city'=>'Chicago', 'state'=>'Illinois');
return $data;
}
//example call to get data
$data = get_data_from_db();
//field names must be set before you add data/records....
$field_names = array_keys($data[0]);
//...set the field names here....
$wp = new WPhpMergeFile($field_names);
//....or set them here...
$wp->setFieldNames($field_names);
//...add records (appends them)....
foreach($data as $record)
{
$wp->addRecord($record);
}
//Overwrite a field in a record
$wp->addFieldData('last name', 'Clooney', 1);
//add a field in a (non-existing) record (will create the record)
$wp->addFieldData('first name', 'Elijah', 4);
//modify a record...
$new_rec = array('first name'=>'Rasmus', 'last name'=>'Lerdorf', 'address'=>'1 PHP Way', 'city'=>'Yahoo', 'state'=>'California');
//...record number 2
$wp->modifyRecord($new_rec, 2);
//optionally save() to disk
$wp->promptDownload('wphp-merge-demo.dat');
?>