【问题标题】:Data values need to store in CSV file数据值需要存储在 CSV 文件中
【发布时间】:2019-04-25 07:08:30
【问题描述】:

我无法在文件中存储数据 csv

我可以加载数据,但我无法将数据存储在 CSV 文件中并将文件存储在特定文件夹中 我的要求是加载 Web 表单详细信息并将电子邮件发送给特定用户。我可以下载 csv 文件,但我无法将详细信息存储在 CSV 文件中

    function webform_email_menu() {
        $items = array();
         $items['webform_email'] = array(
            'title'             =>  'Webform Email',  //page title
            'description'       =>  'Webform Information',  //description show when mouse hover on link
            'page callback'     =>  'webform_list',  //callback function which is invoked when menu item is called.
            'access callback'   =>  true,  //any user can access this page
           );
         return $items;
    }



function webform_list() {
    module_load_include('inc', 'webform', 'includes/webform.export');
    module_load_include('inc', 'webform', 'includes/webform.components');
    module_load_include('inc', 'webform', 'includes/webform.submissions');  
    $submissions = webform_get_submissions(125);
echo "installation Type,Date of Purchase,Date of Install    ,Outdoor Unit Model,Outdoor Unit Serial,Indoor Unit 1 Model,Indoor Unit 1 Serial,Indoor Unit 2 Model,Indoor Unit 2 Serial,Indoor Unit 3 Model,Indoor Unit 3 Serial,Indoor Unit 4 Model,Indoor Unit 4 Serial,Indoor Unit 5 Model,Indoor Unit 5 Serial,Indoor Unit 6 Model,Indoor Unit 6 Serial,Indoor Unit 7 Model,Indoor Unit 7 Serial,Indoor Unit 8 Model,Indoor Unit 8 Serial,Indoor Unit 9 Model,Indoor Unit 9 Serial,Owner First Name,Owner Last Name,Installation Address 1,Installation Address 2,City,State/Province,Zip/Postal Code,Country ,Owner Phone Number,Owner Email,Contractor Information,Contractor Name,Contractor Address 1 ,Contractor Address 2,City,State/Province,Zip/Postal Code,Country,Contractor Phone Number,Contractor Email,Distributor Name,Distributor Address 1,Distributor Address 2,City,State/Province,Zip/Postal Code,Country,TAC,Privacy Statement\n";
   $surveycomponents = db_select('webform_component')
     ->fields('webform_component')
     ->condition('nid', 125)
     ->orderBy('weight')
     ->orderBy('name')
     ->execute()
     ->fetchAllAssoc('cid', PDO::FETCH_ASSOC);
$count =0;
$i=2;
   foreach ($submissions as &$submission) {
       $submission_value=$submission->data;
       $i=0;
       foreach($submission_value as $submissionvalue){
            $numItems = count($submission_value);
            if(++$i === $numItems) {
                echo $csv_export="\n";
            }else{
                echo $csv_export=$submissionvalue[0].',';
            }
       }
    }

        $file_open = fopen('sites/default/files/csv/csvfile.csv', "a");
        file_put_contents("sites/default/files/csv/csvfile.csv", $csv_export);
    $csv_handler = fopen ('sites/default/files/csv/csvfile.csv','w');
    fwrite ($csv_handler,$csv_export);
    fclose ($csv_handler);

}

我做错的地方

【问题讨论】:

    标签: php drupal drupal-7 drupal-modules


    【解决方案1】:

    您可以使用这样的数组将数据存储在 CSV 文件中

    $data = [[1,2,3][11,22,33]];
    $fp = fopen('test.csv', 'w');
    
    foreach ($data as $fields) {
        fputcsv($fp, $fields);
    }
    fclose($fp);
    

    【讨论】:

      【解决方案2】:

      检查您的文件夹权限。即站点/默认/文件/csv。它应该是可写的,以便将某些内容写入文件。

      【讨论】:

        【解决方案3】:

        我不确定你是否期望,但你需要使用 drupal 函数来获得正确的使用路径并准备对其进行修改:

        $path = drupal_realpath('public://').DIRECTORY_SEPARATOR.'csv';
        file_prepare_directory($path);
        $file_open = fopen($path.'/csvfile.csv', "a");
        file_put_contents($path.'/csvfile.csv', $csv_export);
        

        [...]

        https://api.drupal.org/api/drupal/includes%21file.inc/function/file_prepare_directory/7.x

        https://api.drupal.org/api/drupal/includes%21file.inc/function/drupal_realpath/7.x

        【讨论】:

          猜你喜欢
          • 2015-08-24
          • 2014-04-16
          • 2018-05-08
          • 2020-08-08
          • 2015-03-14
          • 2017-11-05
          • 2012-12-17
          • 2016-03-12
          • 2021-03-12
          相关资源
          最近更新 更多