1

if i use the following code i got data in text file

{"title":"sankas","description":"sakars","code":"sanrs"}    
{"title":"test","description":"test","code":"test"}

but my code is working on

{"title":"sankas","description":"sakars","code":"sanrs"}

so i could not add more rows.where i want to change to get correct results.

        $info = array();
    $folder_name = $this->input->post('folder_name');
    $info['title'] = $this->input->post('title');
    $info['description'] = $this->input->post('description');
    $info['code'] = $this->input->post('code');
    $json = json_encode($info);
    $file = "./videos/overlay.txt";
    $fd = fopen($file, "a"); // a for append, append text to file

    fwrite($fd, $json);
    fclose($fd); 

1 Answer 1

3

use php's file_put_content() more information here http://php.net/manual/en/function.file-put-contents.php

Update : assuming that the data is correctly being passed. here is what you can do.

$info = array();
$folder_name = $this->input->post('folder_name');
$info['title'] = $this->input->post('title');
$info['description'] = $this->input->post('description');
$info['code'] = $this->input->post('code');
$json = json_encode($info);
$file = "./videos/overlay.txt";
//using the FILE_APPEND flag to append the content.
file_put_contents ($file, $json, FILE_APPEND);

Update 2:

if you want to access the value back from the text file. overlay.txt here is what you can do

$content = file_get_contents($file);

if you want to fetch title, code, and description separately. and if the string is in json then you need to convert it into array first by using.

//this will convert the json data back to array
$data = json_decode($json);

and to access individual value you can do it like this if you have one row

echo $data['title'];
echo $data['code'];
echo $data['description'];

if you have multiple rows then you can use php foreach loop

foreach($data as $key => $value)
{
    $key contains the key for example code, title and description
    $value contains the value for the correspnding key
}

hope this helps you.

Update 3:

do it like this

$jsonObjects = file_get_contents('./videos/overlay.txt');
$jsonData = json_decode($jsonObjects);
foreach ($jsonData as $key => $value) {
    echo $key . $value;
    //$key contains the key (code, title, descriotion) and $value contains its corresponding value
}
Sign up to request clarification or add additional context in comments.

13 Comments

i am asking correct answer with my code.use use file_put_contents() in my code.
$string = implode($json); file_put_contents("./videos/overlay.txt", $string); thism is not working
@Sankar Mvs i did that. and that should work. if i understand you correct then you want to append the json string in the end of file right?
you are using json_encode already to convert array to json. do you want it to convert into string?
how to retrive this value {"title":"asd","description":"asd","code":"asd"}{"title":"sad","description":"sadd","code":"sadd"}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.