sql 쿼리문 값 배열 넣기 후 for문
본문
<select>
<option value="">값 검색</option>
<?php
$subject_array = array();
$sql = "SELECT wr_subject FROM g_table ";
$result = sql_query($sql);
while($subject_array = sql_fetch_array($result)){
$subject_array = array($sql['wr_subject']);
}
// $subject_array[] = $sql['wr_subject'];
?>
<?php
for($i=0; $i < count($subject_array); $i++){
$find_subject = sql_fetch("SELECT EXISTS (SELECT * FROM g_table WHERE wr_subject ='$subject_array[$i]' LIMIT 1) as success");
if($find_subject['success']){ ?>
<option value="<?php echo $subject_array[$i]?>"> <?php echo $subject_array[$i] ?> </option>
<?php
}
}
?>
</select>
이런식으로 해서 g_table 안에있는 wr_subject 컬럼이 있는 값들만 배열에 담아 select로 출력하려고 하는데 우선 wr_subject 컬럼 체크 후 배열에 담아야하는데 방법이 너무 어려워 질문드립니다ㅠㅠㅠ 배열에 담고 for문 count로 하는것이 맞나요??
!-->답변 2
<select>
<option value="">값 검색</option>
<?php
$subject_array = array();
$sql = "SELECT wr_subject FROM g_table";
$result = sql_query($sql);
while($row = sql_fetch_array($result)){
$subject_array[] = $row['wr_subject'];
}
for($i = 0; $i < count($subject_array); $i++){
$find_subject = sql_fetch("SELECT EXISTS (SELECT * FROM g_table WHERE wr_subject = '$subject_array[$i]' LIMIT 1) as success");
if($find_subject['success']){ ?>
<option value="<?php echo $subject_array[$i]?>"><?php echo $subject_array[$i]?></option>
<?php }
}
?>
</select>
수정 해드렸습니다.
SELECT EXISTS <---이런 쿼리는 필요 없고 복잡하게 할 이유도 없습니다
<?php
$optStr="<option value=''>값 검색</option>";
$result = sql_query("SELECT wr_subject FROM g_table where wr_subject !='' ");
while($row = sql_fetch_array($result)){
$optStr .="<option value='{$row['wr_subject']}'>{$row['wr_subject']}</option>";
}
?>
<select><?php echo $optStr;?></select>