2

我使用以下代码在 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 单元格表中调用它?

谢谢。

4

2 回答 2

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 代码:

<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>
于 2013-07-06T05:35:47.980 回答
0

我不知道您希望深入到什么程度,但是您可以将数据加载到数据建模器/表格系统中,例如http://backgridjs.com

我意识到这可能完全超出您正在寻找的东西,但它很强大并且(大部分)易于学习。

于 2013-07-06T05:38:00.347 回答