User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 402,911 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,097 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2529 | Replies: 109
Reply
Join Date: Feb 2008
Location: Trinidad
Posts: 400
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #51  
Jul 24th, 2008
Originally Posted by R0bb0b View Post
I guess we go with option B then. Delete data and import your csv. The date column should be like this, STR_TO_DATE('13/09/2007', '%d/%m/%Y') .


Where do I use that?
Is it when I am loading the data into the database from the csv file?........

  1. if(count($_FILES) > 0)
  2. {
  3. $ext = "";
  4. $ext = substr(trim($_FILES["file"]["name"]), -4);
  5. $allowedext = array(".txt", ".csv", ".sql");
  6.  
  7. if(in_array($ext, $allowedext))
  8. {
  9. $filename = $_FILES['file']['tmp_name'];
  10. $handle = fopen($filename, "r");
  11.  
  12. while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE)
  13. {
  14. //print_r($data);
  15. //echo("<br />");
  16. $value1 = $data[0];
  17. $value2 = $data[1];
  18. $value3 = $data[2];
  19. $value4 = $data[3];
  20. $value5 = $data[4];
  21. $value6 = $data[5];
  22. $value7 = $data[6];
  23. $value8 = $data[7];
  24. $value9 = $data[8];
  25. $value10 = $data[9];
  26. $value11 = $data[10];
  27. $value12 = $data[11];
  28. //echo("<br />");
  29.  
  30. $query = "INSERT INTO testtsttbills (account,service,billDate,time,timebandtype,destination,callednumber,mins,airtimecharge,tollcharge,amt,privatenumber) VALUES ('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";
  31.  
  32. $qry = mysql_query($query,$conn) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno($conn) . ") " . mysql_error($conn));
  33.  
  34. }
  35. fclose($handle);
  36. echo "<div align='center'><font color='red'>The file was uploaded.</font></div>";
  37. }
  38. else
  39. {
  40. echo "<div align='center'><font color='red'>You are trying to upload an invalid file format. Please try again.</font></div>";
  41. }
  42. }
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 601
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 53
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Master Poster

Re: Problems with exporting from a mysql database to a .csv

  #52  
Jul 24th, 2008
You should be able to do it one of two ways:


Number 1: (adjust '%d/%m/%Y' to match up to the format that is on the csv, it is currently looking for "dd/mm/yyyy")
  1. if(count($_FILES) > 0)
  2. {
  3. $ext = "";
  4. $ext = substr(trim($_FILES["file"]["name"]), -4);
  5. $allowedext = array(".txt", ".csv", ".sql");
  6.  
  7. if(in_array($ext, $allowedext))
  8. {
  9. $filename = $_FILES['file']['tmp_name'];
  10. $handle = fopen($filename, "r");
  11.  
  12. while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE)
  13. {
  14. //print_r($data);
  15. //echo("<br />");
  16. $value1 = $data[0];
  17. $value2 = $data[1];
  18. $value3 = $data[2];
  19. $value4 = $data[3];
  20. $value5 = $data[4];
  21. $value6 = $data[5];
  22. $value7 = $data[6];
  23. $value8 = $data[7];
  24. $value9 = $data[8];
  25. $value10 = $data[9];
  26. $value11 = $data[10];
  27. $value12 = $data[11];
  28. //echo("<br />");
  29.  
  30. $query = "INSERT INTO testtsttbills (account,service,billDate,time,timebandtype,destination,callednumber,mins,airtimecharge,tollcharge,amt,privatenumber) VALUES ('$value1','$value2',STR_TO_DATE('$value3', '%d/%m/%Y'),'$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";
  31.  
  32. $qry = mysql_query($query,$conn) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno($conn) . ") " . mysql_error($conn));
  33.  
  34. }
  35. fclose($handle);
  36. echo "<div align='center'><font color='red'>The file was uploaded.</font></div>";
  37. }
  38. else
  39. {
  40. echo "<div align='center'><font color='red'>You are trying to upload an invalid file format. Please try again.</font></div>";
  41. }
  42. }


Number 2: (adjust list($dy, $mo, $yr) to match up to the format that is on the csv, it is currently looking for "dd/mm/yyyy")
  1. if(count($_FILES) > 0)
  2. {
  3. $ext = "";
  4. $ext = substr(trim($_FILES["file"]["name"]), -4);
  5. $allowedext = array(".txt", ".csv", ".sql");
  6.  
  7. if(in_array($ext, $allowedext))
  8. {
  9. $filename = $_FILES['file']['tmp_name'];
  10. $handle = fopen($filename, "r");
  11.  
  12. while (($data = fgetcsv($handle, 100000, ',', '"')) !== FALSE)
  13. {
  14. //print_r($data);
  15. //echo("<br />");
  16. $value1 = $data[0];
  17. $value2 = $data[1];
  18. list($dy, $mo, $yr) = explode("/", $data[2]);
  19. $value3 = $yr."-".$mo."-".$dy;
  20. $value4 = $data[3];
  21. $value5 = $data[4];
  22. $value6 = $data[5];
  23. $value7 = $data[6];
  24. $value8 = $data[7];
  25. $value9 = $data[8];
  26. $value10 = $data[9];
  27. $value11 = $data[10];
  28. $value12 = $data[11];
  29. //echo("<br />");
  30.  
  31. $query = "INSERT INTO testtsttbills (account,service,billDate,time,timebandtype,destination,callednumber,mins,airtimecharge,tollcharge,amt,privatenumber) VALUES ('$value1','$value2','$value3','$value4','$value5','$value6','$value7','$value8','$value9','$value10','$value11','$value12')";
  32.  
  33. $qry = mysql_query($query,$conn) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno($conn) . ") " . mysql_error($conn));
  34.  
  35. }
  36. fclose($handle);
  37. echo "<div align='center'><font color='red'>The file was uploaded.</font></div>";
  38. }
  39. else
  40. {
  41. echo "<div align='center'><font color='red'>You are trying to upload an invalid file format. Please try again.</font></div>";
  42. }
  43. }
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Feb 2008
Location: Trinidad
Posts: 400
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #53  
Jul 24th, 2008
Hey R0bb0b,

I tried your first method, and it worked perfectly....

the date in the database looks like "2007-11-06"
Last edited by maydhyam : Jul 24th, 2008 at 3:56 pm.
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 601
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 53
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Master Poster

Re: Problems with exporting from a mysql database to a .csv

  #54  
Jul 24th, 2008

cool
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Feb 2008
Location: Trinidad
Posts: 400
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #55  
Jul 25th, 2008
R0bb0b, why are you sweating so much
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 601
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 53
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Master Poster

Re: Problems with exporting from a mysql database to a .csv

  #56  
Jul 25th, 2008
Just relieved to see that the data is consistent. You never really know with a varchar since there really is no structure to keep it in the correct format, you could have had a lot of work ahead of you.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Feb 2008
Location: Trinidad
Posts: 400
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #57  
Jul 25th, 2008
Oho.......I see.......
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Join Date: Feb 2008
Location: Trinidad
Posts: 400
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #58  
Jul 28th, 2008
Originally Posted by R0bb0b View Post
Just relieved to see that the data is consistent. You never really know with a varchar since there really is no structure to keep it in the correct format, you could have had a lot of work ahead of you.


Ok, Well now that the data is consistent, the testGCR.php still doesn't work....
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 601
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 53
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Master Poster

Re: Problems with exporting from a mysql database to a .csv

  #59  
Jul 28th, 2008
OK, so post your code one more time so I can get a feel of where we are.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
Reply With Quote  
Join Date: Feb 2008
Location: Trinidad
Posts: 400
Reputation: maydhyam is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
maydhyam's Avatar
maydhyam maydhyam is offline Offline
Posting Pro in Training

Re: Problems with exporting from a mysql database to a .csv

  #60  
Jul 28th, 2008
testGCR.php

  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. include("./gcrFunction.php");
  4. if(count($_POST['btnSubmit']) == 0)
  5. {
  6. ?>
  7.  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  12. <title>Test Document</title>
  13. <script language="javascript" type="text/javascript" src="datetimepicker.js">
  14.  
  15. //Date Time Picker script- by TengYong Ng of http://www.rainforestnet.com
  16. //Script featured on JavaScript Kit (http://www.javascriptkit.com)
  17. //For this script, visit http://www.javascriptkit.com
  18.  
  19. </script>
  20.  
  21. </head>
  22.  
  23. <body>
  24. <form method="post" target="_blank">
  25. <table>
  26. <tr>
  27. <td width="747">
  28. <p align="left"><span class="style4 style25 style28 style6">Generate Chargeback Report</span></p>
  29. <p align="left"><span class="style21 style29 style5">The Chargeback Reporter works in 2 steps.</span></p>
  30. </td>
  31. </tr>
  32. <tr><td><p>&nbsp;</p></td></tr>
  33. <tr>
  34. <td>
  35. <table width="648" border="0" align="center" cellpadding="1" cellspacing="0" bordercolor="#FFFFFf">
  36. <tr>
  37. <td bgcolor="#99bebe" colspan="4">
  38. <p align="left" class="style8 style7 style32"><strong>
  39. <span class="style21 style33 style11">Step 1: Select Start and End Date</span></strong></p>
  40. </td>
  41. </tr>
  42. <tr>
  43. <td width="100" bgcolor="#99bebe" scope="row">
  44. <div align="left" class="style8 style7 style31 style29 style13">
  45. <div align="right"><strong>Start Date: </strong></div>
  46. </div> </td>
  47. <td width="220" bgcolor="#99bebe">
  48. <input name="sDate" id="sDate" type="text" size="25">
  49. <a href="javascript:NewCal('sDate','ddmmyyyy')">
  50. <img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a Start Date">
  51. </a> </td>
  52. <td width="100" bgcolor="#99bebe" scope="row">
  53. <div align="left" class="style8 style7 style31 style29 style13">
  54. <div align="right"><strong>End Date: </strong></div>
  55. </div> </td>
  56. <td width="220" bgcolor="#99bebe">
  57. <input name="eDate" id="eDate" type="text" size="25">
  58. <a href="javascript:NewCal('eDate','ddmmyyyy')">
  59. <img src="images/cal.gif" width="16" height="16" border="0" alt="Pick an End Date">
  60. </a> </td>
  61. </tr>
  62. <tr>
  63. <td bgcolor="#99bebe" colspan="4">
  64. <p align="left" class="style5">
  65. <span class="style21 style30 style11">Step 2: Generate the Chargeback Report </span></p> </td>
  66. </tr>
  67. <tr>
  68. <td bgcolor="#99bebe"><p>&nbsp;</p></td>
  69. <td bgcolor="#99bebe" colspan="3">
  70. <div align="left">
  71. <strong>
  72. <input type="submit" value="Generate" name="btnSubmit" />
  73. </strong>
  74. </div>
  75. </td>
  76. </tr>
  77. </table>
  78. </td>
  79. </tr>
  80. </table>
  81. </form>
  82. </body>
  83. </html>
  84. <?
  85. }
  86. validateData();
  87. ?>

gcrFunction.php

  1. <?
  2. include('./connect.php');
  3.  
  4.  
  5.  
  6. function validateData()
  7. {
  8. global $conn;
  9.  
  10. $sDate=$_POST['sDate'];
  11. $eDate=$_POST['eDate'];
  12. //echo $sDate;
  13. //echo $eDate;
  14.  
  15. $btn=$_POST['btnSubmit'];
  16.  
  17. if(isset($btn))
  18. {
  19. if(!$sDate)
  20. {
  21. echo '<center><font face=Verdana color=Red size="2"><b>Start Date field is empty!</b></font></center></td>
  22. </tr><tr bgcolor="#ffffff"><td>
  23. <table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf">
  24. <tr><td width="760" bgcolor="#336666" scope="col">&nbsp;</td></tr></table></td></tr><tr bgcolor="#ffffFF">
  25. <td><p align="center" class="style30 style4" >&copy;ITT Department. </p></td></tr></table>';
  26. die("Problem 1");
  27. }
  28. else if(!$eDate)
  29. {
  30. echo '<center><font face=Verdana color=Red size="2"><b>End Date field is empty!</b></font></center></td>
  31. </tr><tr bgcolor="#ffffff"><td>
  32. <table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFf">
  33. <tr><td width="760" bgcolor="#336666" scope="col">&nbsp;</td></tr></table></td></tr><tr bgcolor="#ffffFF">
  34. <td><p align="center" class="style30 style4" >&copy;ITT Department. </p></td></tr></table>';
  35. die("Problem 2");
  36. }
  37. else
  38. {
  39.  
  40. function parseCSVComments($comments)
  41. {
  42. // First off escape all " and make them ""
  43. $comments = str_replace('"', '""', $comments);
  44. if(eregi(",", $comments) or eregi("\n", $comments))
  45. { // Check if I have any commas or new lines
  46. return '"'.$comments.'"'; // If I have new lines or commas escape them
  47. }
  48. else
  49. {
  50. return $comments; // If no new lines or commas just return the value
  51. }
  52. }
  53.  
  54. // Start our query of the database
  55. $sql = mysql_query("SELECT * FROM tsttbills where DATE_FORMAT(date, '%d-%m-%Y') >= '$sDate' and DATE_FORMAT(date, '%d-%m-%Y') <= '$eDate'", $conn);
  56.  
  57. echo $sql;
  58. $numberFields = mysql_num_fields($sql); // Find out how many fields we are fetching
  59.  
  60. if($numberFields)
  61. { // Check if we need to output anything
  62. for($i=0; $i<$numberFields; $i++)
  63. {
  64. // Create the headers for each column, this is the field name in the database
  65. $head[] = mysql_field_name($sql, $i);
  66. }
  67. $headers = join(',', $head)."\n"; // Make our first row in the CSV
  68.  
  69. while($info = mysql_fetch_object($sql))
  70. {
  71. print_r($info);
  72. echo "<br />";
  73. }
  74.  
  75. /*while($info = mysql_fetch_object($sql))
  76. {
  77. foreach($head as $fieldName)
  78. { // Loop through the array of headers as we fetch the data
  79. $row[] = parseCSVComments($info->$fieldName);
  80. } // End loop
  81. $data .= join(',', $row)."\n"; // Create a new row of data and append it to the last row
  82. $row = ''; // Clear the contents of the $row variable to start a new row
  83. }
  84. */
  85. // Start our output of the CSV
  86. /*$filename = $file."_".date("Y-m-d_H-i",time());
  87. header("Content-type: application/x-msdownload");
  88. header("Content-Disposition: attachment; filename=".$filename.".csv");
  89. header("Pragma: no-cache");
  90. header("Expires: 0");*/
  91. echo $headers.$data;
  92. }
  93. else
  94. {
  95. // Nothing needed to be output. Put an error message here or something.
  96. echo ("There is no data to create a CSV file.");
  97. }
  98. } //close the else
  99. }//close the if(isset($btn)) statement
  100. }
  101.  
  102.  
  103.  
  104. ?>
The man who in view of gain thinks of righteousness; who in the view of danger is prepared to give up his life; and who does not forget an old agreement however far back it extends - such a man may be reckoned a complete man.
~ Confucius, The Confucian Analects
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the PHP Forum