Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = 'localhost';
- $db = 'atstest';
- $user = '******';
- $pass = '******';
- mysql_connect($host,$user,$pass) or die("Could not connect to MySQL server!");
- mysql_select_db($db) or die("Could not select database!");
- $output = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+'); //makes a temporary file
- $columns = array('EventID','EventType','Name','Description','DateTime','Cancelled'); //specifies the CSV columns
- fputcsv($output, $columns);
- $query = mysql_query("SELECT * FROM `Event`");
- $counter = 0;
- while ($data = mysql_fetch_assoc($query)){
- $evID = $data['ID'];
- $evType = $data['EventTypeID'];
- $evName = $data['Name'];
- $evDesc = $data['Description'];
- $evDateTime = $data['DateTime'];
- $evCancelled = $data['Canceled'];
- // If EventType = Concert exported EventName is UPPERCASE
- if ($evType == 1)
- {
- $evName = strtoupper($evName);
- }
- // If EventType = Festival and Canceled = 1 exported Event.DateTime = CANCELLED
- if ($evType == 2 && $evCancelled == 1)
- {
- $evDateTime = 'CANCELLED';
- }
- // If EventType = SoccerMatch exported Event.DateTime show DD Month YYYY
- if ($evType == 3)
- {
- $evDateTime = null;
- }
- $expData = array($evID,getTypeLabel($evType),$evName,$evDesc,$evDateTime,$evCancelled);
- fputcsv($output, $expDdata);
- }
- function getTypeLabel($idNum)
- {
- $typeQuery = "SELECT Label FROM EventType WHERE ID=" . $idNum;
- $typeResult = mysql_query($typeQuery);
- $rowResult = mysql_fetch_assoc($typeResult);
- return $rowResult ['Label'];
- }
- rewind($output);
- $export = stream_get_contents($output);
- fclose($output);
- header('Content-type: application/octet-stream');
- header('Content-Disposition: attachment; filename="events.csv"');
- echo $export;
- mysql_close();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement