Jump to content

foreachs in to file (.txt)


Jeisson

Recommended Posts

Hello

I have problem to get the whole loop saved in to a file. I seams to save only the last "section"

So I have two json files that I decode and managed to echo as I wanted.

Now plans changed and I want to save what was echoed in to a file instead.

I planned to use file_put_contents for that.

My code looks like this but it did not put the whole list in to the file. just one.

(I show you only the foreach and attempted file saving.

<code>

foreach ( $venues as $item){

$s1 = '<li style="padding-top:15px;">' . '<a target="_blank" href="http://foursquare.com/v/' . $item['id'] . '">' . $item['name'] . '</a></li>';

$endpoint2 = 'venues/' . $item['id'] . '/tips';

$params2 = '?limit=1';

$url2 = $base_url.$endpoint2.$params2.$auth;

$results2 = file_get_contents($url2);

$json_results2 = json_decode($results2,true);

$venues2 = $json_results2['response']['tips']['items'];

 

foreach ( $venues2 as $ti){

$s2 = '<li>' . '<span>' . date('j.n.Y',$ti['createdAt']) . '</span>' . ' - '

. $ti['text'] . '</li>';

}

}

 

file_put_contents('output.txt',$s1 . $s2);

//echo $s1 . $s2; //for quick check

</code>

 

If I remove the $s1 and respectively $s2 and put the echo instead I get the whole result

on my screen. So why is this only taking the last <li><a href="url and result and so on in to the file?

 

This forum did not allow me to format my text, so appologises for that.

Edited by Jeisson
Link to comment
Share on other sites

I was hasting. Did not notice thius does not give desired result.

this system gives all info, but it makes two different list.

first all of $s1 then all of $s2

what I wanted is like the echo did. ie.

$s1 Venue1

$s2 Date1 - Ti1

$s1 Venue2

$s2 Date2 - Ti2

$s1 Venue3

$s2 Date3 - Ti3

and so on

 

 

before your foreach()

 

$s2 = '';

 

inside your foreach(), change to... ( missing concentration operator ('.') ) after $s2 .=

 

$s2 .= '<li>' . '<span>' . date('j.n.Y',$ti['createdAt']) . '</span>' . ' - '
. $ti['text'] . '</li>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.