开发者

Using <pre> tag to display data in columns?

I am working with PHP, and I want to display some data I have in an array (from a mysql db). The problem is that the first column not always have the same length, so while some rows containing shorter strings in the first column are all well aligned, in those with larger strings the second column gets aligned more to the right. The array looks like this:

$array[0][0] = "string0gvmdksd";
$array[0][1] = "42134";
$array[1][0] = "string1fges";
$array[1][1] = "234";
开发者_开发百科$array[2][0] = "string2gvmdksd sgdfsgdfg gffgdfg";
$array[2][1] = "87342";
$array[3][0] = "string3gffd";
$array[3][1] = "34";

I though about a solution. Getting the length of the longer string in the array[$i][0], then setting a variable with X number of spaces and a tab at the end (X is the longer string length), but that doesn't seem to work.

Here's my code:

function addTabs($input) {
    $length = 0;
    foreach($input as $entry) {
        if(strlen($entry[0])>$length) $length = strlen($entry[0]);
    }

    return str_repeat(" ", $length)."\t";
}

$data = "";
$tabs = addTabs( $array );
foreach( $array as $entry ) {
    $data .=  $entry[0] . $tabs . $entry[1] . "\n";
}

echo "<pre>$data</pre>";

But it displays:

string0gvmdksd                                  42134
string1fges                                 234
string2gvmdksd sgdfsgdfg gffgdfg                                    87342
string3gffd                                 34

Anyone has a solution for this?


If you really have to use <pre> tag then its best to use sprintf function like this:

function maxLen($input) {
    $length = 0;
    foreach($input as $entry)
        if(strlen($entry[0])>$length) $length = strlen($entry[0]);
    return $length;
}
$len = maxLen($array);
foreach( $array as $entry ) {
    $data .=  sprintf("%-$len"."s\t%s\n", $entry[0], $entry[1]);
}
echo "<pre>$data</pre>";

OUTPUT

string0gvmdksd                      42134
string1fges                         234
string2gvmdksd sgdfsgdfg gffgdfg    87342
string3gffd                         34


ehh, why not put these in an HTML table?

<table>
   <tr>
      <td>row 1 column 1</td>
      <td>row 1 column 2</td>
   </tr>
   <tr>
      <td>row 2 column 1</td>
      <td>row 1 column 2</td>
   </tr>
</table>


You might want to try HTML’s <table> tag instead:

  • http://www.w3.org/TR/html4/struct/tables.html
  • http://www.quackit.com/html/tags/html_table_tag.cfm

Get the markup right, and your columns will be aligned nicely automatically.

<table>
    <tr>
        <td>string0gvmdksd</td>
        <td>42134</td>
    </tr>
    <tr>
        <td>string1fges</td>
        <td>234</td>
    </tr>
    <tr>
        <td>string2gvmdksd sgdfsgdfg gffgdfg</td>
        <td>87342</td>
    </tr>
    <tr>
        <td>string3gffd</td>
        <td>34</td>
    </tr>
</table>


PHP has a function for padding text (str_pad), so what you want to do is loop through the array once to work out the longest value for each column, and then a second time to display it:

// work out the max length for each column
$columnWidth = array();
foreach ($array as $row) {
    foreach ($row as $index => $value) {
        if (!isset($columnWidth[$index]) || $columnWidth[$index] < strlen($value)) {
            $columnWidth[$index] = strlen($value);
        }
    }
}

// display the data
foreach ($array as $row) {
    foreach ($row as $index => $value) {
        echo str_pad($value, $columnWidth[$index]);
    }
    echo "\n";
}

you'll probably want a small gap between each column, in which case just add some spaces after the echo.

A HTML table would be much easier of course!


The tab is not needed, and you forgot to subtract the width of the content of the first column!

Perhaps you meant this:

function widthOfFirstColumn($input) {
    $length = 0;
    foreach($input as $entry) {
        if(strlen($entry[1])>$length) $length = strlen($entry[1]);
    }

    return $length;
}

$data  = "";
$width = widthOfFirstColumn($array);
foreach ($array as $entry) {
    $spaces = str_repeat(" ", $width - strlen($entry[0]));
    $data .=  $entry[0] . $spaces . $entry[1] . "\n";
}

Of course, this isn't a great way to present tabular data: <table> is!


Assuming what you want to do is display the data WITHOUT the table borders, create the table as others have suggested and set the style so there are no borders and no padding between cells. Just creating a table and populating it will result in the default borders, padding and cell spacing, which I suspect is not what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜