Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.78 KB | None | 0 0
  1. <?php
  2.  
  3. $host = 'localhost';
  4. $db = 'atstest';
  5. $user = '******';
  6. $pass = '******';
  7.  
  8. mysql_connect($host,$user,$pass) or die("Could not connect to MySQL server!");
  9.  
  10. mysql_select_db($db) or die("Could not select database!");
  11.  
  12. $output = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');  //makes a temporary file
  13. $columns = array('EventID','EventType','Name','Description','DateTime','Cancelled'); //specifies the CSV columns
  14.  
  15. fputcsv($output, $columns);
  16.  
  17. $query = mysql_query("SELECT * FROM `Event`");
  18. $counter = 0;
  19. while ($data = mysql_fetch_assoc($query)){
  20.  
  21.     $evID = $data['ID'];
  22.      $evType = $data['EventTypeID'];
  23.      $evName = $data['Name'];
  24.      $evDesc = $data['Description'];
  25.      $evDateTime = $data['DateTime'];
  26.      $evCancelled = $data['Canceled'];
  27.      
  28.      // If EventType = Concert exported EventName is UPPERCASE
  29.      if ($evType == 1)
  30.        {
  31.        $evName = strtoupper($evName);
  32.        }
  33.    
  34.     // If EventType = Festival and Canceled = 1 exported Event.DateTime = CANCELLED
  35.     if ($evType == 2 && $evCancelled == 1)
  36.        {
  37.        $evDateTime = 'CANCELLED';
  38.        }
  39.    
  40.     // If EventType = SoccerMatch exported Event.DateTime show DD Month YYYY
  41.     if ($evType == 3)
  42.        {
  43.        $evDateTime = null;
  44.        }
  45.    
  46.     $expData = array($evID,getTypeLabel($evType),$evName,$evDesc,$evDateTime,$evCancelled);
  47.     fputcsv($output, $expDdata);
  48. }
  49.  
  50. function getTypeLabel($idNum)
  51. {
  52.     $typeQuery = "SELECT Label FROM EventType WHERE ID=" . $idNum;
  53.     $typeResult = mysql_query($typeQuery);
  54.     $rowResult = mysql_fetch_assoc($typeResult);
  55.     return $rowResult ['Label'];
  56. }
  57.  
  58. rewind($output);
  59.  
  60. $export = stream_get_contents($output);
  61.  
  62. fclose($output);
  63.  
  64. header('Content-type: application/octet-stream');
  65.  
  66. header('Content-Disposition: attachment; filename="events.csv"');
  67.    
  68. echo $export;
  69.  
  70. mysql_close();
  71.  
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement