繁体   English   中英

如何使用foreach循环在PHP和HTML数组中回显名字,姓氏和电子邮件地址对象

[英]How to use a foreach loop to echo first name, last name, and email address objects in an array in PHP and along with HTML

我是PHP新手,一直在努力将名字,姓氏和电子邮件地址作为foreach循环内的对象来回显。 我使用了Profile.php类来定义那些以及get和set方法,然后使用User.php类来设置Username和Profile对象及其get和set方法。 据我尝试获得帮助的人士说,我根本不使用用户名对象作为答案。 我相信问题出在index.php的foreach循环内,但是我已经工作了几个小时,还无法解决这个问题。 这是该程序的UML。

这是程序应输出的内容。

这是代码本身:

index.php文件:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>

        <?php

        require_once('User.php');
        require_once('Profile.php');

        $professors = array("Andrew Besmer", "Gerry Derksen");

        $andrewUser = new User();
        $andrew = new Profile();

        $andrewUser->setProfile($andrew);

        $andrew->setFirstName("Andrew");
        $andrew->setLastName("Besmer");
        $andrew->setEmailAddress("besmera@fakeemail.com");

        $gerryUser = new User();
        $gerry = new Profile();

        $gerryUser->setProfile($gerry);

        $gerry->setFirstName("Gerry");
        $gerry->setLastName("Derksen");
        $gerry->setEmailAddress("derkseng@fakeemail.com");

        foreach($professors as $profile) {
            echo "<h2>" . $profile . "</h2>";
            echo "<ul>";
                echo "<li><b>First:</b>" . $profile->getFirstName() . "</li>";
                echo "<li><b>Last:</b>" . $profile->getLastName() . "</li>";
                echo "<li><b>Email:</b>" . $profile->getEmailAddress() . "</li>";
            echo "</ul>";
        }
        echo "<br>";
        ?>

    </body>
</html>

Profile.php:

<?php
class Profile { 
    protected $firstName = "";
    protected $lastName = "";
    protected $emailAddress = "";

    public function __construct(){

    }

    public function getFirstName(){
        return $this->firstName;
    }

    public function setFirstName($firstName){
        $this->firstName = $firstName;
    }

    public function getLastName() {
        return $this->lastName;
    }

    public function setLastName($lastName) {
        $this->lastName = $lastName;
    }

    public function getEmailAddress() {
        return $this->emailAddress;
    }

    public function setEmailAddress($emailAddress) {
        $this->emailAddress = $emailAddress;
    }
}
?>

user.php的:

class User {

protected $username = "";
protected $professors = array();

public function __construct(){

}

public function getUsername(){
    return $this->username;
}

public function setUsername($username) {
    $this->username = $username;
}

public function getProfile(){
    return $this->profile;
}

public function setProfile($profile){
    $this->profile = $profile;
  }
}

感谢您的帮助和您的宝贵时间。

$professors = array("Andrew Besmer", "Gerry Derksen");

如您所见,此数组包含string (教授的姓名),而不是对象。 因此,在foreach循环中:

foreach($professors as $profile)

您将获得字符串"Andrew Besmer""Gerry Derksen"作为$profile变量的值,这不是我们想要的。

为了解决这个问题,您可以在创建对象之后在foreach循环之前定义$professors数组,如下所示:

$professors  = array($andrewUser, $gerryUser);

然后在foreach循环中,您将获得User对象,并可以通过它访问Profile对象:

foreach ($professors as $user) {
    echo $user->profile->getEmailAddress();
}

希望这可以帮助您开始解决问题。

代替

 $professors = array("Andrew Besmer", "Gerry Derksen");

尝试使用

$professors[] = $andrewUser;
$professors[] = $gerryUser;
foreach ($professors as $professor) {
  echo "<h2>" . $professor->profile->getFirstName() . "</h2>";
  etc...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM