PHP chained operation output excel (csv), chained csv_PHP tutorial

Source: Internet
Author: User
PHP chained operation outputs excel (csv) and chained csv. PHP chain operation outputs excel (csv). in the chain csv work, we often encounter product operations that allow you to export some simple and standardized data, at this time, if there is a simple way to output excel (csv) and chain csv via PHP chain operation

In our work, we often encounter product operations that allow you to export some simple and standardized data. at this time, it would be much easier to use a simple method. The following is a simple excel (csv) Method class that I use for chained operations. When it comes to chained operations, which may be frequently used in jquery, do you also feel that chained operations are quite helpful? we also implement chained operations in this class.

In fact, the chain operation is very simple, that is, the previous class method returns an object of this class ($ this), provided to the next method call.

 Ext = $ ext = null? $ This-> ext: $ ext; header ("Content-Disposition: attachment; filename = ". $ filename. ". ". $ this-> ext); ob_flush (); return $ this ;} /*** @ desc print the excel title * @ param array $ title the line of the title to be output * @ param object Array2csv object itself */public function title ($ title) {$ title = implode (",", $ title); echo $ title. "\ n"; return $ this;}/*** @ desc print a row of excel content * @ param array $ body content to be output * @ param object Array2csv object itself */public Function body ($ body) {if (! Is_array ($ body) | empty ($ body) {return false;} $ body = implode (",", $ body); echo $ body. "\ n"; return $ this ;} /*** @ desc print multi-row excel content * @ param array $ bodyArr multi-row content to be output * @ param object Array2csv object itself */public function multiBody ($ bodyArr) {if (! Is_array ($ bodyArr) | empty ($ bodyArr) return false; foreach ($ bodyArr as $ key => $ value) {if (is_array ($ value )) {$ value = implode (",", $ value); echo $ value. "\ n" ;}}return $ this ;}$ test = new Array2csv ('test'); $ arr = array ('luluyrt @ 163.com ', 'runningman1', 'runninguserman'), array ('luluyrt @ 163.com ', 'runningman2', 'runninguserman '), array ('luluyrt @ 163.com ', 'Running man3', 'Running userman'), array ('luluyrt @ 163.com ', 'Running man4 ', 'runninguserman'), array ('luluyrt @ 163.com ', 'runningman5', 'runninguserman'), array ('luluyrt @ 163.com ', 'runningman6', 'runninguserman'); $ test-> title (array ('test', 'hha ', 'Haha ')) -> body (array ('2017, sadkl ', 'sdsas', 'sdvsvdd sub')-> multiBody ($ arr );

Shows the output csv file:

$ Link = mysqli_connect ($ host, $ user, $ passwd, $ db); mysqli_query ($ link, "set names utf8 ");

Only after encoding conversion can the information be displayed normally. the following shows the information about my database and the effect before and after encoding:

Database information

Comparison before and after Database encoding

Send Me ~

Export (csv), chained csv jobs often encounter product operations that allow you to export some simple and standardized data. at this time, if there is a simple method that can be used...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.