0

Here is what I'm trying to achieve,

Read a text file using PHP and assign each line of it to an array value and print them,

<?php
$file = fopen("news.txt", "r");
$news = array();
$count = 0;
while (!feof($file)) {
    $count = $count + 1 ;
   $news[] = fgets($file);
}

fclose($file);
for ($x=1; $x<$count; $x++){
            echo $news[$x];
}
?>

The code is working fine but now I want to create an array in javascript, exactly equal to the size of the number of lines read using php ($count), here is my code for that,

var content=new Array()
    for (var i=0;i<<?php echo $count ?>;i++)

This javascript loop will iterate equal number of time as the number of lines in text file. Since it is clear from the code that each line of text is stored in a variable $news,

How can i assign each line of text from the variable $news to the array of inside the javascript ?

It should be something like this,

 var content=new Array()
    for (var i=0;i<<?php echo $count ?>;i++){
           content[i] = ***$news[i]***

How to assigned each line of text from PHP variable to the each variable of array content[] ?

3 Answers 3

2

You can simplify the code you use to convert a file to an array:

$news = file($file);

As for transferring this to JS you can use json.

var content = <?php echo json_encode($news);?>;

Remember to check the result of the call to file in case the file doesn't exist or is otherwise inaccessible.

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

5 Comments

var content = <?php echo json_encode($news);?>; you are assigning the array $news to the variable content , how can i access it after that ?
In Javascript it will be accessible through the content variable and will be an array. It will still be available in PHP as $news.
thanks a lot, it worked :) also, either i display content[0] or content[1] , it is displaying the same output.
This probably means the first two lines of the file are the same. Is this the case?
@Xufyan What do you get if you print_r($news)?
2

This is a wrong approach. Encode your array as a JSON string and pass it, like so:

var jsonString = <?php echo json_encode($news); ?>;

You can then manipulate the JSON object any way you want.

For example, to print all the contents:

var json = <?php echo json_encode($news); ?>;
for(var i=0; i < json.length; i++){
    console.log(json[i]);
}

1 Comment

JSON is legal javascript. There's no need to place it in a string and then decode it.
1

Just iterate the array in PHP and build the array in JS instead of printing with echo.

var content=new Array()
<?php for($i=0;$i < $count;$i++) { ?>
    content[<?php echo $i; ?>] = '<?php echo $news[$i]; ?>';
<?php } ?>

If you prefer you can also convert the array to JSON and use it in JS.

var content=<?php json_encode($news); ?>;

EDIT, COMPLETE CODE

Try using this:

var content=new Array();
<?php
$file = fopen("news.txt", "r");
$news = array();
while (!feof($file)) {
    echo "content.push('".fgets($file)."')";
}
fclose($file);
?>

Now JS variable content should be the array you're looking for.

1 Comment

your first approach seems alright but i don't know why it is not working, when i am manually displaying the content like, content[1] = 'ABC' , it works, but when i am using your approach then nothing is displaying

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.