[ Index ]

PHP Cross Reference of Crawler

title

Body

[close]

/ -> export.php (source)

   1  <?php
   2  /* Exports database as CSV
   3   *
   4   * NB: If you are crawling a large website (> 65,000 URLs) Excel cannot open the file, however Access can
   5   *
   6   * @package Crawler
   7  */
   8   
   9  /**
  10   * Include necessary files
  11   */
  12  include ('config.php');
  13  include ('includes/functions.php');
  14  include ('includes/mysql_functions.php');
  15  
  16  /**
  17   * Set the content headers
  18   */
  19  header('Content-type: application/CSV');
  20  header("Content-Disposition: attachment; filename=export.csv");
  21      
  22  /**
  23   * SQL query to generate our results
  24   */
  25  $pages = mysql_query("SELECT url, title, clicks, http_code, size, type, modified, (SELECT count(*) FROM links WHERE `to` = urls.ID) as incoming, (SELECT count(*) FROM links WHERE `to` = urls.ID) as outgoing from urls");
  26  
  27  /**
  28   * Count the number of pages in our dataset
  29   */
  30  $count = mysql_num_rows($pages);
  31  
  32  /**
  33   * Get array of fields by parsing keys of first row array
  34   *
  35   * NOTE TO SELF: There is a better way to do this
  36   */
  37  $fields = array_keys(mysql_fetch_assoc($pages));
  38  
  39  /**
  40   * Print the header row and a new line charecter
  41   */
  42  foreach ($fields as $field) {
  43      echo "$field\t";
  44  }
  45  echo "\n";
  46  
  47  /**
  48   * When we looped through to grab the field names, we moevd the internal pointer.
  49   * Reset internal pointer so our loop includes the first row
  50   */
  51  mysql_data_seek($pages,0);
  52  
  53  
  54  /**
  55   * Loop through the rows (pages)
  56   */
  57  for ($i=0; $i < $count; $i++) {
  58  
  59      /**
  60       * Fetch the row as an associative array
  61       */
  62      $page = mysql_fetch_assoc($pages);
  63      
  64      /**
  65       * Loop through each field within the row
  66       */
  67      foreach ($page as $key=>$field) {
  68      
  69          /**
  70           * If it the 'size', or 'modified' field, make it human readible, otherwise just output
  71           */
  72          switch($key) {
  73              case 'size':
  74                  echo file_size($field);
  75              break;
  76              case 'modified':
  77                  if (!is_null($field)) echo date('Y-m-d H:i:s',$field);
  78              break;
  79              default:
  80                  echo $field;
  81              break;
  82          } //End switch
  83          echo "\t"; 
  84      } //End Field
  85      echo "\n";
  86  } //End Row
  87  
  88  ?>


Generated: Thu Jun 3 17:10:09 2010 Cross-referenced by PHPXref 0.7