如何解析csv文件数据和在Drupal将其插入到数据库7

问题描述:

我已经使用代码在数据库导入csv文件,但如何读取该文件数据并显示它在Drupal如何解析csv文件数据和在Drupal将其插入到数据库7

我用下面的代码导入CSV文件

<?php 
    function file_upload_permission() { 
    return array(
     'administer uploader' => array(
     'title' => t('Administer Uploader'), 
     'description' => t('Allow the following roles to upload csv files to the server.'), 
      ), 
     ); 
    } 

    /** 
    * Implements hook_menu() 
    */ 
    function file_upload_menu() { 

     $items['file_upload/import'] = array(
     'title' => 'Upload a File', 
     'type' => MENU_CALLBACK, 
     'description' => 'Import a csv', 
     'page callback' => 'drupal_get_form', 
    'page arguments' => array('file_upload_import_form'), 
     'access arguments' => array('access content'), 
    'access callback' => TRUE 
     ); 

    return $items; 
    } 

    /** 
    * Builds a form that will allow users to upload csv files 
    * hook_menu() 
    */ 
    function file_upload_import_form($form, $form_state) { 

      $form['notes'] = array(
      '#type' => 'markup', 
      '#markup' => '<div class="import-notes">A few notes when uploading. <ul><li>Make  
     sure   the file is in a .csv format.</li><li>Columns should be in *this* 
      order</li><li>Be sure to click the "Upload" button when you select a csv.</li> 
     </ul></div>', 
     '#upload_location' => 'public://tmp/', 
     ); 

     $form['import'] = array(
      '#title' => t('Import'), 
      '#type' => 'managed_file', 
      '#description' => t('The uploaded csv will be imported and temporarily saved.'), 
      '#upload_location' => 'public://tmp/', 
      '#upload_validators' => array(
      'file_validate_extensions' => array('csv'), 
           ), 
       ); 

     $form['submit'] = array (
     '#type' => 'submit', 
     '#value' => t('Import'), 
     ); 
      return $form; 

    } 

     /** 
     * Submit handler for file_upload_import_form() 
     */ 
    function file_upload_import_form_submit($form, $form_state) { 

    // Check to make sure that the file was uploaded to the server properly 
     $uri = db_query("SELECT uri FROM {file_managed} WHERE fid = :fid", array(
         ':fid' => $form_state['input']['import']['fid'], 
        ))->fetchField(); 

     if(!empty($uri)) { 
     if(file_exists(drupal_realpath($uri))) { 
      // Open the csv 
      $handle = fopen(drupal_realpath($uri), "r"); 
     // Go through each row in the csv and run a function on it. In this case we are 
      parsing by '|' (pipe) characters. 

      // If you want commas are any other character, replace the pipe with it. 

      while (($data = fgetcsv($handle, 0, ',', '"')) !== FALSE) { 

     $result = db_insert('test')->fields(array('price' => $data[1],))->execute(); 
     $operations[] = array(
      'file_upload_import_batch_processing', // The function to run on each row 
      array($data), // The row in the csv 
     ); 
     } 

     // Once everything is gathered and ready to be processed... well... process it! 
     $batch = array(
     'title' => t('Importing CSV...'), 
     'operations' => $operations, // Runs all of the queued processes from the while loop 
     above. 
    'finished' => 'file_upload_import_finished', // Function to run when the import is 
       successful 
     'error_message' => t('The installation has encountered an error.'), 
     'progress_message' => t('Imported @current of @total products.'), 
    ); 
    batch_set($batch); 
    fclose($handle);  
    } 
} 
else { 
    drupal_set_message(t('There was an error uploading your file. Please contact a System 
    administator.'), 'error'); 
    } 


} 

此代码工作正常上传文件particuler文件夹和数据库file_managed表 但问题是,在上传之后我怎么能显示Csv文件数据并在数据库中插入新数据表的 ?

if anyone knows , please let me know, 
thanks in advance. 

可以使用Feeds模块,它可以导入CSV文件。 Feed通常会从导入的数据中创建节点,因此最终得到的是一大堆节点,每个节点都对应于CSV文件中的一行。

如果要将数据导入到自定义表格中,可以将Data与Feed结合使用。在项目页面上,它说:

您可以使用Feed将RSS,Atom,CSV或OPML文件导入到数据表中。

有了一定的调整,这应该为你工作(如果我理解正确你的问题):

/** 
** View the table td_e_list 
    */ 

function access_td_e_list_view() { 

    $results = db_query("SELECT * FROM {td_e_list}"); 
    $header = array(t('Department'),t('Name'),t('Office'),t('Mobile'),t('Home'),t('Email'),t('Alt. Email'),t('D/R Office')); 

    $rows = array(); 

    foreach($results AS $result) { 
    $rows[] = array(
     $result->dept, 
     $result->name, 
     $result->office, 
     $result->mobile, 
     $result->home, 
     $result->email, 
     $result->altemail, 
     $result->drphone, 
    ); 
    } 
    return theme('table', array('header' => $header, 'rows' => $rows)); 
}