你是想把留言写在一个文件中保存?
1。每次用追加的方式写入留言,留言之间需换行
2。每个留言的发言人、题目、内容等用特殊字符分隔
3。用下面函数读文件到一个数组中
array file ( string filename [, int use_include_path] )
4。数组每个元素为一条留言
5。用explode函数从留言中取出各字段信息
注意要有文件可写权限!
1。每次用追加的方式写入留言,留言之间需换行
2。每个留言的发言人、题目、内容等用特殊字符分隔
3。用下面函数读文件到一个数组中
array file ( string filename [, int use_include_path] )
4。数组每个元素为一条留言
5。用explode函数从留言中取出各字段信息
注意要有文件可写权限!
$filenum=fopen($file_name,"r");
flock($filenum,LOCK_SH);
$file_data=fread($filenum,filesize($file_name));
fclose($filenum);
return $file_data;
}
function writetofile($file_name,$data,$method="w") {
$filenum=fopen($file_name,$method);
flock($filenum,LOCK_EX);
$file_data=fwrite($filenum,$data);
fclose($filenum);
return $file_data;
}
大多使用分隔符来实现如同数据库的多列储存
如 author||title||email||content||...
提取时每行读入再用 explode() 提取分列信息
_______________
<?
$array1 = file('test_data.php');
$attay2 = explode('||',$array1[0]);
echo $attay2[2];
?>
_____________
test_data.php
_____________
11||22||33||44||55
_____________
姓名:aaa||性别:bbb||email:lklklk||姓名:BBB||性别:BBB||email:lklkl||....就这样,我问的是怎么样用二维数组(其它方法也行)将内容值列出来,比如想要姓名为BBB的姓名,怎么办?要不是不是我的存储方法有问题?
<?//city$MFIELD->city[en][1]="Hong Kong";
$MFIELD->city[en][2]="New York";
$MFIELD->city[en][3]="London";
$MFIELD->city[en][4]="Beijing";
$MFIELD->city[en][5]="Los Angeles";
$MFIELD->city[en][6]="Seattle";
$MFIELD->city[en][7]="Shanghai";$MFIELD->city[zhcn][1]="Ïã¸Û";
$MFIELD->city[zhcn][2]="ŦԼ";
$MFIELD->city[zhcn][3]="Â׶Ø";
$MFIELD->city[zhcn][4]="±±¾©";
$MFIELD->city[zhcn][5]="Âåɼí¶";
$MFIELD->city[zhcn][6]="Î÷ÑÅͼ";
$MFIELD->city[zhcn][7]="ÉϺ£";
$MFIELD->city[zhcn][8]="ÉîÛÚ";$MFIELD->city[zhtw][1]="­»´ä";
$MFIELD->city[zhtw][2]="¯Ã¬ù";
$MFIELD->city[zhtw][3]="­Û´°";
$MFIELD->city[zhtw][4]="¥_¨Ê";
$MFIELD->city[zhtw][5]="¬¥§üÁF";
$MFIELD->city[zhtw][6]="¦è¶®¹Ï";
$MFIELD->city[zhtw][7]="¤W®ü";$MFIELD->city[ko][1]="Hong Kong ";
$MFIELD->city[ko][2]="New York ";
$MFIELD->city[ko][3]="London ";
$MFIELD->city[ko][4]="Beijing ";
$MFIELD->city[ko][5]="Los Angeles ";
$MFIELD->city[ko][6]="Seattle ";
$MFIELD->city[ko][7]="Shanghai ";$MFIELD->city[ja][1]="Hong Kong ";
$MFIELD->city[ja][2]="New York ";
$MFIELD->city[ja][3]="London ";
$MFIELD->city[ja][4]="Seattle ";
$MFIELD->city[ja][5]="Los Angeles ";
$MFIELD->city[ja][6]="Seattle ";
$MFIELD->city[ja][7]="Shanghai ";
$MFIELD->city[ge][1]="Hong Kong ";
$MFIELD->city[ge][2]="New York ";
$MFIELD->city[ge][3]="London ";
$MFIELD->city[ge][4]="Beijing ";
$MFIELD->city[ge][5]="Los Angeles ";
$MFIELD->city[ge][6]="Seattle ";
$MFIELD->city[ge][7]="Shanghai ";$MFIELD->city[ru][1]=" Ãîíêîíã";
$MFIELD->city[ru][2]=" Íþ-éîðê";
$MFIELD->city[ru][3]=" Ëîíäîí";
$MFIELD->city[ru][4]=" Ïåêèí";
$MFIELD->city[ru][5]=" Íîøàäè";
$MFIELD->city[ru][6]=" Ùèÿòó";
$MFIELD->city[ru][7]=" Øàíõàé";
$MFIELD->city[ru][8]=" Øýíü÷æýí";$MFIELD->city[fr][1]=" Honkong ";
$MFIELD->city[fr][2]=" New York";
$MFIELD->city[fr][3]=" Londre";
$MFIELD->city[fr][4]=" Beijing";
$MFIELD->city[fr][5]=" Los Angeles ";
$MFIELD->city[fr][6]=" Seattle";
$MFIELD->city[fr][7]=" Shanghai";
$MFIELD->city[fr][8]=" Shengzheng";//country$MFIELD->country[en][1]="China";
$MFIELD->country[en][2]="USA";
$MFIELD->country[en][3]="Korea";
$MFIELD->country[en][4]="Australia";
$MFIELD->country[en][5]="Japan";
$MFIELD->country[en][6]="UK";
$MFIELD->country[en][7]="Thailand";$MFIELD->country[zhcn][1]="Öйú";
$MFIELD->country[zhcn][2]="ÃÀ¹ú";
$MFIELD->country[zhcn][3]="º«¹ú";
$MFIELD->country[zhcn][4]="°Ä´óÀûÑÇ";
$MFIELD->country[zhcn][5]="ÈÕ±¾";
$MFIELD->country[zhcn][6]="Ó¢¹ú";
$MFIELD->country[zhcn][7]="Ì©¹ú";$MFIELD->country[zhtw][1]="¤¤°ê";
$MFIELD->country[zhtw][2]="¬ü°ê";
$MFIELD->country[zhtw][3]="Áú°ê";
$MFIELD->country[zhtw][4]="¿D¤j§Q¨È";
$MFIELD->country[zhtw][5]="¤é¥»";
$MFIELD->country[zhtw][6]="­^°ê";
$MFIELD->country[zhtw][7]="®õ°ê";
.
.
.
.
.
.
$MFIELD->level[zhcn][1]="Ò»°ã»áÔ±";
$MFIELD->level[zhcn][2]="Òø¿¨»áÔ±";
$MFIELD->level[zhcn][3]="½ð¿¨»áÔ±";$MFIELD->level[zhtw][1]="¤@¯ë·|­û";
$MFIELD->level[zhtw][2]="»È¥d·|­û";
$MFIELD->level[zhtw][3]="ª÷¥d·|­û";$MFIELD->level[ko][1]="Regular ";
$MFIELD->level[ko][2]="Silver ";
$MFIELD->level[ko][3]="Gold ";$MFIELD->level[ja][1]="Regular ";
$MFIELD->level[ja][2]="Silver ";
$MFIELD->level[ja][3]="Gold ";
$MFIELD->level[ge][1]="Regular ";
$MFIELD->level[ge][2]="Silver ";
$MFIELD->level[ge][3]="Gold ";$MFIELD->level[ru][1]=" îáû÷íûé ÷ëåí";
$MFIELD->level[ru][2]=" ÷ëåí íà êàðòî÷êó ñåðåáðà";
$MFIELD->level[ru][3]=" ÷ëåí íà êàðòî÷êó çîëîòà";$MFIELD->level[fr][1]=" Membre général ";
$MFIELD->level[fr][2]=" Membre du Carte d'Argent";
$MFIELD->level[fr][3]=" Membre du Carte d'Or";
?>
<?
/*
Aim : Get the input rows of a maintainable field for a language, and then join
them with existing data. Finally, write all them to the mfields_array.php
Called by : mfields.php
Input : $mfield, $mfield_lang, $mfield_rows & all the fields of the table.
To call : write data to mfields_array.php / come back to mfields.php
Output : Nil
Steps : 1.) to join the original data with the new data from the form
2.) write all the data to the mfields_array.php
3.) come back to the mfields.php
Functions : 1. FormData ()
- to get the form's data, and format them as -> $MFIELD->city[en][1]="value"\n;
then put all of them into a string
*/// ****************************************************************************************************
include ("../../lib/application.php"); // always put this statement on the top
include ("mfields_array.php"); // Arrays of 9 maintainable fields require_login(); // check user has login or not
if ($SESSION[current]!=currentfile()) {
$SESSION[previous]=$SESSION[current]; // record the previous page
$SESSION[current]=currentfile(); // record the current page
}
require_priv($SESSION[current],true); // check user has enough right or no
// ******************************************************************************************************
// to store the form data in an array
$form=$HTTP_POST_VARS;
// to store all 9 maintaintable fields'name in an array
$mfields=array("city","country","chain","program","nature","feature","breakfast","region","level");
$typetxt=array("en","zhcn","zhtw","ko","ja","es","ge","ru","fr");
$string="<?\n"; // ## Step 1 ##
/*to join the original data with the new data from the form
*/
for ($x=0;$x<9;$x++) //do for all 9 maintainable fields x 3 type of languages
{ $string=$string."\n\n\n//$mfields[$x]\n"; for($t=0;$t<count($typetxt);$t++){
$language=$typetxt[$t];
$string=$string."\n";
if ($mfield_lang!="$mfields[$x][$language]")
{
$obj=$MFIELD->$mfields[$x];
for ($y=1;$y<=sizeof($obj[$language]);$y++) //en : English
{ $string =$string."\$MFIELD->$mfields[$x][$language][$y]="."\"".quotemeta($obj[$language][$y])."\";\n" ;
}
}
else
$string=$string.FormData($form);
}
}//end of for x
$string=$string."?>\n";// ## Step 2 ##
/* The file which you go to modify must be assigned to be the appache group
ie. chown nobody.nobody filename & chmod 0440 filename
So the apache can have access right to modify it.
*/
///////ÔÚÔ­°æÖд˴¦Ã»ÓÐÆÁ±Î/////////////////////////////////////////////
//chmod ("$CONFIG->root/rsa"."/mfields_array.php",0660); //change the read-only php to writable file
if ($file=fopen("mfields_array.php","w"))
{
fputs($file,$string);
///////ÔÚÔ­°æÖд˴¦Ã»ÓÐÆÁ±Î/////////////////////////////////////////////
// chmod ("$CONFIG->root/rsa"."/mfields_array.php",0440); //change the writable file to read-only now
}
else
echo '<font color="red" size="4"><B>Open file error !</B></font><BR>'; // ## Step 3 ##
// Come back
redirect($form[urllocation]);/***************************************** Function Part *******************************************/ /* to get the form's data, and format them as -> $MFIELD->city[en][1]="value"\n;
then put all of them into a string
*/
function FormData($form)
{
for ($x=1;$x<=$form[mfield_rows];$x++)
{
if (!empty($form[$form[mfield].$x]))
$inputstring = $inputstring."\$MFIELD->"."$form[mfield_lang]"."[".$x."]="."\"".$form[$form[mfield].$x]."\";\n" ; }
return $inputstring;
}//end of FormData()
?>
\n 或<br>怎么不行啊?