dongyang1518 2013-07-06 05:28
浏览 17
已采纳

格式化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.

  • 写回答

2条回答 默认 最新

  • douxian4323 2013-07-06 05:35
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存