2013-08-31 4 views
0

폼에서 입력을 받아서 PHP 배열에 저장하고 배열을 인쇄하고 싶습니다. 두 개의 파일, 즉 array.php와 marks.php가 있습니다. 정적 인 것으로 생각합니다. 도와주세요!이 PHP 코드의 출력을 얻으려면

marks.php

<style type="text/css"> 
.my_table{ 
margin-top:150px; 
margin-left:400px; 
} 
</style> 
<?php 
if(!isset($_POST['send'])){ 
?> 
<table align='center' class='my_table'> 
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> 
<tr><th></th><th align='left'>Student Marks</th></tr> 
<tr><td>enter subject</td><td><input type='text' name='subject'></td></tr> 
<tr><td>enter marks</td><td><input type='text' name='marks'></td></tr> 
<tr><td colspan='2' align='right'><input type='submit' value='submit' name='send'/>  </td></tr> 
</form> 
</table> 
<?php 
} 
else{ 
    include 'array.php'; 
    $svtoarray=new SaveMarks(); 
$svtoarray->addToArray($_POST['marks']); 
    $svtoarray->printArray(); 
    } 
?> 

array.php

<?php 
class SaveMarks{ 
static $index=0; 
function SaveMarks(){ 
    } 
static $marks=array(); 
function addToArray($value){ 
    $marks[$index]=$value; 
    $index++; 
    } 

function printArray(){ 
    $countarr=count($marks); 
    for($ind=0;$ind<$countarr;$ind++){ 
     print $marks[ind]; 
     } 
    } 
} 

?> 
+0

$ _POST 자체는 배열입니다. 왜 모든 복잡한 것들? –

+0

나는'

은''
'보다 더 나은 생각합니다. (그냥 느낌 : P) – Mageek

답변

0

반원 (메소드 또는 속성)을 선언하고 그 값은 클래스 정의로부터 직접 액세스 할 수 있으며 않는다는 것을 의미 인스턴스화 할 필요가 없습니다. 반대로 정적 멤버는 인스턴스화에서 액세스 할 수 없습니다. 정적

SaveMarks : $ 지수 또는 SaveMarks : $ 표시로 인덱스 마크를 선언 becuase 즉 이

하지만 $ svtoarray = 새로운 SaveMarks 괜찮습니다(); $ svtoarray-> 지수와 $ svttoarray-> 마크는

당신이 "공개"로 선언해야한다 인스턴스화 된 객체에 액세스 할 수 있도록하는 대신 "정적"의

PHP manual for static keyword를 참조 괜찮 없습니다. "순수한"구현은 표시를 비공개로 선언하고 속성의 직접 수정을 허용하지 않고 공용 "setMarks"메서드를 정의하는 것입니다.

관련 문제