![](/img/trans.png)
[英]HTML/PHP - Validation - Basic validation requirements for e-commerce websites? (first and last name along with email)
[英]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.