gpt4 book ai didi

php implode 函数空数组不起作用

转载 作者:行者123 更新时间:2023-11-29 10:15:24 31 4
gpt4 key购买 nike

我试图从查询中获取数组中的值,并使用 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>

最佳答案

您需要检查空数组

像这样更新代码

$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);

关于php implode 函数空数组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50188160/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com