2011-12-12 2 views
1

양식에 선택 양식 필드가 있습니다.배열 및 MySQL 쿼리에서 동적 양식 선택 필드 생성

<select name="students" size="10" multiple="multiple"> 
    <?php 
    do { 
    ?> 
    <option value="<?php echo $row_rsStudents['student_id']?>"><?php echo  $row_rsStudents['student_sname1']?> <?php echo $row_rsStudents['student_sname2']?> <?php  echo $row_rsStudents['student_fname']?>: "</option> 
    <?php 
    } while ($row_rsStudents = mysql_fetch_assoc($rsStudents)); 
    $rows = mysql_num_rows($rsStudents); 
    if($rows > 0) { 
     mysql_data_seek($rsStudents, 0); 
     $row_rsStudents = mysql_fetch_assoc($rsStudents); 
    } 
    ?> 
    </select> 

나는 또한 내가 폭발 루프 한 배열을 가지고있다 - -이 : 필드 옵션은 MySQL의 쿼리 및 PHP는 에코에 의해 동적으로 생성

<?php 
    $studentsids = $row_rsClasses['class_students']; 
    $students = explode(":", $studentsids); 
    for($i = 0; $i < count($students); $i++){ 
    echo "$students[$i]"; 
    } 
    ?> 

을하지만 일치하는 항목을 표시 할 선택 필드에서 선택됩니다. 도움을 주신 모든 분들께 감사드립니다.

답변

0

올바른 결과를 얻으려면 변수 $students의 두 번째 코드 단편에서 강조 표시 될 학생 목록이 있습니까?

그리고 while 루프 내에서 첫 번째 코드 조각에서 읽히는 모든 학생이 있습니다.

특정 사용자가 배열 $students 내에 있는지 표시하려면 사용자 ID가이 배열 내에 있는지 확인하기 만하면됩니다.

if (in_array($row_rsStudents['student_id'], $studentsids)) { 
    echo 'selected="selected"'; 
} 

특정 학생이 배열 내에있는 경우 선택한 속성을 삽입하기 위해 양식 코드 내에 포함될 수 있습니다.

<?php 
$studentsids = $row_rsClasses['class_students']; 
// I assume this array contains all IDs that shall be highlighted 
$students = explode(":", $studentsids); 
?> 

그리고 첫 번째 코드는 모든 학생을 보여줍니다 :

는 두 번째 코드는 먼저 실행 감안할

<select name="students" size="10" multiple="multiple"> 
<?php 
// why have you used do while? Do you have an existing row before? 
// using while(): ... endwhile; is just another style, I prefer it in HTML templates 
while ($row_rsStudents = mysql_fetch_assoc($rsStudents): 
?> 
<option value="<?php echo $row_rsStudents['student_id']?>" <?php if (in_array($row_rsStudents['student_id'], $studentsids)) echo 'selected="selected"' ?> > 
    <?php echo $row_rsStudents['student_sname1']?> 
    <?php echo $row_rsStudents['student_sname2']?> 
    <?php echo $row_rsStudents['student_fname']?> 
</option> 
<?php endwhile; ?> 

<?php 
// what was this intended to do? 
// 
//$rows = mysql_num_rows($rsStudents); 
//if($rows > 0) { 
// mysql_data_seek($rsStudents, 0); 
// $row_rsStudents = mysql_fetch_assoc($rsStudents); 
//} 
?> 
</select>