将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

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.

If I well understood you basically want to write this output to a txt file, right? In this case you shouldn't use echo function but fopen and fwrite. Take a look to this TUTORIAL

Instead of pointing the autocomplete to data.txt, why don't you just point it to your PHP page?

$("element").fcbkcomplete({
    ...
    json_url: "fetched.php",
    ...
});

You could use a combination of ob_start & ob_get_contents then put in a text file with file_put_contents

<?php
$getData = mysql_query("SELECT Name FROM tblIngredient");
ob_start();
while($info = mysql_fetch_array($getData))
{
    echo "{\"key: value:\"";
    echo $info['Name'];
    echo "key: value:\"";
    echo $info['Name'];
    echo "}, ";
}
$return = ob_get_contents();
ob_end_clean();

file_put_contents('./data.txt',$return);
?>

Or if its valid json you actually want to save to file then you use json_encode on the array

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

while($info = mysql_fetch_array($getData)){
    $result[]=$info['Name'];
}
file_put_contents('./data.txt',json_encode($result));
?>