Jeisson Posted October 26, 2015 Share Posted October 26, 2015 (edited) 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 October 26, 2015 by Jeisson Quote Link to comment https://forums.phpfreaks.com/topic/298839-foreachs-in-to-file-txt/ Share on other sites More sharing options...
printf Posted October 26, 2015 Share Posted October 26, 2015 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/298839-foreachs-in-to-file-txt/#findComment-1524374 Share on other sites More sharing options...
Jeisson Posted October 26, 2015 Author Share Posted October 26, 2015 wau beautiful. so many details in this language. I have not yet tried it but sounded convincing. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/298839-foreachs-in-to-file-txt/#findComment-1524392 Share on other sites More sharing options...
Jeisson Posted October 26, 2015 Author Share Posted October 26, 2015 Tried it. worked partially. but full solution was simply to do the same "trick" fo $s1 and that was it. Quote Link to comment https://forums.phpfreaks.com/topic/298839-foreachs-in-to-file-txt/#findComment-1524400 Share on other sites More sharing options...
Jeisson Posted October 27, 2015 Author Share Posted October 27, 2015 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/298839-foreachs-in-to-file-txt/#findComment-1524453 Share on other sites More sharing options...
Jeisson Posted October 27, 2015 Author Share Posted October 27, 2015 Resolved. I didn´t find trough googeling nyesterday but today I found easily an alternative. I use before the desired content ob_start(); and after $content = ob_get_contents(); file_put_contents('output.txt',$content); Quote Link to comment https://forums.phpfreaks.com/topic/298839-foreachs-in-to-file-txt/#findComment-1524460 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.