PHP 객체에 정보를 전달하는 기본 html 양식을 작성하려고합니다. 그런 다음 객체를 배열에 추가합니다. 그것은 양식에서 정보를 객체로 전달하고 배열에 추가하고 객체를 표시하는 지점까지 작동합니다. 그러나 두 번째 객체를 배열에 추가하려고하면 배열을 새로운 단일 요소 배열로 대체하는 대신 오히려 추가하는 것으로 보입니다. 여기 내 코드가있다 ... 어떤 아이디어?PHP : Array가 다시 초기화되는 것으로 보입니다.
index.php 파일 :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Forms</title>
</head>
<body>
<h2>Add Data</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
<button type="submit" name="submit" value="client">Submit</button>
</form>
<?php
include_once 'clientInfo.php';
include_once 'clientList.php';
if ($_POST) {
$clientArray[] = new clientInfo($_POST["Fname"], $_POST["Lname"]);
}
if (!empty($clientArray)) {
$clientList = new clientList($clientArray);
}
?>
<p><a href="clientList.php">go to client list</a></p>
</body>
</html>
clintInfo.php 파일 :
<?php
class clientInfo {
private$Fname;
private$Lname;
public function clientInfo($F, $L) {
$this->Fname = $F;
$this->Lname = $L;
}
public function __toString() {
return $this->Fname . " " . $this->Lname;
}
}
?>
clientList.php 파일 :
<?php
class clientList {
public function clientList($array) {
foreach($array as $c) {
echo $c;
}
}
}
?>
EDITED WORKING CODE 대답
index.php 파일 :
<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Forms</title>
</head>
<body>
<h2>Add Data</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
<button type="submit" name="submit" value="client">Submit</button>
</form>
<?php
if ($_POST) {
$testClient = new clientInfo($_POST["Fname"], $_POST["Lname"]);
echo $testClient . " was successfully made. <br/>";
$_SESSION['clients'][] = $testClient;
echo end($_SESSION['clients']) . " was added.";
}
?>
<p><a href="clientList.php">go to client list</a></p>
</body>
</html>
clientList.php 파일 :
<?php
include('clientInfo.php');
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>
Accessing session variables
</title>
</head>
<body>
<h1>
Content Page
</h1>
<?php
for ($i = 0; $i < sizeof($_SESSION['clients']); $i++) {
echo $_SESSION['clients'][$i] . " was added. <br/>";
}
?>
<p><a href="index.php">return to add data</a></p>
</body>
</html>
는 오브젝트 파일 clientInfo.php는 동일 머물렀다. 다차원 $ _SESSION 배열에 저장해야 할 객체를 for 루프, foreach 루프로 호출하면 foreach 루프 작업을 수행하는 방법을 알고있는 다른 사람이 아니라면 foreach 루프가 작동하지 않습니다. 부가 메모에서 $ testClient 변수는 건너 뛰고 방금 생성하여 $ _SESSION과 함께 배치 할 수 있지만 임시 변수를 사용하면 쉽게 문제를 일으키고 어떻게 작동시키는 지 알 수 있습니다. Josh가 제공 한 답변으로 작업 코드를 게시 할 것입니다.
나는 그것을 생각하기 시작했다. 그러나 나는 새로운 사람이기 때문에 그것을 피하려고 노력했다. 세션 변수로 추가 할 수있는 작업을 살펴 보겠습니다. 감사! – defaultNINJA
@defaultNINJA : 다행스럽게도 행운을 빈다. 그리고 여기에 새로운 사람인 것 같아서 녹색 체크 표시를 클릭하여 질문의 답변을 올바르게 표시하는 것을 잊지 마십시오. 이것은 나중에 귀하의 질문을 찾는 사람들에게 도움이됩니다. – Josh
좋습니다. 예, 저는 JAVA와 C++로 프로그래밍을 해왔습니다. 지금은 처음으로 프로그래밍 작업을했고, PHP 작업을 해보고 싶습니다. 이제 OOP 기술을 JAVA와 C++에서 PHP로 변환하려고합니다! 즐거운 시간 :-) – defaultNINJA