TIL
[PHP] isset 함수와 $_SERVER['PHP_SELF'] : 학년별로 데이터 불러오기
IMRUNNING
2024. 7. 6. 18:43
반응형
isset
isset 함수는 PHP에서 변수가 설정되었는지 (즉, 존재하고 NULL이 아닌지) 확인하는 데 사용된다.
true 또는 false를 반환한다.
$cf_community_grade = isset($_GET['cf_community_grade']) ? (int)$_GET['cf_community_grade'] : $config['cf_community_grade'];
여기서 사용된 isset 함수는 $_GET['cf_community_grade']
가 설정되었는지를 확인하기 위해 사용되었다. 이 경우 $_GET['cf_community_grade']
가 설정되어 있으면 해당 값을 사용하고, 그렇지 않으면 $config['cf_community_grade']
의 기본값을 사용하게 된다.
isset($_GET['cf_community_grade'])
: $_GET['cf_community_grade']가 설정되어 있는지 확인(int)$_GET['cf_community_grade']
: 설정되어 있다면, 해당 값을 정수형으로 캐스팅$config['cf_community_grade']
: $_GET['cf_community_grade']가 설정되어 있지 않다면 이 기본값을 사용
SQL Fetch 코드
// 학년 선택값을 받아옵니다. 기본값은 1학년입니다.
$cf_community_grade = isset($_GET['cf_community_grade']) ? (int)$_GET['cf_community_grade'] : 1;
$sql_common = " from {$g5['status_config_table']} ";
$sql_search = " where st_grade = '{$cf_community_grade}' ";
$sql_order = " order by st_order asc ";
$sql = " select count(*) as cnt
{$sql_common}
{$sql_search}
{$sql_order} ";
$row = sql_fetch($sql);
이렇게 학년별 데이터를 불러온다.
FORM 태그
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="community_grade">
<p>학년별 스테이터스 조회</p>
<select name="cf_community_grade" id="cf_community_grade">
<option value="1" <?php echo $cf_community_grade == '1' ? 'selected' : ''; ?>>1학년</option>
<option value="2" <?php echo $cf_community_grade == '2' ? 'selected' : ''; ?>>2학년</option>
<option value="3" <?php echo $cf_community_grade == '3' ? 'selected' : ''; ?>>3학년</option>
<option value="4" <?php echo $cf_community_grade == '4' ? 'selected' : ''; ?>>4학년</option>
<option value="5" <?php echo $cf_community_grade == '5' ? 'selected' : ''; ?>>5학년</option>
<option value="6" <?php echo $cf_community_grade == '6' ? 'selected' : ''; ?>>6학년</option>
<option value="7" <?php echo $cf_community_grade == '7' ? 'selected' : ''; ?>>7학년</option>
</select>
<button type="submit">조회</button>
</div>
</form>
- 태그의
method="get"
속성을 사용하면 폼 데이터를 URL의 쿼리 스트링으로 전달한다. 여기서 선택된 학년(cf_community_grade) 값이 URL 파라미터로 전달됨. $_SERVER['PHP_SELF']
는 PHP에서 현재 실행 중인 스크립트의 경로를 반환하는 슈퍼글로벌 변수다. 이는 현재 페이지의 URL 경로를 나타내며, 스크립트의 이름과 경로를 포함함.
반응형