在我的一种形式中,我有以下代码,列出了文件夹的图像,并向其中添加了复选框,这些值是图像的名称,以便我可以检查我想要的图像:
<?php
$imgdir = 'upload/'; //Pick your folder
$allowed_types = array('png','jpg','jpeg','gif'); //Allowed types of files
$dimg = opendir($imgdir);//Open directory
while($imgfile = readdir($dimg))
{
if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
/*If the file is an image add it to the array*/
{$a_img[] = $imgfile;}
}
echo "<ul>";
$totimg = count($a_img); //The total count of all the images
//Echo out the images and their paths incased in an li.
for($x=0; $x < $totimg; $x++){echo "<input type='checkbox' name='check_imagens[]' value='" .$a_img[$x]."'/><img height='50' width='50' src='http://localhost/code/" . $imgdir . $a_img[$x] . "' />"; echo $a_img[$x];}
echo "</ul>";
?>
现在,在我的班级模型中,我试图通过以下代码获取复选框的值:
$data_imagens = array(
'imagens_selected' => $this->input->post('check_imagens')
);
var_dump($data_imagens);
但是,我想我得到的价值超过了价值,或者至少我遇到了一些问题,因为如果我接下来在我的数据库中进行搜索以查找检查的图像的 ID:
foreach ($data_imagens as $value) {
$query = $this->db->query("SELECT id_imagem FROM imagens where nome_imagem='".$value."';");
}
foreach ($query->result_array() as $row)
{
echo $row['id_imagem'];
}
谁能指出我做错了什么?谢谢!
解决了:
我使用错误的 foreach 来获取 check_images 数组的值。代替:
foreach ($data_imagens as $value) {
$query = $this->db->query("SELECT id_imagem FROM imagens where nome_imagem='".$value."';");
}
foreach ($query->result_array() as $row)
{
echo $row['id_imagem'];
}
我需要的是:
foreach ($this->input->post('check_imagens') as $key => $value)
{
$query = $this->db->query("SELECT id_imagem FROM imagens where nome_imagem='".$value."';");
foreach ($query->result() as $row)
{
echo $row->id_imagem;
}
$imagens_selected[$key]=$value;
echo $imagens_selected[$key];
}
无论如何感谢您的帮助。