0

我正在尝试设置一个管理页面,在该页面中,我可以使用 Web 表单中的复选框从图库中选择图像,然后从文件系统和数据库中删除图像。我遇到的问题是我应该如何更新数据库。

正如您在模型中看到的那样,我目前在删除每个文件后更新数据库,只保留删除的数量并从“旧”订单号中减去该数量,因为删除前 10 个图像之后的图像 23 将结束向上是图像 23-10 = 13)。不确定是否可能存在一些无法正常工作的中断情况。

它似乎有效,但注意到如果画廊有很多图片,数据库订单 ID 在 35 之后没有更新。我想知道这是否可能是一些限制,比如可以在给定数量内与数据库建立多少连接时间?有没有办法可以存储所有订单 ID 并一次更新它们,或者每次都正确连接到数据库(我怀疑,因为这似乎是不好的做法)?

网络表格:

<form action="http://localhost/admin/galleryRemove" method="post" enctype="multipart/form-data">
    <input type="checkbox" name="orderID[]" value="1"  />
    <img src="http://localhost/img/galleries/001/img01.jpg" alt="1" class="imgCheck" />

    <input type="checkbox" name="orderID[]" value="2"  />
    <img src="http://localhost/img/galleries/001/img02.jpg" alt="2" class="imgCheck" />

    <input type="checkbox" name="orderID[]" value="3"  />
    <img src="http://localhost/img/galleries/001/img03.jpg" alt="3" class="imgCheck" />

    <input type="checkbox" name="orderID[]" value="4"  />
    <img src="http://localhost/img/galleries/001/img04.jpg" alt="4" class="imgCheck" />

    <input type="checkbox" name="orderID[]" value="5"  />
    <img src="http://localhost/img/galleries/001/img05.jpg" alt="5" class="imgCheck" />

    <input type="checkbox" name="orderID[]" value="6"  />
    <img src="http://localhost/img/galleries/001/img06.jpg" alt="6" class="imgCheck" />

    <input type="checkbox" name="orderID[]" value="7"  />
    <img src="http://localhost/img/galleries/001/img07.jpg" alt="7" class="imgCheck" />

    <input type="checkbox" name="orderID[]" value="8"  />
    <img src="http://localhost/img/galleries/001/img08.jpg" alt="8" class="imgCheck" />

    <input type="checkbox" name="orderID[]" value="9"  />
    <img src="http://localhost/img/galleries/001/img09.jpg" alt="9" class="imgCheck" />

    <input type="checkbox" name="orderID[]" value="10"  />
    <img src="http://localhost/img/galleries/001/img10.jpg" alt="10" class="imgCheck" />

    <input type="hidden" name="galleryID" value="1" />
    <input type="submit" name="remove" value="Remove"  /> This will remove all images checked.
</form>

用于处理表单帖子的 CI 模型。

// Start of removing an image functions
    function imgRemove()
    {
        $cnt = 0;
        $id = $this->input->post('galleryID');
        foreach ($_POST['orderID'] as $order)
        {                
            $order = $order - $cnt;
            // get the picture name
            $this->db->select('*');
            $this->db->where("gallery_id = '$id'");
            $this->db->where("`order` = '$order'");
            $this->db->from('gallery');
            $q = $this->db->get();
            if ($q->num_rows != 0)
            {
                $result = $q->result();
                $picture = $result[0]->picture;

                // Get the path and then unlink(delete) the file and it's thumbnail
                $imgPath = $this->imgPath($id);
                $img =  $imgPath . $picture;
                $thumbImg =  $imgPath . 'thumbs/thumb' . $picture;
                if (file_exists($img))
                {
                    unlink($img);
                }
                if (file_exists($thumbImg))
                {
                    unlink($thumbImg);
                }

                // Remove the line from the database
                $this->db->where('gallery_id', $id);
                $this->db->where('order', $order);
                $this->db->delete('gallery');
                // Reorder all images after the deleted one.
                $this->imgOrderCheckRemove($id, $order);
                $cnt++;
            }
        }
    }

数据库表布局:
在此处输入图像描述

结果来自 var_dump($images); 就在 update_batch 之前。

array(3) {
    [2]=>
    array(5) {
        ["prim_id"]=>
        string(1) "8"
        ["gallery_id"]=>
        string(1) "2"
        ["picture"]=>
        string(21) "002.jpg"
        ["order"]=>
        string(1) "2"
        ["alt_text"]=>
        string(1) "2"
    }
    [10]=>
    array(5) {
        ["prim_id"]=>
        string(2) "16"
        ["gallery_id"]=>
        string(1) "2"
        ["picture"]=>
        string(18) "010.jpg"
        ["order"]=>
        string(2) "10"
        ["alt_text"]=>
        string(2) "10"
    }
    [13]=>
    array(5) {
        ["prim_id"]=>
        string(2) "19"
        ["gallery_id"]=>
        string(1) "2"
        ["picture"]=>
        string(15) "013.jpg"
        ["order"]=>
        string(2) "13"
        ["alt_text"]=>
        string(2) "13"
    }
}
4

1 回答 1

2

继承人的东西。我可能错过了一些东西,因为我几乎是在飞行中写的,但它应该工作得很好。

function imgRemove()
    {
        $id = $this->input->post('galleryID');
        $imgPath = $this->imgPath($id);

        $this->db->where('gallery_id', $id);
        $result = $this->db-get('gallery');
        $tempArray = $result->result_array();

        // reindex $tempArray for more less processing next loop
        foreach($tempArray as $img)
            $images[$img['order']] = $img;

        foreach ($_POST['orderID'] as $order)
        {
            $removethese[] = $order;
            if( isset($images[$order]) ) {
                $unlinkthese[] = $imgPath . $images[$order]['picture'];
                $unlinkthese[] = $imgPath . 'thumbs/thumb' . $images[$order]['picture'];

            }
            // lets keep $images clear, it should contain only images that will exists after cleanup
            unset($images[$order]);

        }
        // now lets remove files
        foreach($unlinkthese as $unlink )
            unlink ($unlink);

        // and then remove them from database
        $this->db->query("DELETE * FROM gallery where order IN (" . implode(',',$removethese) . " AND gallery_id=$id";

        // now lets do some re-ordering. We want to use CIs update_batch to make complex IF WHEN query

        for( $i = 1; $i <= count($images); $i++ )
            $images['order'] = $i;

        $this->db->update_batch('gallery', $images, 'prim_id'); 

    }
于 2012-09-15T09:27:47.577 回答