你是想把留言写在一个文件中保存?
1。每次用追加的方式写入留言,留言之间需换行
2。每个留言的发言人、题目、内容等用特殊字符分隔
3。用下面函数读文件到一个数组中
array file ( string filename [, int use_include_path] )
4。数组每个元素为一条留言
5。用explode函数从留言中取出各字段信息
注意要有文件可写权限!

解决方案 »

  1.   

    function readfromfile($file_name) {
    $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;
    }
      

  2.   

    关键不是用二维数组,而是采用什么结构存贮数据
    大多使用分隔符来实现如同数据库的多列储存
    如 author||title||email||content||...
    提取时每行读入再用 explode() 提取分列信息
      

  3.   

    test.php
    _______________
    <?
    $array1 = file('test_data.php');
    $attay2 = explode('||',$array1[0]);
    echo $attay2[2];
    ?>
    _____________
    test_data.php
    _____________
    11||22||33||44||55
    _____________
      

  4.   

    我的文件是这样的 
       姓名:aaa||性别:bbb||email:lklklk||姓名:BBB||性别:BBB||email:lklkl||....就这样,我问的是怎么样用二维数组(其它方法也行)将内容值列出来,比如想要姓名为BBB的姓名,怎么办?要不是不是我的存储方法有问题?
      

  5.   

    这是所写的文本文件:
    <?//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]="&Iuml;&atilde;&cedil;&Ucirc;";
    $MFIELD->city[zhcn][2]="&Aring;&brvbar;&Ocirc;&frac14;";
    $MFIELD->city[zhcn][3]="&Acirc;×&para;&Oslash;";
    $MFIELD->city[zhcn][4]="±±&frac34;&copy;";
    $MFIELD->city[zhcn][5]="&Acirc;&aring;&Eacute;&frac14;í&para;";
    $MFIELD->city[zhcn][6]="&Icirc;÷&Ntilde;&Aring;&Iacute;&frac14;";
    $MFIELD->city[zhcn][7]="&Eacute;&Iuml;&ordm;&pound;";
    $MFIELD->city[zhcn][8]="&Eacute;&icirc;&Ucirc;&Uacute;";$MFIELD->city[zhtw][1]="&shy;&raquo;&acute;&auml;";
    $MFIELD->city[zhtw][2]="&macr;&Atilde;&not;ù";
    $MFIELD->city[zhtw][3]="&shy;&Ucirc;&acute;°";
    $MFIELD->city[zhtw][4]="&yen;_¨&Ecirc;";
    $MFIELD->city[zhtw][5]="&not;&yen;§ü&Aacute;F";
    $MFIELD->city[zhtw][6]="&brvbar;è&para;&reg;&sup1;&Iuml;";
    $MFIELD->city[zhtw][7]="¤W&reg;ü";$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]=" &Atilde;&icirc;íê&icirc;í&atilde;";
    $MFIELD->city[ru][2]=" &Iacute;&thorn;-é&icirc;&eth;ê";
    $MFIELD->city[ru][3]=" &Euml;&icirc;í&auml;&icirc;í";
    $MFIELD->city[ru][4]=" &Iuml;&aring;êèí";
    $MFIELD->city[ru][5]=" &Iacute;&icirc;&oslash;à&auml;è";
    $MFIELD->city[ru][6]=" &Ugrave;è&yuml;òó";
    $MFIELD->city[ru][7]=" &Oslash;àí&otilde;àé";
    $MFIELD->city[ru][8]=" &Oslash;&yacute;íü÷&aelig;&yacute;í";$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]="&Ouml;&ETH;&sup1;ú";
    $MFIELD->country[zhcn][2]="&Atilde;&Agrave;&sup1;ú";
    $MFIELD->country[zhcn][3]="&ordm;&laquo;&sup1;ú";
    $MFIELD->country[zhcn][4]="°&Auml;&acute;ó&Agrave;&ucirc;&Ntilde;&Ccedil;";
    $MFIELD->country[zhcn][5]="&Egrave;&Otilde;±&frac34;";
    $MFIELD->country[zhcn][6]="&Oacute;&cent;&sup1;ú";
    $MFIELD->country[zhcn][7]="&Igrave;&copy;&sup1;ú";$MFIELD->country[zhtw][1]="¤¤°ê";
    $MFIELD->country[zhtw][2]="&not;ü°ê";
    $MFIELD->country[zhtw][3]="&Aacute;ú°ê";
    $MFIELD->country[zhtw][4]="&iquest;D¤j§Q¨&Egrave;";
    $MFIELD->country[zhtw][5]="¤é&yen;&raquo;";
    $MFIELD->country[zhtw][6]="&shy;^°ê";
    $MFIELD->country[zhtw][7]="&reg;&otilde;°ê";
      

  6.   

    中间有删截
    .
    .
    .
    .
    .
    .
    $MFIELD->level[zhcn][1]="&Ograve;&raquo;°&atilde;&raquo;á&Ocirc;±";
    $MFIELD->level[zhcn][2]="&Ograve;&oslash;&iquest;¨&raquo;á&Ocirc;±";
    $MFIELD->level[zhcn][3]="&frac12;&eth;&iquest;¨&raquo;á&Ocirc;±";$MFIELD->level[zhtw][1]="¤@&macr;&euml;·|&shy;&ucirc;";
    $MFIELD->level[zhtw][2]="&raquo;&Egrave;&yen;d·|&shy;&ucirc;";
    $MFIELD->level[zhtw][3]="&ordf;÷&yen;d·|&shy;&ucirc;";$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]=" &icirc;á&ucirc;÷í&ucirc;é ÷&euml;&aring;í";
    $MFIELD->level[ru][2]=" ÷&euml;&aring;í íà êà&eth;ò&icirc;÷êó  &ntilde;&aring;&eth;&aring;á&eth;à";
    $MFIELD->level[ru][3]=" ÷&euml;&aring;í íà êà&eth;ò&icirc;÷êó &ccedil;&icirc;&euml;&icirc;òà";$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";
    ?>
      

  7.   

    写文件的方法
    <?
      /*
          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.
      */
        ///////&Ocirc;&Uacute;&Ocirc;&shy;°&aelig;&Ouml;&ETH;&acute;&Euml;&acute;&brvbar;&Atilde;&raquo;&Oacute;&ETH;&AElig;&Aacute;±&Icirc;/////////////////////////////////////////////
       //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);
     ///////&Ocirc;&Uacute;&Ocirc;&shy;°&aelig;&Ouml;&ETH;&acute;&Euml;&acute;&brvbar;&Atilde;&raquo;&Oacute;&ETH;&AElig;&Aacute;±&Icirc;/////////////////////////////////////////////
        // 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()
    ?>
      

  8.   

    哦,天啊,太复杂了,看不懂的了,只要简单一点的,怎么样分行存储呢?用
    \n 或<br>怎么不行啊?