PHP Classes

File: example1.php

Recommend this page to a friend!
  Classes of Ignatius Teo   Sample Data Generator   example1.php   Download  
File: example1.php
Role: Example script
Content type: text/plain
Description: Basic example
Class: Sample Data Generator
Generating random sample data based on rules
Author: By
Last change:
Date: 19 years ago
Size: 4,853 bytes


Class file image Download
 * example1.php - Basic example
 * random data sample generator
 * $Id: example1.php,v 1.3 2004/12/04 00:01:54 Owner Exp $
include "tsample.php";

// some sample data
$firstnames = array (
"Adam", "Bob", "Charles", "Dave", "Edgar", "Fred", "Greg", "Harry", "Ian", "James", "Kingsley",
"Larry", "Michael", "Neil", "Oscar", "Patrick", "Quentin", "Roger", "Steve", "Tom", "Uriah",
"Victor", "Walter", "Xander", "Yanik", "Zak"

$lastnames = array(
"Ackerman", "Barry", "Connor", "Donohue", "Elliott", "Fitzpatrick",
"Goldsworthy", "Hodgkinson", "Inch", "Jones", "Kelly", "Lynch",
"Murphy", "Nielson", "O'Neill", "Phillips", "Quinn", "Ryan",
"Smith", "Tucker", "Unwin", "Voss", "Waugh", "Xavier", "Yeoman", "Zachariah"

$streets = array(
"First St", "Second St", "Third St", "Fourth St", "Fifth St",
"Sixth Ave", "Seventh Ave", "Eigth Ave", "Ninth Ave", "Tenth Ave",
"Oak Court", "Maple Court", "Red Gum Court", "Teak Court", "Jarrah Court", "Mahogany Court",
"Poplar Circuit", "Conifer Circuit", "Fir Circuit", "Elm Circuit", "Pine Circuit",
"Cedar Grove", "Box Grove", "Olive Grove",

$cities = array(
"Charlotte", "Kansas City", "Orlando", "Cincinnati", "Jackson", "Pittsburgh", "Cleveland",
"Lancaster", "Plattsburgh", "Des Moines", "Louisville", "Portland", "Denver", "Los Angeles",
"Raleigh", "Detroit", "Madison", "Sacramento", "Durham", "Miami", "San Antonio",
"Fort Smith", "Milwaukee", "San Diego", "Greensboro", "Minneapolis", "Spokane",
"Greenville", "Monterey", "Tampa", "Houston", "New Orleans", "West Palm Beach", "Honolulu",
"Oklahoma City", "Wilmington", "Indianapolis", "Omaha", "Jacksonville", "Orlando"

$states = array(
"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado",
"Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia",
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana",
"Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",
"Oregon", "Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina",
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virgin Islands",
"Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"

$ad_methods = array(
"Website", "Banner Ad", "Friend", "Magazine", "Newspaper", "Radio", "TV"

$flavors = array(
"Vanilla", "Chocolate", "Strawberry", "Banana", "Macadamia", "Pistachio", "Honey Nougat",
"Mint", "Blueberry", "Coffee", "Cappucino", "Mocha", "Choc-chip", "Butterscotch", "Peppermint"

$colors = array(
"Blue", "Green", "Red", "Yellow", "Orange", "Purple", "Indigo", "Magenta", "Cyan",
"Teal", "Navy", "Black", "White"

 * this is the guts of it!
 * to create a sample recordset, you basically
 * write a "ruleset" to determine what the records
 * will contain

$rule_set = array(
// one way to generate some real names...
"firstname" => array("choice", $firstnames),
"lastname" => array("choice", $lastnames),

// inserting a value as-is
"gender" => array("as_is", "male"),
// or just leave it blank
"blank" => "",

// the current datetime
"cdate" => array("datetime"),

// random birthdate between the ages of 16 to 90
"birthdate" => array("dob", 16, 90),

// a datetime between 1 Jan 2000 and 31 Dec 2003
    // NOTE: you can only specify dates after 1 Jan 1970 - obviously...
"reg_date" => array("datetime", mktime(0,0,0,1,1,2000), mktime(23,59,59,12,31,2003)),

// one way to populate addresses - well, it is only sample data :-)
"address" => array("choice", $streets),
"city" => array("choice", $cities),
"state" => array("choice", $states),

// one way to specify zip codes and phone numbers
"zip" => array("range", 40000, 89999),
"phone" => array("range", 5550000, 5999999),

// another way to generate a random 8-digit number
"cust_id" => array("number", 8),

// choose none or 1 option
"referred" => array("choice", $ad_methods, 0, 1),

// choose only 3 options
"flavor" => array("choice", $flavors, 3),

// choose 1 to 7 options
"color" => array("choice", $colors, 1, 7),

// now simply call the generate method using the ruleset
// to generate a specified number of records
print "<pre>";
print_r(TSample::generate($rule_set, 10));