我试图从查询中获取数组中的值,并使用implode在字符串中转换数组值,直到它工作正常,
所以在那之后我在另一个条件查询在选择框中传递了那个implode变量,所以现在发生的是如果数组为空,选择框正在消失,任何人都可以帮我解决那个问题。
这是我的代码
<select name="course1id" class="form-control" required>
<?php
$r = array();
$query = "SELECT course1id,course2id FROM mdl_course_relation";
$result = $DB->get_records_sql($query);
foreach ($result as $res) {
$r[] = $res->course1id;
$r[] = $res->course2id;
}
$courseids = implode(",", $r);
//$where = '';
//if (sizeof($r))
//$where .= 'AND id NOT IN($courseids)';
?>
<?php
$sql = "select id,fullname from {course} WHERE id!=1 AND id NOT IN($courseids)";
$courses = $DB->get_records_sql($sql);
?>
<option value="">Choose Course</option>
<?php
if (sizeof($courses)): foreach ($courses as $row):
$coursename = $row->fullname;
?>
<option value="<?php echo $row->id . '|' . $row->fullname; ?>"><?php echo $row->fullname; ?></option>
<?php
endforeach;
endif;
?>
</select>
答案 0 :(得分:1)
您需要检查空数组
像这样更新代码
$courseids = implode(",", $r);
$where = '';
if (count($r)>0)
{
$where = "AND id NOT IN($courseids)";
}
$sql = "select id,fullname from {course} WHERE id!=1 $where";
$courses = $DB->get_records_sql($sql);