home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / csv.php < prev    next >
Encoding:
PHP Script  |  2003-11-20  |  4.8 KB  |  170 lines

  1. <?php
  2. /* $Id: csv.php,v 2.1 2003/11/20 16:31:51 garvinhicking Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Set of functions used to build CSV dumps of tables
  7.  */
  8.  
  9. /**
  10.  * Outputs comment
  11.  *
  12.  * @param   string      Text of comment
  13.  *
  14.  * @return  bool        Whether it suceeded
  15.  */
  16. function PMA_exportComment($text) {
  17.     return TRUE;
  18. }
  19.  
  20. /**
  21.  * Outputs export header
  22.  *
  23.  * @return  bool        Whether it suceeded
  24.  *
  25.  * @access  public
  26.  */
  27. function PMA_exportHeader() {
  28.     global $what;
  29.     global $add_character;
  30.     global $separator;
  31.     global $enclosed;
  32.     global $escaped;
  33.  
  34.     // Here we just prepare some values for export
  35.     if ($what == 'excel') {
  36.         $add_character      = "\015\012";
  37.         $separator          = isset($GLOBALS['excel_edition']) && $GLOBALS['excel_edition'] == 'mac' ? ';' : ',';
  38.         $enclosed           = '"';
  39.         $escaped            = '"';
  40.         if (isset($GLOBALS['showexcelnames']) && $GLOBALS['showexcelnames'] == 'yes') {
  41.             $GLOBALS['showcsvnames'] = 'yes';
  42.         }
  43.     } else {
  44.         if (empty($add_character)) {
  45.             $add_character  = $GLOBALS['crlf'];
  46.         } else {
  47.             $add_character  = str_replace('\\r', "\015", $add_character);
  48.             $add_character  = str_replace('\\n', "\012", $add_character);
  49.             $add_character  = str_replace('\\t', "\011", $add_character);
  50.         } // end if
  51.         $separator          = str_replace('\\t', "\011", $separator);
  52.     }
  53.     return TRUE;
  54. }
  55.  
  56. /**
  57.  * Outputs database header
  58.  *
  59.  * @param   string      Database name
  60.  *
  61.  * @return  bool        Whether it suceeded
  62.  *
  63.  * @access  public
  64.  */
  65. function PMA_exportDBHeader($db) {
  66.     return TRUE;
  67. }
  68.  
  69. /**
  70.  * Outputs database footer
  71.  *
  72.  * @param   string      Database name
  73.  *
  74.  * @return  bool        Whether it suceeded
  75.  *
  76.  * @access  public
  77.  */
  78. function PMA_exportDBFooter($db) {
  79.     return TRUE;
  80. }
  81.  
  82. /**
  83.  * Outputs create database database
  84.  *
  85.  * @param   string      Database name
  86.  *
  87.  * @return  bool        Whether it suceeded
  88.  *
  89.  * @access  public
  90.  */
  91. function PMA_exportDBCreate($db) {
  92.     return TRUE;
  93. }
  94.  
  95. /**
  96.  * Outputs the content of a table in CSV format
  97.  *
  98.  * @param   string      the database name
  99.  * @param   string      the table name
  100.  * @param   string      the end of line sequence
  101.  * @param   string      the url to go back in case of error
  102.  * @param   string      SQL query for obtaining data
  103.  *
  104.  * @return  bool        Whether it suceeded
  105.  *
  106.  * @access  public
  107.  */
  108. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
  109.     global $what;
  110.     global $add_character;
  111.     global $separator;
  112.     global $enclosed;
  113.     global $escaped;
  114.  
  115.     // Gets the data from the database
  116.     $result      = PMA_mysql_query($sql_query) or PMA_mysqlDie('', $sql_query, '', $error_url);
  117.     $fields_cnt  = mysql_num_fields($result);
  118.  
  119.     // If required, get fields name at the first line
  120.     if (isset($GLOBALS['showcsvnames']) && $GLOBALS['showcsvnames'] == 'yes') {
  121.         $schema_insert = '';
  122.         for ($i = 0; $i < $fields_cnt; $i++) {
  123.             if ($enclosed == '') {
  124.                 $schema_insert .= stripslashes(mysql_field_name($result, $i));
  125.             } else {
  126.                 $schema_insert .= $enclosed
  127.                                . str_replace($enclosed, $escaped . $enclosed, stripslashes(mysql_field_name($result, $i)))
  128.                                . $enclosed;
  129.             }
  130.             $schema_insert     .= $separator;
  131.         } // end for
  132.         $schema_insert  =trim(substr($schema_insert, 0, -1));
  133.         if (!PMA_exportOutputHandler($schema_insert . $add_character)) return FALSE;
  134.     } // end if
  135.  
  136.     // Format the data
  137.     while ($row = PMA_mysql_fetch_row($result)) {
  138.         $schema_insert = '';
  139.         for ($j = 0; $j < $fields_cnt; $j++) {
  140.             if (!isset($row[$j])) {
  141.                 $schema_insert .= $GLOBALS[$what . '_replace_null'];
  142.             }
  143.             else if ($row[$j] == '0' || $row[$j] != '') {
  144.                 $row[$j] = stripslashes($row[$j]);
  145.                 // loic1 : always enclose fields
  146.                 if ($what == 'excel') {
  147.                     $row[$j]       = ereg_replace("\015(\012)?", "\012", $row[$j]);
  148.                 }
  149.                 if ($enclosed == '') {
  150.                     $schema_insert .= $row[$j];
  151.                 } else {
  152.                     $schema_insert .= $enclosed
  153.                                    . str_replace($enclosed, $escaped . $enclosed, $row[$j])
  154.                                    . $enclosed;
  155.                 }
  156.             }
  157.             else {
  158.                 $schema_insert .= '';
  159.             }
  160.             if ($j < $fields_cnt-1) {
  161.                 $schema_insert .= $separator;
  162.             }
  163.         } // end for
  164.         if (!PMA_exportOutputHandler($schema_insert . $add_character)) return FALSE;
  165.     } // end while
  166.     mysql_free_result($result);
  167.  
  168.     return TRUE;
  169. } // end of the 'PMA_getTableCsv()' function
  170. ?>