Advertisement
Guest User

Untitled

a guest
Apr 14th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. <?php
  2. $db = mysqli_connect("localhost", "tvstar5_gocyw", "123123123", "tvstar5_gocyw");
  3. if(isset($_POST['downloads'])) {
  4. //create query to select as data from your table
  5. $select = "SELECT * FROM cyw_netsignups";
  6.  
  7. //run mysql query and then count number of fields
  8. $export = mysqli_query ( $db, $select )
  9. or die ( "Sql error : " . mysql_error( ) );
  10. $fields = mysqli_num_fields ( $export );
  11.  
  12. //create csv header row, to contain table headers
  13. //with database field names
  14. for ( $i = 0; $i < $fields; $i++ ) {
  15. /* line 481 */ $header .= mysqli_fetch_field( $export ) . ",";
  16. }
  17.  
  18. //this is where most of the work is done.
  19. //Loop through the query results, and create
  20. //a row for each
  21. while( $row = mysqli_fetch_row( $export ) ) {
  22. $line = '';
  23. //for each filepro_fieldcount(oid)d in the row
  24. foreach( $row as $value ) {
  25. //if null, create blank field
  26. if ( ( !isset( $value ) ) || ( $value == "" ) ){
  27. $value = ",";
  28. }
  29. //else, assign field value to our data
  30. else {
  31. $value = str_replace( '"' , '""' , $value );
  32. $value = '"' . $value . '"' . ",";
  33. }
  34. //add this field value to our row
  35. $line .= $value;
  36. }
  37. //trim whitespace from each row
  38. $data .= trim( $line ) . "\n";
  39. }
  40. //remove all carriage returns from the data
  41. $data = str_replace( "\r" , "" , $data );
  42.  
  43.  
  44. //create a file and send to browser for user to download
  45. header("Content-type: application/vnd.ms-excel");
  46. header("Content-disposition: csv" . date("Y-m-d") . ".csv");
  47. header( "Content-disposition: filename=".$file_name.".csv");
  48. print "$header\n$data";
  49. exit;
  50. }
  51.  
  52. ?>
  53. <form action="" method="post">
  54. <input type="submit" name="downloads" value="Download">
  55. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement