Paste: x

Author: x
Mode: php
Date: Fri, 19 Mar 2010 03:26:34
Plain Text |
<?php

session_start();

if(isset($_REQUEST['export'])) {
	header('Content-type: text/plain');
	header('Content-Disposition: attachment; filename="export.csv"');
	readfile($_SESSION['export']);
	exit;
}

?><!doctype html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8">
<title>Compare</title>
<style type="text/css">
body { font-family: sans-serif; }
p.error { font-weight: bold; color: #FF3300; }
p.success { font-weight: bold; color: #339933; }
td.letter { font-weight: bold; font-size: 350%; padding: 0 0.3em 0 0; }
textarea { width: 30em; height: 4em; }
div.results { margin: 0 0 0 4em; }
</style>
</head>
<body>
<h1 style="display: inline;">Compare</h1>

<?php

function array_diff_inverse($a, $b) {
	$c = array_diff($a, $b);
	$c = array_intersect($c, $a);
	return $c;
}

if((isset($_REQUEST['a']) && isset($_REQUEST['b'])) && ($_REQUEST['a'] !== '' && $_REQUEST['b'] !== '')) {
	$a = explode("\r\n", $_REQUEST['a']);
	$a_fields = $a[0];
	$a_fields = explode("\t", $a_fields);
	
	$b = explode("\r\n", $_REQUEST['b']);
	$b_fields = $b[0];
	$b_fields = explode("\t", $b_fields);
	
	$a_key_html = 'Key field for <b>A</b>: <select name="a_key_sel">';
	foreach($a_fields as $field) {
		if(!isset($_REQUEST['a_key_sel'])) $_REQUEST['a_key_sel'] = null;
		if($field == $_REQUEST['a_key_sel']) $selected = ' selected'; else $selected = null;
		$a_key_html .= '<option value="'.$field.'"'.$selected.'>'.$field.'</option>';
	}
	$a_key_html .= '</select>';
	
	$b_key_html = 'Key field for <b>B</b>: <select name="b_key_sel">';
	foreach($b_fields as $field) {
		if(!isset($_REQUEST['b_key_sel'])) $_REQUEST['b_key_sel'] = null;
		if($field == $_REQUEST['b_key_sel']) $selected = ' selected'; else $selected = null;
		$b_key_html .= '<option value="'.$field.'"'.$selected.'>'.$field.'</option>';
	}
	$b_key_html .= '</select>';
}

if(isset($_REQUEST['a_key_sel'])) $a_key_sel = $_REQUEST['a_key_sel'];
if(isset($_REQUEST['b_key_sel'])) $b_key_sel = $_REQUEST['b_key_sel'];

if((isset($_REQUEST['a']) && isset($_REQUEST['b'])) && ($_REQUEST['a'] !== '' && $_REQUEST['b'] !== '')) {
	
	$a = explode("\r\n", $_REQUEST['a']);
	if(!isset($a_key_sel)) $a_key_sel = null;
	$a_key = array_search($a_key_sel, $a_fields);
	$a = array_slice($a, 1);
	foreach($a as $line) {
		$bits = explode("\t", $line);
		if($bits[0] == '') continue;
		$a_keys[] = $bits[0];
		foreach($bits as $bit) {
			$a_master[$bits[$a_key]][] = $bit;
		}
	}
	
	$b = explode("\r\n", $_REQUEST['b']);
	if(!isset($b_key_sel)) $b_key_sel = null;
	$b_key = array_search($b_key_sel, $b_fields);
	$b = array_slice($b, 1);
	foreach($b as $line) {
		$bits = explode("\t", $line);
		if($bits[0] == '') continue;
		$b_keys[] = $bits[0];
		foreach($bits as $bit) {
			$b_master[$bits[$b_key]][] = $bit;
		}
	}
}

if(!isset($_REQUEST['a'])) $_REQUEST['a'] = null;
if(!isset($_REQUEST['b'])) $_REQUEST['b'] = null;
if(!isset($a_key_html)) $a_key_html = null;
if(!isset($b_key_html)) $b_key_html = null;

if($_REQUEST['a'] == null && $_REQUEST['b'] == null) {
	echo '<p>Paste data from two Excel files into the fields below and click <b>Process</b>.  Note that the first row of data should consist of field names.</p>';
	$button = '<input type="submit" value="Process">';
}
else if($_REQUEST['a_key_sel'] == null && $_REQUEST['b_key_sel'] == null) {
	echo '<p>Next, select the key fields from each dataset and click <b>Compare</b>.</p>';
	$button = '<input type="submit" value="Compare">';
}
else {
	echo '<p>Select one of the buttons below to display the desired comparison.</p>';
	$button = '<p style="margin: 1em 0;"><b>Comparison options:</b></p><table>
	<tr><td align="right">Display records within A that are not within B:</td><td><input type="submit" name="in_a_not_b" value="Display"></td>
	<tr><td align="right">Display records within B that are not within A:</td><td><input type="submit" name="in_b_not_a" value="Display"></td>
	<tr><td align="right">Display records present in both A and B:</td><td><input type="submit" name="in_both" value="Display"></td>
	</table>';
}

echo '<form action="?" method="post">
<table>
<tr><td class="letter">A</td><td><textarea name="a">'.$_REQUEST['a'].'</textarea></td><td style="padding: 0 0 0 0.5em;">'.$a_key_html.'</td></tr>
<tr><td class="letter">B</td><td><textarea name="b">'.$_REQUEST['b'].'</textarea></td><td style="padding: 0 0 0 0.5em;">'.$b_key_html.'</td></tr>
<tr><td></td><td>'.$button.'</td></tr>
</table>
</form>';

$export = null;

if(isset($_REQUEST['in_a_not_b'])) {
	echo '<div class="results"><h2>Result</h2>';
	$result = @array_diff_inverse($a_keys, $b_keys);
	if(count($result) == 0 || !isset($a_master[current($result)])) {
		echo '<p class="error">There are no records within A that are not within B for the specified keys.</p>';
		exit;
	}
	echo '<p class="success">Records within A that are not within B: <a href="?export">(Export)</a></p><table border="1"><tr>';
	foreach($a_fields as $field) {
		echo '<td><b>'.$field.'</b></td>';
		$export .= $field.',';
	}
	$export .= "\n";
	echo '</tr>';
	foreach($result as $match) {
		echo '<tr>';
		foreach($a_master[$match] as $item) {
			echo '<td>'.$item.'</td>';
			$export .= $item.',';
		}
		echo '</tr>';
		$export .= "\n";
	}
	echo '</table>';
	$fn = md5(time()).'.csv';
	$_SESSION['export'] = $fn;
	file_put_contents($fn, $export);
}

if(isset($_REQUEST['in_b_not_a'])) {
	echo '<div class="results"><h2>Result</h2>';
	$result = @array_diff_inverse($b_keys, $a_keys);
	if(count($result) == 0 || !isset($b_master[current($result)])) {
		echo '<p class="error">There are no records within B that are not within A for the specified keys.</p>';
		exit;
	}
	echo '<p class="success">Records within B that are not within A: <a href="?export">(Export)</a></p><table border="1"><tr>';
	foreach($b_fields as $field) {
		echo '<td><b>'.$field.'</b></td>';
		$export .= $field.',';
	}
	echo '</tr>';
	$export .= "\n";
	foreach($result as $match) {
		echo '<tr>';
		foreach($b_master[$match] as $item) {
			echo '<td>'.$item.'</td>';
			$export .= $item.',';
		}
		echo '</tr>';
		$export .= "\n";
	}
	echo '</table>';
	$fn = md5(time()).'.csv';
	$_SESSION['export'] = $fn;
	file_put_contents($fn, $export);
}

if(isset($_REQUEST['in_both'])) {
	echo '<div class="results"><h2>Result</h2>';
	$result = @array_intersect($a_keys, $b_keys);
	if(count($result) == 0 || !isset($a_master[current($result)])) {
		echo '<p class="error">All records from both datasets are unique for the specified keys.</p>';
		exit;
	}
	echo '<p class="success">Records present in both A and B: <a href="?export">(Export)</a></p><table border="1"><tr>';
	foreach($a_fields as $field) {
		echo '<td><b>'.$field.'</b></td>';
		$export .= $field.',';
	}
	echo '</tr>';
	$export .= "\n";
	foreach($result as $match) {
		echo '<tr>';
		foreach($a_master[$match] as $item) {
			echo '<td>'.$item.'</td>';
			$export .= $item.',';
		}
		echo '</tr>';
		$export .= "\n";
	}
	echo '</table>';
	$fn = md5(time()).'.csv';
	$_SESSION['export'] = $fn;
	file_put_contents($fn, $export);
}

?>

</body>
</html>

New Annotation

Summary:
Author:
Mode:
Body: