2

(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

Community
  • 1
  • 1
MattHeywood
  • 181
  • 1
  • 4
  • 18
  • 2
    Something with your title is wrong. HTML can't generate PHP. – markus Apr 18 '12 at 15:05
  • What exactly are you wanting to save to the txt file? – Dale Apr 18 '12 at 15:06
  • I think he meant the other way around, but I still believe something's wrong with the title, as he seems to be trying to convert JSON to Text file – Aleks G Apr 18 '12 at 15:06
  • Related: use [json_encode](http://us.php.net/manual/en/function.json-encode.php) – webbiedave Apr 18 '12 at 15:08
  • apologies I've edited the title I wrote it the wrong way round. Basically Im getting a html output from the above script data.php (pulling values from my table tblIngredient). Im wanting to know how I can use these values so they appear in the autocomplete results (It originally worked from a manually entered data.txt file). @webbiedave - how exactly does the json encode script work? – MattHeywood Apr 18 '12 at 15:12
  • You're not getting HTML output, but [JSON](http://en.wikipedia.org/wiki/JSON) output. I've edited the question title to reflect that. Also, you should be using PDO or MySQLI instead of `mysql*` functions as [they are deprecated](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Agi Hammerthief Dec 15 '14 at 11:57

4 Answers4

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:

{"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.

Dale
  • 10,384
  • 21
  • 34
  • This worked nicely and is now writing the data to a .txt file which is exactly what I wanted, however It's not appearing in the auto complete results. Im wondering if this is because my results are formatted like this: {"key": "vodka","value": "vodka"},{"key": "tequila","value": "tequila"},{ ..... This is missing spaces after each of the commas (bit of a silly question but what code do I use for a whitespace, &nbsp would be for html but I thought it was literally by hitting spacebar >.<) – MattHeywood Apr 18 '12 at 15:44
  • I'm not sure to be honest I would have assumed just hitting the space bar would be ok. I don't think the lack of a space is breaking it though. It may the way it being brought back into the page. – Dale Apr 18 '12 at 15:48
  • You've been a great help with this, quick question, How would I get a "[" at the start and a "]" at the end of the generated .txt file? $getData = mysql_query("SELECT Name FROM tblIndredient"); $start = '['; while($info = mysql_fetch_array($getData)) { $string = '{"key": "'.$info['Name'].'","value": "'.$info['Name'].'"}, '; $fp = fopen('data.txt', 'a'); fwrite($fp, $string); fclose($fp); } ....Im not sure how to do it so it appears just at the start and end without it appearing on every database entry. – MattHeywood Apr 18 '12 at 16:23
  • Don't bother writing the [ ] to the file, fetch your file content into a string, then concatenate the string with [ ], `$string = 'yourcodefromthefile'; $string = '[' . $string .']';` – Dale Apr 18 '12 at 16:29
  • Im not extremely experienced with PHP, how exactly do I go about fetching the content of data.txt and re-write it with the brackets? As the $string variable is only defined within the while loop (Does the code you pasted go within the while loop? – MattHeywood Apr 18 '12 at 16:50
  • `$string = '[' . file_get_contents('yourfile.txt') . ']';` – Dale Apr 18 '12 at 16:52
1

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",
    ...
});
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
1

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));
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
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

Naigel
  • 9,086
  • 16
  • 65
  • 106