简体   繁体   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

I am new to PHP and have been struggling with echoing out a first name, last name, and email address as objects inside of a foreach loop. 我是PHP新手,一直在努力将名字,姓氏和电子邮件地址作为foreach循环内的对象来回显。 I have used a Profile.php class to define those and the get and set methods, and then I have a User.php class to set up a Username and Profile object and its get and set methods. 我使用了Profile.php类来定义那些以及get和set方法,然后使用User.php类来设置Username和Profile对象及其get和set方法。 I don't use the username object at all for my answer, according to people I have tried getting help from. 据我尝试获得帮助的人士说,我根本不使用用户名对象作为答案。 I believe that the problem is within the foreach loop in index.php, but I have worked for hours and cannot wrap my head around it yet. 我相信问题出在index.php的foreach循环内,但是我已经工作了几个小时,还无法解决这个问题。 Here is the UML for the program. 这是该程序的UML。

Here is what the program should ouput. 这是程序应输出的内容。

And here is the code itself: 这是代码本身:

index.php: 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: 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: 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;
  }
}

Thanks for the help and your time. 感谢您的帮助和您的宝贵时间。

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

As you can see, this array contains string (the names of professers), not objects. 如您所见,此数组包含string (教授的姓名),而不是对象。 Thus, in the foreach loop: 因此,在foreach循环中:

foreach($professors as $profile)

You will get the strings, "Andrew Besmer" , and then "Gerry Derksen" as the value of $profile variable, which is not what we want. 您将获得字符串"Andrew Besmer""Gerry Derksen"作为$profile变量的值,这不是我们想要的。

To solve this, you can define the $professors array after creating the objects, just before the foreach loop as follows: 为了解决这个问题,您可以在创建对象之后在foreach循环之前定义$professors数组,如下所示:

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

Then in the foreach loop, you will get User objects, and through which you can access Profile object: 然后在foreach循环中,您将获得User对象,并可以通过它访问Profile对象:

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

Hope this will help you get started to solve your problem. 希望这可以帮助您开始解决问题。

Instead of 代替

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

try using 尝试使用

$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