ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); date_default_timezone_set('Europe/London'); if (PHP_SAPI == 'cli') { die('This example should only be run from a Web Browser'); } /** Include PHPExcel */ //require_once dirname(__FILE__) . '../Classes/PHPExcel.php'; require "../Classes/PHPExcel.php"; require "dbcon.php"; require "../models/SamplesModel.php"; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("GRC Tech")->setLastModifiedBy("GRC Tech")->setTitle("Office 2007 XLSX Hive Data")->setSubject("Office 2007 XLSX Hive Data")->setDescription("GRC Hive Data")->setKeywords("Bee Hive Honey Mites")->setCategory("Bee Hive"); $db = writeDatabase(); $samplesModel = new SamplesModel($db); $sampleList = $samplesModel->getAllSamples(); //Set Row Titles $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', 'Hive Name')->setCellValue('B1', 'Mites')->setCellValue('C1', 'Sample Period')->setCellValue('D1', 'Date Recorded'); $rowCount = 2; // Add some data while ($row = $sampleList->fetch(PDO::FETCH_ASSOC)) { $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A' . $rowCount, $row['hive_id'])->setCellValue('B' . $rowCount, $row['num_mites'])->setCellValue('C' . $rowCount, $row['sample_period'])->setCellValue('D' . $rowCount, $row['collection_date']); $rowCount++; } // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('MiteCount'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client’s web browser (Excel2007)
<?php require "dbcon.php"; $hivename = $_POST['hivename']; $observationdate = $_POST['observationdate']; $duration = $_POST['duration']; $mitecount = $_POST['mitecount']; //Establish database connection $conn = writeDatabase(); $stmt = $conn->prepare("INSERT INTO samples(hive_id, collection_date, sample_period, num_mites)VALUES(:name, :observationdate, :duration, :mitecount)"); $stmt->bindParam(':name', $hivename); $stmt->bindParam(':observationdate', $observationdate); $stmt->bindParam(':duration', $duration); $stmt->bindParam(':mitecount', $mitecount); //Execute query $stmt->execute(); //$stmt->close(); //$connection->close(); //if the query is successful, notify the user echo $hivename . " Data Recorded Successfully!"; $stmt = $conn->prepare("SELECT hive_id, num_mites, collection_date FROM samples WHERE hive_id = '{$hivename}'"); $stmt->execute(); echo "<table>"; echo "<tr>"; echo "<th>Hive ID</th>"; echo "<th>Mites</th>"; echo "<th>Sample Date</th>"; echo "</tr>"; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo "<tr>"; echo "<td>" . $row['hive_id'] . "</td>";