数据值需要存储在CSV文件中

时间:2019-04-25 07:08:30

标签: php drupal drupal-7 drupal-modules

我无法在文件中存储数据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);

}

我犯错的地方

3 个答案:

答案 0 :(得分:0)

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

$data = [[1,2,3][11,22,33]];
$fp = fopen('test.csv', 'w');

foreach ($data as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);

答案 1 :(得分:0)

检查您的文件夹权限。即site / default / files / csv。为了将某些内容写入文件,它应该是可写的。

答案 2 :(得分:0)

我不确定您的期望如何,但是您需要使用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