Advertisement
Guest User

Untitled

a guest
May 18th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <system.web>
  5. <globalization responseEncoding="utf-8" requestEncoding="utf-8" fileEncoding="utf-8" />
  6. </system.web>
  7. </head>
  8. <body>
  9. <?php
  10.  
  11. require 'functions.inc.php';
  12.  
  13. //EDIT YOUR MySQL Connection Info:
  14. $DB_Server = "mydb15.surftown.dk"; //your MySQL Server
  15. $DB_Username = "plougm1_kreds"; //your MySQL User Name
  16. $DB_Password = "forbund3"; //your MySQL Password
  17. $DB_DBName = "plougm1_kredslejr"; //your MySQL Database Name
  18. $DB_TBLName = "users"; //your MySQL Table Name
  19. //$DB_TBLName, $DB_DBName, may also be commented out & passed to the browser
  20. //as parameters in a query string, so that this code may be easily reused for
  21. //any MySQL table or any MySQL database on your server
  22.  
  23. //DEFINE SQL QUERY:
  24. //you can use just about ANY kind of select statement you want -
  25. //edit this to suit your needs!
  26. $sql = "Select user_name, user_imu from $DB_TBLName";
  27.  
  28. //Optional: print out title to top of Excel or Word file with Timestamp
  29. //for when file was generated:
  30. //set $Use_Titel = 1 to generate title, 0 not to use title
  31. $Use_Title = 0;
  32. //define date for title: EDIT this to create the time-format you need
  33. $now_date = date('m-d-Y H:i');
  34. //define title for .doc or .xls file: EDIT this if you want
  35. $title = "Data hentet fra tabellen $DB_TBLName fra databasen $DB_DBName $now_date";
  36. /*
  37.  
  38. Leave the connection info below as it is:
  39. just edit the above.
  40.  
  41. (Editing of code past this point recommended only for advanced users.)
  42. */
  43. //create MySQL connection
  44. $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
  45. or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
  46. //select database
  47. $Db = @mysql_select_db($DB_DBName, $Connect)
  48. or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
  49. //execute query
  50. $result = @mysql_query($sql,$Connect)
  51. or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
  52.  
  53. //if this parameter is included ($w=1), file returned will be in word format ('.doc')
  54. //if parameter is not included, file returned will be in excel format ('.xls')
  55. if (isset($w) && ($w==1))
  56. {
  57. $file_type = "msword";
  58. $file_ending = "doc";
  59. }else {
  60. $file_type = "vnd.ms-excel";
  61. $file_ending = "xls";
  62. }
  63. //header info for browser: determines file type ('.doc' or '.xls')
  64. header("Content-Type: application/$file_type charset=utf-8");
  65. header("Content-Disposition: attachment; filename=Deltagerliste.$file_ending");
  66. header("Pragma: no-cache");
  67. header("Expires: 0");
  68.  
  69. /* Start of Formatting for Word or Excel */
  70.  
  71. if (isset($w) && ($w==1)) //check for $w again
  72. {
  73. /* FORMATTING FOR WORD DOCUMENTS ('.doc') */
  74. //create title with timestamp:
  75. if ($Use_Title == 1)
  76. {
  77. echo("$title\n\n");
  78. }
  79. //define separator (defines columns in excel & tabs in word)
  80. $sep = "\n"; //new line character
  81.  
  82. while($row = mysql_fetch_row($result))
  83. {
  84. //set_time_limit(60); // HaRa
  85. $schema_insert = "";
  86. for($j=0; $j<mysql_num_fields($result);$j++)
  87. {
  88. //define field names
  89. $field_name = mysql_field_name($result,$j);
  90. //will show name of fields
  91. $schema_insert .= "$field_name:\t";
  92. if(!isset($row[$j])) {
  93. $schema_insert .= "NULL".$sep;
  94. }
  95. elseif ($row[$j] != "") {
  96. $schema_insert .= "$row[$j]".$sep;
  97. }
  98. else {
  99. $schema_insert .= "".$sep;
  100. }
  101. }
  102. $schema_insert = str_replace($sep."$", "", $schema_insert);
  103. $schema_insert .= "\t";
  104. print(trim($schema_insert));
  105. //end of each mysql row
  106. //creates line to separate data from each MySQL table row
  107. print "\n----------------------------------------------------\n";
  108. }
  109. }else{
  110. /* FORMATTING FOR EXCEL DOCUMENTS ('.xls') */
  111. //create title with timestamp:
  112. if ($Use_Title == 1)
  113. {
  114. echo("$title\n");
  115. }
  116. //define separator (defines columns in excel & tabs in word)
  117. $sep = "\t"; //tabbed character
  118.  
  119. //start of printing column names as names of MySQL fields
  120. for ($i = 0; $i < mysql_num_fields($result); $i++)
  121. {
  122. echo mysql_field_name($result,$i) . "\t";
  123. }
  124. print("\n");
  125. //end of printing column names
  126.  
  127. //start while loop to get data
  128. while($row = mysql_fetch_row($result))
  129. {
  130. //set_time_limit(60); // HaRa
  131. $schema_insert = "";
  132. for($j=0; $j<mysql_num_fields($result);$j++)
  133. {
  134. if(!isset($row[$j]))
  135. $schema_insert .= "NULL".$sep;
  136. elseif ($row[$j] != "")
  137. {
  138. $value = $row[$j];
  139. $replace = array("&aring;" => "å","&oslash;" => "ø","&Oslash;" => "Ø","&aelig;" => "æ","&Aring;" => "Å","&AElig;" => "Æ");
  140. $strRpl = str_replace_assoc($replace,$value);
  141. $schema_insert .= "$strRpl".$sep;
  142. }
  143. else
  144. $schema_insert .= "".$sep;
  145. }
  146. $schema_insert = str_replace($sep."$", "", $schema_insert);
  147. //following fix suggested by Josue (thanks, Josue!)
  148. //this corrects output in excel when table fields contain \n or \r
  149. //these two characters are now replaced with a space
  150. $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
  151. $schema_insert .= "\t";
  152. print(trim($schema_insert));
  153. print "\n";
  154. }
  155. }
  156.  
  157. ?>
  158. </body>
  159. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement