dourao1877
2012-04-18 15:03
浏览 831
已采纳

将PHP生成的JSON保存到文本文件中

(This question is following on from my previous problem which has been fixed (Here)

I found a similar question to this (Here) but the solution wasn't quite what I was looking for.

I currently have a JQuery styled autocomplete search engine on my site, called (fcbkcomplete). The original operation of the script ran from a .txt file, looked up the manually-entered values and outputted them in the autocomplete results. However, I want the script to get the values from a MYSQL database instead of being manually entered into a .txt file as I eventually want the user to have the option of adding their own values.

I have therefore made the Javascript call a .php file instead of .txt with this script in it:

//connection details

$getData = mysql_query("SELECT Name FROM tblIngredient"); 

 while($info = mysql_fetch_array($getData)) 
 {
     echo "{\"key: value:\";
     echo $info['Name'];
     echo "key: value:\"";
     echo $info['Name'];
     echo "}, ";
 }

This script works and outputs the data in the correct format:

{"key": "vodka","value": "vodka"}, {"key": "tequila","value": "tequila"},

So I need to know how I then convert this JSON output into data.txt (so as the database table grows, the .txt file is updated automatically when the script is run)

This is the final step in the search on my site so any help is much appreciated.

-Matt

图片转代码服务由CSDN问答提供 功能建议

(这个问题是我之前修复过的问题的后续问题(这里) \ n

我发现了一个与此相似的问题( 这里)但解决方案并不是我想要的。

我目前在我的网站上有一个JQuery样式的自动完成搜索引擎,名为(fcbkcomplete)。脚本的原始操作从.txt文件运行,查找手动输入的值 并在自动完成结果中输出它们。但是,我希望脚本从MYSQL数据库中获取值,而不是手动输入.txt文件,因为我最终希望用户拥有opti 因此,我添加了自己的值。

因此我使用此脚本将Javascript调用为.php文件而不是.txt:

   //连接细节
 
 $ getData = mysql_query(“SELECT Name FROM tblIngredient”);  
 
 while($ info = mysql_fetch_array($ getData))
 {
 echo“{\”key:value:\“; 
 echo $ info ['Name']; 
 echo”key:value  :\“”; 
 echo $ info ['Name']; 
 echo“},”; 
} 
   
 
 

此脚本工作并输出 数据格式正确:

  {“key”:“vodka”,“value”:“vodka”},{“key”:“tequila”,“value”  :“tequila”},
   
 
 

所以我需要知道如何将此JSON输出转换为data.txt(以便数据库表增长, 脚本运行时会自动更新.txt文件。

这是我网站搜索的最后一步,所以非常感谢任何帮助。

-Matt

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • dsfdfdfd6576578 2012-04-18 15:13
    最佳回答

    I can't really tell which bit of data you want to save to this txt file but I'm going to assume it's this:

    {"key": "vodka","value": "vodka"}, {"key": "tequila","value": "tequila"},
    

    If it isn't you can just substitute it for what you want.

    I think the function you are looking for is fopen() specifically using 'a' as the second argument.

    From the manual:

    'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

    Here's an example (a very brief one at that) of how to use it.

    $string = '{"key": "vodka","value": "vodka"}, {"key": "tequila","value": "tequila"}';
    $fp = fopen('somefile.txt', 'a');
    fwrite($fp, $string);
    fclose($fp);
    

    One thing to consider here (amongst others) is the file will need the proper permissions for write access.

    评论
    解决 无用
    打赏 举报
查看更多回答(3条)

相关推荐 更多相似问题