Homer5 Posted July 25, 2014 Share Posted July 25, 2014 I've managed to upload .csv file, process it, and display output ... but I'm puzzled how to make it look nice so it can be exported (save as txt) It's indexed array of associative arrays, so I am doing output like this: for ($i=0; $i<=count($filtered_array)-1; $i++) { echo $filtered_array[$i]['position'] . ") " . $filtered_array[$i]['Title'] . " ---> " . $filtered_array[$i]['URL'] . "<br />"; } output I get is: 1) Sanctum ---> http://www.imdb.com/title/tt0881320/ 2) Horrible Bosses ---> http://www.imdb.com/title/tt1499658/ 3) Pirates of the Caribbean: On Stranger Tides ---> http://www.imdb.com/title/tt1298650/ 4) Mission: Impossible - Ghost Protocol ---> http://www.imdb.com/title/tt1229238/ 5) Headhunters ---> http://www.imdb.com/title/tt1614989/ Output I'd like looks something like this: 1) Sanctum ---> http://www.imdb.com/title/tt0881320/ 2) Horrible Bosses ---> http://www.imdb.com/title/tt1499658/ 3) Pirates of the Caribbean: On Stranger Tides ---> http://www.imdb.com/title/tt1298650/ 4) Mission: Impossible - Ghost Protocol ---> http://www.imdb.com/title/tt1229238/ 5) Headhunters ---> http://www.imdb.com/title/tt1614989/ How would you do it ? Could it be done easier with help of some HTML ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 25, 2014 Share Posted July 25, 2014 printf() lets you pad strings to a fixed length. However, you'll need to loop through the array twice to first get the longest title. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted July 25, 2014 Share Posted July 25, 2014 Best to use a table for tabular data. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 25, 2014 Share Posted July 25, 2014 (edited) Best to use a table for tabular data. Except he stated he wants to save it as a txt file. @Homer5: You'll also want to take into account the length of the longest position. And if you are saving it as a text file you should use \n instead of the HTML line break - <br> Edited July 25, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted July 25, 2014 Solution Share Posted July 25, 2014 (edited) $filtered_array = array( array('position'=>'1', 'Title' => 'Sanctum', 'URL'=>'http://www.imdb.com/title/tt0881320/'), array('position'=>'2', 'Title' => 'Horrible Bosses', 'URL'=>'http://www.imdb.com/title/tt1499658/'), array('position'=>'3', 'Title' => 'Pirates of the Caribbean: On Stranger Tides', 'URL'=>'http://www.imdb.com/title/tt1298650/'), array('position'=>'4', 'Title' => 'Mission: Impossible - Ghost Protocol', 'URL'=>'http://www.imdb.com/title/tt1229238/'), array('position'=>'123', 'Title' => 'Headhunters', 'URL'=>'http://www.imdb.com/title/tt1614989/') ); $positioinTail = ')'; //Detemine the max lengths $positionLength = 0; $titleLength = 0; foreach($filtered_array as $row) { $positionLength = max($positionLength, strlen($row['position'].$positioinTail)); $titleLength = max($titleLength, strlen($row['Title'])); } //Output fixed width data foreach($filtered_array as $row) { $output .= sprintf("%-{$positionLength}s", $row['position'].$positioinTail) . ' '; $output .= sprintf("%-{$titleLength}s", $row['Title']) . ' '; $output .= '---> ' . $row['URL'] . "\n"; } echo $output; Output 1) Sanctum ---> http://www.imdb.com/title/tt0881320/ 2) Horrible Bosses ---> http://www.imdb.com/title/tt1499658/ 3) Pirates of the Caribbean: On Stranger Tides ---> http://www.imdb.com/title/tt1298650/ 4) Mission: Impossible - Ghost Protocol ---> http://www.imdb.com/title/tt1229238/ 123) Headhunters ---> http://www.imdb.com/title/tt1614989/ NOTE: That is the right output if you are wanting to write to a txt file or provide the user in a download. For it to 'display' on a web page as you wish you will need to wrap the output in PRE tags. Edited July 25, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
Homer5 Posted July 25, 2014 Author Share Posted July 25, 2014 @Psycho - you forgot $output = ""; before final foreach loop, but other than that your code works like a charm ! Thanks a lot ! Quote Link to comment 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.