Using PHP to import data into the Foxmail implementation code

Source: Internet
Author: User
Tags format exit header connect mysql php file first row mysql database
Recently Sunny Little did a PHP alumni, the students themselves there to add or modify the name, mailbox, OICQ and other information, and save in the MySQL database. One day sunny little suddenly thought, if can use PHP to generate a file, for students to download and import the data into their Foxmail address book, that would be so good!

Said to dry, soon Xiao Yang will be launched this function. How did that happen? This is only illustrated by exporting names, mailboxes, and Oicq three items.

To be able to import the Foxmail address Book, you must first understand the file contents and format of the import Foxmail Address Book. Open Foxmail4.2 An account's address book, as you can see in its menu bar "Tools"-"import", Foxmail supports the import of two external files: CSV file and WAB file. We chose to generate a CSV file. What about the content and format of the Foxmail CSV file that you can import? Let's first export a CSV file from Foxmail to see. Select a folder in the Foxmail address book that is not empty, execute tools-Export-text files, and save the file named "TEMP." CSV, select "Name", "E-mail Address" and "OICQ" in "Please select output field" in "Next" and click "Finish" to generate "TEMP" in the specified path. CSV file. If you install 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 of Excel, double-click Open, its interface as shown in the figure.

  

We still have no way of knowing its write format in this case. Will "TEMP." CSV file to open with Notepad, you can find that its format is very simple: as the figure, the first line of the file is Foxmail Address Book field, the other row is the corresponding value of the field, the fields and values are separated by commas in English. So we are in PHP in this format to generate CSV files, others can download and import their foxmail!

  
But there is another problem to be solved, that is, since the file is separated by commas, if there is a comma in the database record (note: The following symbols, if no special instructions are English symbols) how to do? Of course you can first replace the comma in the data with the Chinese comma, but there is actually a way that is, if the corresponding field in the CSV file has a double quotation mark ("" "" ") as the separated value, the English comma is not used as the delimited value, and the two consecutive double quotes in the field (that is," ") are only displayed as one. Instead of being a delimited value.
With these realizations, we can write PHP files that export CSV files:
Copy CodeThe code is as follows:
? This line must be placed at the beginning of the program, no spaces or lines. Because the following header () function does not allow you to output anything to the user before use.
$dfname = "Tofoxmail.csv"; The file name that was generated
To connect to the MySQL database:
mysql_connect ("localhost", "yourname", "YourPassword") or Die ("Cannot connect to 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 datasheet
If there is no information, then:
if (@mysql_num_rows ($getdata) ==0) {
echo "Sorry, there is no information!";
Exit
}
The following generates a file for downloading:
Header ("Content-disposition:filename= $dfname");
Header ("Content-type:unknown/unknown");
echo "Name, email address, oicq,foxaddrid"; Output first row, Foxmail Address Book Field
$i = 1;
while ($row =mysql_fetch_array ($getdata)) {//Get the value of the data type
Replace each double quote in the data with two:
$row [Name]=str_replace ("\" "," \ "\"), $row [name]);
$row [Email]=str_replace ("\"), "\" \ "", $row [email]);
$row [Oicq]=str_replace ("\" "," \ "\"), $row [Oicq]);
The row that outputs the value of the corresponding field, with double quotes and commas as delimiters:
Echo Base64_decode ("dqo="). " \ "$row [name]\", \ "$row [email]\", \ "$row [oicq]\], $i";
/* the "Base64_decode" ("dqo=") is a newline character and is not "\ n" because the two are not exactly the same.
Using the latter may cause foxmail import to fail.
*/
$i + +;
}
Exit
}
?>
<!--If you did not click on the download link, the following HTML content is displayed-->
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Export data to Foxmail address book </title><body> Click <a href= "javascript:this.location=this.location+" Action=downit "" > here </a> download file, Save and import into your foxmail address book. <br>
<B> Operation Method </B><br>
......
</body>

Running the above PHP file on the server, the downloaded "tofoxmail.csv" file is opened in Notepad as shown in the picture.

Click "Tools"-"Import"-"CSV file" in the Foxmail Address Book ..., a lot of data in the database all of a sudden have been imported, this idea is not bad!

  

(The above procedures are tested and passed in both 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.