Home  >  Article  >  Backend Development  >  Implementation code for importing data into Foxmail using PHP_PHP tutorial

Implementation code for importing data into Foxmail using PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:34:22875browse

Recently, Xiaoyang made a PHP classmate directory, where students can add or modify their name, email, OICQ and other information, and save it in the mySQL database. One day, Xiaoyang suddenly thought that it would be great if PHP could be used to generate a file for students to download and import the information into their Foxmail address book!

Just do it, and soon Xiaoyang launched this function. How is that achieved? Here we only explain the export of name, email address and OICQ.

If you want information, you can import it into the Foxmail address book. Of course, you must first understand the content and format of the file imported into the Foxmail address book. Open the address book of an account in Foxmail4.2, and you can see in its menu bar "Tools" - "Import" that Foxmail supports the import of two external files: "CSV file" and "Wab file". We choose to generate a CSV file. So what is the content and format of CSV files that can be imported into Foxmail? Let's first export a CSV file from Foxmail and take a look. Select a folder with a non-empty record in the Foxmail address book, execute "Tools" - "Export" - "Text File", save the file name as "TEMP.CSV", and select the output field in "Next" "Select "Name", "Email Address" and "OICQ", and click "Finish" to generate the "TEMP.CSV" file in the specified path. If you have installed the Microsoft office series, you will find that this is a file that is opened by default with EXCEL. In fact, it is a comma-separated value file of EXCEL. After double-clicking to open it, the interface will be as shown in the figure.

 Implementation code for importing data into Foxmail using PHP_PHP tutorial

 We still have no way of knowing its writing format in this case. Open the "TEMP.CSV" file with Notepad, and you will find that its format is very simple: as shown in the figure, the first line of the file is the field of the Foxmail address book, and the other lines are the values ​​corresponding to the fields. Each field and value is Separated by English commas. So we generate CSV files in this format in PHP, and others can download and import them into their Foxmail!

 Implementation code for importing data into Foxmail using PHP_PHP tutorial
However, there is still a problem that needs to be solved, that is, since the file uses commas as separated values, if there are English commas in the database records (Note: The following symbols refer to English unless otherwise specified) symbol)? Of course, you can first replace the commas in the data with Chinese commas, but there is actually another way, that is, if double quotes ("ie " "") are added to both ends of the corresponding fields in the CSV file to separate the values, the English commas in between It is not used as a separated value, and two consecutive English double quotes (i.e. """) in the field are only displayed as one, not as a separated value.
With these understandings, we can write the PHP file that exports the CSV file:

Copy the code The code is as follows:

$dfname="tofoxmail.csv"; //Generated file name
//Connect to mySQL database:
mysql_connect("localhost","yourname","yourpassword") or die("Cannot connect 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 there is no data, then:
if(@mysql_num_rows($getdata)==0){
echo "Sorry , there is no information yet!";
exit;
}
//The following generates a file for download:
header("Content-disposition: filename=$dfname");
header ("Content-type: unknown/unknown");
echo "name, email address, OICQ, foxaddrID"; // Output the first line, the field of the Foxmail address book
$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]);
//Output the row corresponding to the value of the corresponding field. Each value is separated by double quotes and commas:
echo base64_decode( "DQo=").""$row[name]","$row[email]","$row[oicq]",$i";
/*In the above formula, "base64_decode("DQo=" )" is a newline character. The reason why "n" is not used is because the two are not exactly the same.
Using the latter may cause Foxmail import to fail.
*/
$i++;
}
exit;
}
?>


< ;head>

Export data to Foxmail address book< ;/head>
Clickhere to download the file, save and import it to your Foxmail address book.

How to operate

……



Run the above PHP file on the server, and open the downloaded "tofoxmail.csv" file with Notepad as shown in the figure.

Click "Tools" - "Import" - "CSV File" in the Foxmail address book... and a lot of data in the database will be imported at once. This is not a bad idea!

 Implementation code for importing data into Foxmail using PHP_PHP tutorial

 (The above programs have been tested in Apache+PHP4+mySQL and IIS+PHP4+mySQL.)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322417.htmlTechArticleRecently Xiaoyang made a PHP classmate directory, where students can add or modify their names, email addresses, OICQ, etc. information and save it in the mySQL database. One day Xiaoyang suddenly thought that if it could be used...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn