Code for importing data to Foxmail using PHP

Source: Internet
Author: User
The principle below is to use PHP to generate a file and then download and import the data to their Foxmail address book. Recently, Xiaoyang made a PHP transcript where students added or modified the name, email address, OICQ, and other information and stored them in the mySQL database. One day, Xiaoyang suddenly thought that it would be nice if he could use PHP to generate a file for students to download and import the data into their Foxmail address book!

Just do what you want, and soon Xiaoyang will launch this function. How is that implemented? Here, we only describe the exported name, email address, and OICQ.

You can import materials to the Foxmail address book. of course, you must first understand the content and format of the files imported into the Foxmail address book. Open the address book of an account in Foxmail4.2. in the menu bar "tools"-"import", you can see that Foxmail supports the import of two types of external files: "CSV files" and "Wab files ". Select generate CSV file. What is the content and format of CSV files that can be imported into Foxmail? Let's first export a CSV file from Foxmail. In the Foxmail address book, select a folder with a non-empty record, execute "tool"-"export"-"text file", and save the file name as "TEMP. CSV. in "next", select "name", "email address", and "OICQ" in "select output Field ", after clicking "finish", the "TEMP. CSV file. If you have installed the Microsoft Office series, you will find that this is a file opened by default in EXCEL. In fact, it is a comma-separated value file in EXCEL. after you double-click it, its interface is displayed.

  

In this case, we still cannot know its write format. Set "TEMP. the CSV file is opened in notepad, and the format is very simple: the first line of the file is the field in the Foxmail address book, and the other lines are the value corresponding to the field, fields and values are separated by commas. So we generate CSV files in PHP in this format, and others can download and import them to their Foxmail!

  
However, there is another problem to be solved, that is, since the files are separated by commas (,), what should I do if the database records contain English commas? Of course, you can replace the comma in the data with a Chinese comma, but there is another way, that is, if the two ends of the corresponding fields in the CSV file are added with double quotation marks """) separate values. The English comma in the field is not used as the separated values, and two consecutive double quotes (") in the field are only displayed as one, not as the separated values.
With these knowledge, we can compile the php file for exporting CSV files:

The code is as follows:


$ Dfname = "tofoxmail.csv"; // generated file name
// Connect to the mySQL database:
Mysql_connect ("localhost", "yourname", "yourpassword") or die ("cannot connect to the database! ");
Mysql_select_db ("alumni") or die ("database error! ");
If ($ action = "downit "){
$ Getdata = mysql_query ("SELECT name, email, oicq FROM classdata"); // SELECT the specified record in the data table
// If no information is available:
If (@ mysql_num_rows ($ getdata) = 0 ){
Echo "Sorry, there is no information! ";
Exit;
}
// The following file is generated for download:
Header ("Content-disposition: filename = $ dfname ");
Header ("Content-type: unknown/unknown ");
Echo "name, email address, OICQ, foxaddrID"; // output the first line, Foxmail address book field
$ I = 1;
While ($ row = mysql_fetch_array ($ getdata) {// Get the data type value
// Replace each double quotation mark in the data with two:
$ Row [name] = str_replace ("\" "," \ "\" ", $ row [name]);
$ Row [email] = str_replace ("\" "," \ "\" ", $ row [email]);
$ Row [oicq] = str_replace ("\" "," \ "\" ", $ row [oicq]);
// Output the row of the value corresponding to the field. each value is separated by double quotation marks and commas:
Echo base64_decode ("DQo = "). "\" $ row [name] \ ", \" $ row [email] \ ", \" $ row [oicq] \ ", $ I ";
/* In the above formula, "base64_decode (" DQo = ")" is a line break. the reason why "" is not used is because the two are different,
Using the latter may cause Foxmail import to fail.
*/
$ I ++;
}
Exit;
}
?>




Export data to Foxmail address book
Click here to download the file and save it and import it to your Foxmail address book.

Procedure

......



Run the php file on the server and download the downloaded tofoxmail.csv file. Use Notepad to open the file.

In the Foxmail address book, click "tools"-"import"-"CSV file "......, A lot of data in the database is imported at once. this is a good idea!

  

(The above programs have passed the test in Apache + PHP4 + mySQL and IIS + PHP4 + mySQL .)

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.