多列关联数组的CSS格式

时间:2013-03-24 22:46:05

标签: php css textbox associative-array word-wrap

我正在尝试让以下PHP函数在带有自动换行的CSS滚动文本框中工作(这是整个程序示例 - 没有其他CSS)。

<?php

    function prettyPrint( $my_array ) {

    if (is_array($my_array)) {

        echo "<table style=border=0 cellspacing=2 cellpadding=1 width=100%>";
        echo '<tr><td colspan=2 style="background-color:#B29980;"></td></tr>';

        foreach ($my_array as $k => $v) {
            if (is_int($k)){$k=$k+1;}                    
            echo '<tr><td valign="top" style="width:20px;background-color:#F0F0F0;">';
            echo '<strong>' . "&nbsp;&nbsp;" . $k . "&nbsp;&nbsp;" . "</strong></td><td>";
            prettyPrint( $v ) ;
            echo "</td></tr>";
        }

        echo '<tr><td colspan=2 style="background-color:#B29980;"></td></tr>';
        echo "</table>";
        return;
    }

    echo $my_array;
}

$array = array( array ( "Txt1" => "Lorem ipsum dolor sit amet...
                        "Txt2" => "Lorem ipsum dolor sit amet...
                array ( "Txt3" => "Lorem ipsum dolor sit amet...
                        "Txt4" => "Lorem ipsum dolor sit amet...
prettyPrint($array); 

?>

我曾尝试在第3行添加:

echo '<div style="height:250px; width:980px; overflow:auto; overflow-x: hidden">';

这个div标签在分组项目之间增加了很大的空间,也没有自动换行。如何获取函数的输出并将其显示在单词包装的滚动文本框中?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

据我了解,你想要<div> - 容器周围的一切,对吧?

...一样

____________ this is the <div> ________________
|                                              |
| inside here is the nested table              |
| with text 1 to text 4                        |
|______________________________________________|

然后去:

echo '<div style="height:250px; width:980px; overflow:auto; overflow-x: hidden">';
prettyPrint($array);
echo '</div>';

你的函数prettyPrint是递归的,意味着多次从它自己调用。如果你在其中包含<div>,那么在嵌套表中最终会有几个<div> - 容器,这是一团糟。

相关问题