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

6
  • 2
    Something with your title is wrong. HTML can't generate PHP. Commented Apr 18, 2012 at 15:05
  • What exactly are you wanting to save to the txt file? Commented Apr 18, 2012 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 Commented Apr 18, 2012 at 15:06
  • Related: use json_encode Commented Apr 18, 2012 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? Commented Apr 18, 2012 at 15:12

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 >.<)
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.
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.
Don't bother writing the [ ] to the file, fetch your file content into a string, then concatenate the string with [ ], $string = 'yourcodefromthefile'; $string = '[' . $string .']';
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?
|
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",
    ...
});

1 Comment

Then your JSON is probably wrong and putting it into a txt file will likely not help you.
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));
?>

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.