2013-07-06 2 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.

Я понимаю, что это может быть полностью сверху, что вы ищете, но оно прочное и (в основном) легко учиться.

Смежные вопросы