Databug

Sunday, July 16, 2006

Added Street Name to Profile

Added a function to grab a random street name from a new data file. Only viable file of street names I could find contained street names from Anchorage, Alaska. For the most part, it doesn't really show.

A demo:

Array
(
[first_name] => Othella
[last_name] => Finch
[user_name] => ofinch
[full_name] => Othella Finch
[gender] => f
[email] => ofinch49788@aol.com
[street] => Altoona Drive #654
[zip] => 91360
[city] => Thousand Oaks
[state] => CA
[phone] => (858) 555-9667
)

 

Tuesday, July 11, 2006

Method: _databug_thresh_file($fpath, $ratio_denominator=3)

I'm trying to make the class workable without a database because I'm lazy and find integrating them in my PHP scripts a bit of a nuisance and, well, I just find it tidier not to get MySQL involved sometimes.

There are a couple methods in the class for opening files and pulling data into an array. Most involve some randomization of the result. So I wrote this method to lighten the load. The $ratio_denominator argument indicates the ratio of lines (as in 1 in every $ratio_denominator lines) that will be pulled into the array (i.e. the "threshing"). The same concept is used with set_zip_data_randomly_by_range function.

/**
* @access private
* @return array
*/
function _databug_thresh_file($fpath, $ratio_denominator=3)
{
// *** DATA

# internal
$skip_token = '%';
$_d = $ratio_denominator;
$_i = 0;

# return
$LINES = array();


// *** MANIPULATE

# sanity check
if ( !is_file($source_path) )
{
trigger_error("file [$source_path] not found", E_USER_WARNING);
return 0;
}

# open file (for reading)
$_handle = @fopen($source_path, "r");

# Set Mod Offset
$_mod_offset = mt_rand(0, $_d-1);

# fetch file lines
while ( !feof($_handle) )
{
$_i++;
if ( $_i % $_d <> $_mod_offset )
{
continue;
}

$_buffer = fgets($_handle, 4096);
$_line = trim($_buffer);

# check for skip token
if ( substr($_line,0,1) <> $skip_token && !empty($_line) )
{
$LINES[] = $_line;
}
}

# close file
fclose($_handle);

# DEBUG
#print_r($LINES);

# catch
if ( !count($LINES) )
{
trigger_error('no lines found', E_USER_WARNING);
}
elseif ( count($LINES) == 1 )
{
trigger_error('only 1 line fetched -> check EOL delimiter', E_USER_NOTICE);
}

// *** RETURN

return $LINES;
}

 

Friday, July 07, 2006

Waldo Kent from Cleveland, OH

Doing a little more work on Databug.

Test case: Clark Kent's underachieving cousin.

Array
(
[first_name] => Waldo
[last_name] => Kent
[user_name] => wkent
[full_name] => Waldo Kent
[gender] => m
[email] => wkent12609@sbcglobal.net
[zip] => 44110
[city] => Cleveland
[state] => OH
[phone] => (212) 555-0080
)