Jump to content

Problem In Exporting Csv From Php


jthan03

Recommended Posts

I need your help guys.

 

I have a PHP code that output an array of data into CSV, but when I tried to export it, it appears in the page and not in the csv file.

 

Here is the function:

 

 

 

function outputCsv(&$controller, &$request, &$user){

 

 

$Ac = new ActionChain();

$Ac->register('csv','MenteMember', 'MakeCsv');

$Ac->execute($controller, $request, $user);

$strCsvData = $Ac->fetchResult('csv');

 

$filename = "member.csv";

 

header("Cache-Control: public");

header("Pragma: public");

header("Content-disposition: attachment; filename=" . $filename );

header("Content-type: application/octet-stream; name=" . $filename );

print mb_convert_encoding($strCsvData, "SJIS", "UTF8" );

 

return;

}

 

 

Your answer will be a great help!

Edited by jthan03
Link to comment
Share on other sites

What happens when you change this line:

 

header("Content-type: application/octet-stream; name=" . $filename );

 

to this:

 

header("Content-type: application/octet-stream");

 

it's still appears on the page... the data that is being exported into CSV is almost 23,000 data, but there are almost 45,000 data and it successfully exported into the CSV...

Link to comment
Share on other sites

How are you calling this function? Also, if you are using PHP 5.3+, the call-time pass-by-reference is deprecated.

yes sir, i'm calling this function to have an exported CSV... actually this was made by using Mojavi 2... I'm fixing the errors that occur in the system and that's the only problem that i need to fix...

Link to comment
Share on other sites

That's not what I meant. Is the function within the same file that you are calling for the export? I can't tell because you haven't posted the whole code. I don't think there is anything wrong with your function. However, if the file you are calling in the browser is the same one that includes the function, then you need to return the function appropriately:

 

function outputCsv(&$controller, &$request, &$user) {

$Ac = new ActionChain();
$Ac->register('csv','MenteMember', 'MakeCsv');
$Ac->execute($controller, $request, $user);
$strCsvData = $Ac->fetchResult('csv');

$filename = "member.csv";

header("Cache-Control: public");
header("Pragma: public");
header("Content-disposition: attachment; filename=" . $filename );
header("Content-type: application/octet-stream; name=" . $filename );
print mb_convert_encoding($strCsvData, "SJIS", "UTF8" );
}

return outputCsv($controller, $request, $user);

Link to comment
Share on other sites

sorry, here is the whole php file:

 

<?php


require_once(LIB_DIR.'Db/Member/Member.Factory.php');
require_once(LIB_DIR.'Page.php');
require_once(BASE_DIR.'modules/MenteMember/actions/BaseMenteMemberAction.class.php');


class ListAction extends BaseMenteMemberAction {

   function initialize(&$controller, &$request, &$user){



       $nyuten_cnt_over = $request->getParameter('nyuten_cnt_over');
       $nyuten_cnt_over = mb_convert_kana($nyuten_cnt_over,"n");
       $request->setParameter('nyuten_cnt_over',$nyuten_cnt_over);

       $nyuten_cnt_under = $request->getParameter('nyuten_cnt_under');
       $nyuten_cnt_under = mb_convert_kana($nyuten_cnt_under,"n");
       $request->setParameter('nyuten_cnt_under',$nyuten_cnt_under);

       if (!$this->setAttributeDbRef($controller, $request, $user)) {
           $controller->redirect(MENTE_DB_ERR_URL);
           return false;
       }

       $this->setShopList($controller, $request, $user);

       $this->setLastNyutenList($controller, $request, $user);

       return TRUE;
   }


   function getRequestMethods(){
       return REQ_POST|REQ_GET;
   }


   function execute(&$controller, &$request, &$user) {



       if (!$request->getParameter('search')) {
           return VIEW_SUCCESS;
       }

       //DB
       $db = $request->getAttribute('db');

       $blOwnerShopChk = $this->chkOwnerShop($controller, $request, $user);
       if (!$blOwnerShopChk) {
           return VIEW_SUCCESS;
       }

       if ($request->getParameter('csv') == '1') {
           $this->outputCsv($controller, $request, $user);

           return VIEW_NONE;
       }


       $MemberFactory = new MemberFactory();

       $arrSearch = array();

       $arrSearch = $this->getMemberSearchArr($controller, $request, $user);

       $intMemberCount = $MemberFactory->getMemberCountBySearchArr($db,$arrSearch);

       define('PAGE_NUMBER_DISPLAY','10');

       $limit = "50";



       $Page = new PageTag($limit,intval($request->getParameter('offset')),$intMemberCount);
       $Page->load();

       $param = '&mo='.$controller->getRequestModule()
               .'&ac='.$controller->getRequestAction()
               .'&member_id='.urlencode($request->getParameter('member_id'))
               .'&member_nm='.urlencode($request->getParameter('member_nm'))
               .'&member_kana_nm='.urlencode($request->getParameter('member_kana_nm'))
               .'&member_post_no='.urlencode($request->getParameter('member_post_no'))
               .'&member_add1='.urlencode($request->getParameter('member_add1'))


               .'&member_tel_no='.urlencode($request->getParameter('member_tel_no'))
               .'&member_mobil_tel_no='.urlencode($request->getParameter('member_mobil_tel_no'))
               .'&member_birth_day='.urlencode($request->getParameter('member_birth_day'))
               .'&black_flg='.$request->getParameter('black_flg')
               .'&member_flg='.$request->getParameter('member_flg')
               .'&delete_flg='.$request->getParameter('delete_flg')
               .'&shop_fc_no='.$request->getParameter('shop_fc_no')

               .'&last_nyuten='.$request->getParameter('last_nyuten')
               .'&nyuten_cnt_over='.$request->getParameter('nyuten_cnt_over')
               .'&nyuten_cnt_under='.$request->getParameter('nyuten_cnt_under')

               .'&search='.$request->getParameter('search');

       $Page->makeTagNoHtml($param);



       $arrMember = $MemberFactory->getMemberArrBySearchArr($db,$arrSearch,$limit,$request->getParameter('offset'));




       $request->setAttribute('arrMember',$arrMember);
       $request->setAttribute('intMemberCount',intval($intMemberCount));
       $request->setAttribute('tagNoHtml',$Page->getTagNoHtml());


       return VIEW_SUCCESS;

   }

   function outputCsv(&$controller, &$request, &$user){



       $Ac = new ActionChain();
       $Ac->register('csv','MenteMember', 'MakeCsv');
       $Ac->execute($controller, $request, $user);
       $strCsvData = $Ac->fetchResult('csv');


       $filename = "member.csv";


       header("Cache-Control: public");
       header("Pragma: public");
       header("Content-disposition: attachment; filename=" . $filename );
       header("Content-type: application/octet-stream; name=" . $filename );
       print mb_convert_encoding($strCsvData, "SJIS", "UTF8" );

       return;
   }
}


?>

Link to comment
Share on other sites

I'm going to assume that you're generating a HTML page at the same time here, based upon your rather sparse description of the issue. With that I mean that if you select "View source" in your browser, you'll see both HTML content and the CSV file in question.

 

If that's the case, then you cannot do this. You'll have to create only one content type at any given time, as the browsers only accept a single content type from the same HTTP response. This means you'll have to move the CSV file generation to it's own request, and make sure that absolutely no HTML content (nor any HTML HTTP headers) are being sent. Then call this link after generating the HTML content you want to display, either via a redirect (JS or HTTP) or having the user manually click on a link.

Link to comment
Share on other sites

I'm going to assume that you're generating a HTML page at the same time here, based upon your rather sparse description of the issue. With that I mean that if you select "View source" in your browser, you'll see both HTML content and the CSV file in question.

 

If that's the case, then you cannot do this. You'll have to create only one content type at any given time, as the browsers only accept a single content type from the same HTTP response. This means you'll have to move the CSV file generation to it's own request, and make sure that absolutely no HTML content (nor any HTML HTTP headers) are being sent. Then call this link after generating the HTML content you want to display, either via a redirect (JS or HTTP) or having the user manually click on a link.

 

thanks for the advice... any ideas where is can I place the best position of the CSV function? not in this file?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.