-1

私のフォームの1つに、フォルダーの画像を一覧表示する次のコードがあり、必要な画像を確認できるように、画像の名前であるチェックボックスを追加します。

<?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'];
    }

誰かが私が間違っていることを教えてもらえますか?ありがとうございました!

解決済み:

check_images配列の値を取得するために間違ったforeachを使用していました。それ以外の:

    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];
}

とにかく助けてくれてありがとう。

4

1 に答える 1

0

複数の値が必要ない場合は、<input>名前に配列のような表記を使用しないでください。

<input type='checkbox' name='check_imagens[]' value='"...
                                         ^^^^

これをに変更するだけcheck_imagensです。

于 2013-01-08T19:30:00.020 に答える