2013-07-06 93 views
2

我在PHP对象使用如下代码5个属性:格式的HTML表格回声输出

<?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细胞表里面打电话了吗?

谢谢。

+0

你是否试过循环'$ person-> show_attributes()'并在表格中附加一个新的'​​'? – pdoherty926

回答

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> 
+0

Hummm,漂亮。我不知道你可以在PHP代码中使用​​标签。谢了哥们。 – Ashir

0

我不知道如何深入你正在寻找去,但您可以将数据加载到数据建模/表系统如http://backgridjs.com

我意识到这可能完全超出了你所期望的范围,但它很强大,并且(大部分)很容易学习。