(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.
我目前在我的网站上有一个JQuery样式的自动完成搜索引擎,称为(fcbkcoco)。脚本的原始操作从.txt文件运行,查找手动输入的值并在自动完成结果中输出它们。但是,我希望脚本从MYSQL数据库中获取值,而不是手动输入.txt文件,因为我最终希望用户可以选择添加自己的值。
I have therefore made the Javascript call a .php file instead of .txt with this script in it:
因此,我使用此脚本将Javascript调用为.php文件而不是.txt:
//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)
所以我需要知道如何将这个JSON输出转换为data.txt(因此当数据库表增长时,.txt文件会在脚本运行时自动更新)
This is the final step in the search on my site so any help is much appreciated.
这是我网站搜索的最后一步,所以非常感谢任何帮助。
-Matt
4 个解决方案
#1
2
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:
我无法确定你想要保存到这个txt文件的数据位,但我会假设它是这样的:
{"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.
我认为您正在寻找的函数是fopen(),特别是使用'a'作为第二个参数。
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.
'a'仅供写作;将文件指针放在文件的末尾。如果该文件不存在,请尝试创建它。
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.
这里要考虑的一件事(其中包括)文件将需要适当的写访问权限。
#2
1
Instead of pointing the autocomplete to data.txt, why don't you just point it to your PHP page?
而不是将自动完成指向data.txt,为什么不将它指向您的PHP页面?
$("element").fcbkcomplete({
...
json_url: "fetched.php",
...
});
#3
1
You could use a combination of ob_start & ob_get_contents then put in a text file with file_put_contents
您可以使用ob_start和ob_get_contents的组合,然后使用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
或者如果它实际上是你想要保存到文件的有效json那么你在数组上使用json_encode
<?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));
?>
#4
0
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
如果我很好理解你基本上想把这个输出写成一个txt文件,对吧?在这种情况下,您不应使用echo函数,而应使用fopen和fwrite。看一下这个教程
#1
2
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:
我无法确定你想要保存到这个txt文件的数据位,但我会假设它是这样的:
{"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.
我认为您正在寻找的函数是fopen(),特别是使用'a'作为第二个参数。
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.
'a'仅供写作;将文件指针放在文件的末尾。如果该文件不存在,请尝试创建它。
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.
这里要考虑的一件事(其中包括)文件将需要适当的写访问权限。
#2
1
Instead of pointing the autocomplete to data.txt, why don't you just point it to your PHP page?
而不是将自动完成指向data.txt,为什么不将它指向您的PHP页面?
$("element").fcbkcomplete({
...
json_url: "fetched.php",
...
});
#3
1
You could use a combination of ob_start & ob_get_contents then put in a text file with file_put_contents
您可以使用ob_start和ob_get_contents的组合,然后使用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
或者如果它实际上是你想要保存到文件的有效json那么你在数组上使用json_encode
<?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));
?>
#4
0
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
如果我很好理解你基本上想把这个输出写成一个txt文件,对吧?在这种情况下,您不应使用echo函数,而应使用fopen和fwrite。看一下这个教程