Jump to content

verticaly align output text


Go to solution Solved by Psycho,

Recommended Posts

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 ?

Link to comment
https://forums.phpfreaks.com/topic/290110-verticaly-align-output-text/
Share on other sites

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 by Psycho
  • Solution
$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 by Psycho
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.