setOutputEncoding('CP1251');
// Read in the excel file.
$data->read($_FILES['excelFile']['tmp_name']);
// Create a connection to the database.
$link = mysql_connect($conf['dbhost'], $conf['dbuser'], $conf['dbpass']);
mysql_select_db($conf['dbname']);
// Delete any listings already in there (this will prevent multiple instances!)
$sql = 'DROP TABLE IF EXISTS `listings`';
$sql2 = 'CREATE TABLE `listings` (
`id` int(11) NOT NULL auto_increment,
`address` varchar(255) collate latin1_general_ci NOT NULL,
`apt` varchar(5) collate latin1_general_ci NOT NULL,
`bedrooms` tinyint(1) NOT NULL,
`bath` tinyint(1) NOT NULL,
`city` varchar(255) collate latin1_general_ci NOT NULL,
`furnished` varchar(1) collate latin1_general_ci NOT NULL,
`disposal` varchar(1) collate latin1_general_ci NOT NULL,
`ac` varchar(1) collate latin1_general_ci NOT NULL,
`dishw` varchar(1) collate latin1_general_ci NOT NULL,
`balcony` varchar(1) collate latin1_general_ci NOT NULL,
`patio` varchar(1) collate latin1_general_ci NOT NULL,
`parking` varchar(1) collate latin1_general_ci NOT NULL,
`available` varchar(1) collate latin1_general_ci NOT NULL,
`path` varchar(255) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1112 ';
mysql_query($sql);
mysql_query($sql2);
echo 'Creating table: \'listings\'....';
// Loop through the excel data to build the sql queries used to insert the data
// into the database.
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++)
{
// Create a container for the data.
$listingData = array();
// Loop through the excel data.
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
{
// Store the value of the current cell.
$temp = $data->sheets[0]['cells'][$i][$j];
if (is_null($temp))
{
// If the current cell is null set it to 'n'
$temp = 'n';
}
// Store the value in the container.
$listingData[] = $temp;
}
// Create a sql statement to insert the current excel row.
$sql = "insert into listings (address, apt, bedrooms, bath, city,
furnished, disposal, ac, dishw, balcony,
patio, parking, available, path)
values('$listingData[0]', '$listingData[1]', '$listingData[2]',
'$listingData[3]', '$listingData[4]', '$listingData[5]',
'$listingData[6]', '$listingData[7]', '$listingData[8]',
'$listingData[9]', '$listingData[10]', '$listingData[11]', '$listingData[12]', '$listingData[13]');";
// Attempt to insert the data.
$result = mysql_query($sql);
// If there was an error display it.
if (!$result)
{
$reason = mysql_error() . "
" . $sql;
include_once 'uploadFailed.tpl';
exit;
}
}
// Show a successful upload.
include_once 'uploadSuccessful.tpl';
}
}
?>