格式化HTML表格中的回声输出
I have an object in PHP with 5 attributes using the following code:
<?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();
?>
I'm calling this class using the following 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 $person->show_attributes();
?>
</body>
</html>
Now, that will print something like
Male, Latin, 1.83 cm, 85 kg, Brown
But I want to print something like
--------------------------------------
|Male | Latin | 1.83 cm | 85 kg | Brown|
--------------------------------------
Using a HTML table.
I have try a couple of things, but I can't make it happend.
Is there a way to force
echo $person->show_attributes();
to only show one attribute so I can call it from inside a HTML cell table?
Thanks.
我在PHP中有一个具有5个属性的对象,使用以下代码: p>
&lt;?php
class Person
{
private $ gender,$ race,$ height,$ weight,$ eyes_color;
公共函数启动($ gender,$ race,$ height,$ weight, $ eyes_color)
{
$ this-&gt; gender = $ gender;
$ this-&gt; race = $ race;
$ this-&gt; height = $ height;
$ this-&gt; weight = $ weight;
$ this-&gt; eyes_color = $ eyes_color;
}
公共函数show_attributes()
\ n返回sprintf(“%s,%s,%s,%s,%s”,$ this-&gt;性别,$ this-&gt;竞赛,$ this-&gt;高度,$ this-&gt;重量,$ this-&gt; eyes_color);
}
}
$ person = new person();
?&gt;
code> pre>
我正在调用此 使用以下HTML代码的类 p>
&lt; html&gt;
&lt; head&gt;
&lt; meta http-equiv =“Content-Type”content =“text / html ; charset = UTF-8“&gt;
&lt; title&gt; Class Person&lt; / title&gt;
&lt; / head&gt;
&lt; body&gt;
&lt;?php
require_once(”Person.php“) ;
$ person-&gt; start(“男性”,“拉丁语”,“1.83 cm”,“85 kg”,“Brown”);
echo $ person-&gt; show_attributes();
?&gt;
&lt; / body&gt;
&lt; / html&gt;
code> pre>
现在,这将打印类似 p>
男,拉丁文,1.83厘米,85公斤,布朗
code> pre>
但我想要公关 int类似 p>
--------------------------------- -----
|男| 拉丁文| 1.83厘米| 85公斤| 布朗|
--------------------------------------
code> pre>
使用HTML表。 p>
我尝试了几件事,但我无法实现。 p>
有没有办法强制 p>
echo $ person-&gt; show_attributes();
code> pre>
只显示一个属性,以便我可以从HTML单元格表中调用它? p>
谢谢。 p>
div>
Try this
<?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>
I don't know how in-depth you're looking to go, but you can load the data into a data modeler/table system like http://backgridjs.com.
I realize that may be completely over the top for what you're looking for, but it's robust and (mostly) easy to learn.