格式化HTML表格中的回声输出

时间:2013-07-06 05:28:53

标签: php html oop

我在PHP中有一个具有5个属性的对象,使用以下代码:

<?php
class Person
{
    private $gender, $race, $height, $weight, $eyes_color;
    public function start ($gender,$race,$height, $weight, $eyes_color)
    {
        $this->gender=$gender; 
        $this->race=$race; 
        $this->height=$height;
        $this->weight=$weight;
        $this->eyes_color=$eyes_color;
    }
    public function show_attributes()
    { 
    return sprintf("%s, %s, %s, %s, %s", $this->gender, $this->race, $this->height, $this->weight,$this->eyes_color);
    }
}
$person=new person();
?>

我正在使用以下HTML代码调用此类

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Class Person</title>
    </head>
    <body>
        <?php
        require_once("Person.php");
        $person->start("Male","Latin","1.83 cm","85 kg","Brown");
        echo $person->show_attributes();
        ?>
    </body>
</html>

现在,这将打印类似

的内容
Male, Latin, 1.83 cm, 85 kg, Brown

但我想要打印像

这样的东西
 --------------------------------------
|Male | Latin | 1.83 cm | 85 kg | Brown|
 --------------------------------------

使用HTML表格。

我尝试了几件事,但我无法实现。

有没有办法强迫

echo $person->show_attributes();

只显示一个属性,以便我可以从HTML单元格表中调用它?

感谢。

2 个答案:

答案 0 :(得分:3)

试试这个

<?php
class Person
{
    private $gender, $race, $height, $weight, $eyes_color;
    public function start ($gender,$race,$height, $weight, $eyes_color)
    {
        $this->gender=$gender; 
        $this->race=$race; 
        $this->height=$height;
        $this->weight=$weight;
        $this->eyes_color=$eyes_color;
    }
    public function show_attributes()
    { 
    return sprintf("<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>", $this->gender, $this->race, $this->height, $this->weight,$this->eyes_color);
    }
}
$person=new person();
?>

HTML code:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Class Person</title>
    </head>
    <body>
        <?php
        require_once("Person.php");
        $person->start("Male","Latin","1.83 cm","85 kg","Brown");
        echo "<table>":
        echo "<tr>";
        echo $person->show_attributes();
        echo "</tr>";
        echo "</table>";
        ?>
    </body>
</html>

答案 1 :(得分:0)

我不知道您有多深入,但您可以将数据加载到http://backgridjs.com等数据建模器/表格系统中。

我意识到,对于你正在寻找的东西,它可能完全超出顶部,但它很强大且(大多数)容易学习。