我目前正在处理多选下拉列表。我需要在编辑表单上显示所选的值。
in_array()
没有按照我的预期工作,我的逻辑中是否有错误?
显示数据库中所选值的代码是:
<div class="form-group">
<label class="col-md-3 control-label" for="selectbasic">Update Artist Selection</label>
<div class="col-md-6">
<select id="artists1" multiple="multiple" name="id_artist_fk[]" class="form-control ">
<?php
foreach ($artist_list as $key => $value) {
if (in_array($value['id_artist'], $current_artist_list, true)) {
$selected = "selected='selected'";
}
// print_r($value['id_artist']. "==". $current_artist_list);
echo "<option value=\"{$value['id_artist']}\" {$selected}>{$value['name']} {$value['surname']}</option>";
}
?>
</select>
</div>
</div>
$artist_list
来自:
public function get_artist_list() {
$sql = "SELECT * FROM tbl_v_artist WHERE status != 0;";
$result = $this->database->doSelectQuery($sql);
$artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist = array(
'id_artist' => $row['id_artist'],
'name' => $row['name'],
'surname' => $row['surname'],
'status' => $row['status']
);
array_push($artists, $artist);
}
}
return $artists;
}
$current_artist_list
来自:
$current_artist_list = $vid->get_artistsID_for_video($_POST['id_video']);
get_artistsID_for_video
是:
public function get_artistsID_for_video($video_id) {
try {
$sql = "SELECT
tbl_video_artist.id_artist_fk
FROM tbl_video_artist
left join tbl_v_artist
ON tbl_v_artist.id_artist = tbl_video_artist.id_artists
WHERE tbl_video_artist.id_video_fk = {$video_id};";
//echo $sql;
$result = $this->database->doSelectQuery($sql);
$artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist = array(
'id_artist_fk' => $row['id_artist_fk']
);
array_push($artists, $artist);
}
}
return $artists;
} catch (Exception $ex) {
$ex->getMessage();
$ex->getFile();
}
}
请指点我正确的方向。
我已按如下方式编辑了get_artistsID_for_video:
public function get_artistsID_for_video($video_id) {
try {
$sql = "SELECT
tbl_video_artist.id_artist_fk
FROM tbl_video_artist
left join tbl_v_artist
ON tbl_v_artist.id_artist = tbl_video_artist.id_artists
WHERE tbl_video_artist.id_video_fk = {$video_id};";
//echo $sql;
$result = $this->database->doSelectQuery($sql);
// $artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist [] = $row['id_artist_fk'];
// array_push($artists, $artist);
}
return $artist;
}
} catch (Exception $ex) {
$ex->getMessage();
$ex->getFile();
}
}
答案 0 :(得分:1)
请检查$ current_artist_list必须是单个数组。即$ current_artist_list [0] = 1; $ current_artist_list [1] = 2;
哪个会将您的ID与条件匹配。现在看起来你的$ current_artist_list是带有键的关联数组值。尝试按上面提到的仅推送值或更改下面的代码。
$artists = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()) {
$artist = array(
'id_artist_fk' => $row['id_artist_fk']
);
array_push($artists, $row['id_artist_fk']);
}
}
答案 1 :(得分:1)
查看in_array
手册:
in_array - 检查数组中是否存在值
那么,检查你的in_array($value['id_artist'], $current_artist_list, true)
是什么?
它检查$current_artist_list
中是否存在$value['id_artist']
的值。例如,如果$value['id_artist']
20 ,in_array
会检查数据 20 是否在您的数组中。
但 20 中的值 <{1}}数组中 。
因为$current_artist_list
中每个元素的格式为$current_artist_list
。
因此,您正在搜索 20 ,但您存储的值为array('id_artist_fk' => $row['id_artist_fk'])
。
('id_artist_fk' => 20)
NOT EQUALS 20
。
此修复程序位于array
:
get_artistsID_for_video()
现在您在数组中搜索while ($row = $result->fetch_array()) {
$artists[] = $row['id_artist_fk'];
}
,其中每个元素都是数字 。
让您的搜索更加更快(仍在20
中):
get_artistsID_for_video
将while ($row = $result->fetch_array()) {
// create array key with value of artist id
$artists[$row['id_artist_fk']] = 1;
}
替换为:
in_array