3
//inside admin controller    
public function pictures($table)
{ 
    $image = new image_CRUD();
    $image  ->  set_table($table)
            ->  set_url_field('name')
            ->  set_title_field('title')
            //->  set_ordering_field('priority')
            ->  set_image_path('assets/uploads');
    $output = $image -> render();            
    $this->view_output('images/main.php',$image -> render());
}

url:localhost/my_site/admin/pictures/photos我得到一堆关于 css_files 和 js_files 的未定义变量以及 foreach() 的无效参数的错误。当 url 是localhost/my_site/admin/pictures并且代码如下时,它可以完美运行。

//inside admin controller    
public function pictures()
{ 
    $image = new image_CRUD();
    $image  ->  set_table('photos')
            ->  set_url_field('name')
            ->  set_title_field('title')
            //->  set_ordering_field('priority')
            ->  set_image_path('assets/uploads');
    $output = $image -> render();            
    $this->view_output('images/main.php',$image -> render());
}

问题是在使用 codeigniter 的方法在图像 crud 渲染函数上传递参数时。使用php ashttp://localhost/my_site/admin/pictures?table=photos的普通方法,代码如下,它可以工作,但是我不能这样上传图片,会有上传错误。

public function pictures()
{        
        $image = new image_CRUD();
        $table = $_GET['table'];
        $image  ->  set_table($table)
        ......
}

我怎样才能像我首先所说的那样传递论点?

好的,我得到了解决方案。以前,库image_crud.phpgetState() 函数没有 uri 段不是 numberic、'upload_file'、'ajax_list'、'ordering' 和 'insert_title' 的条件。所以错误是由于没有找到合适的条件并且代码没有执行。下面给出的代码必须添加到库中:image_crud.php第 477 行:

else
        {
            $upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/upload_file');
            $ajax_list_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/ajax_list');
            $ordering_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/ordering');
            $insert_title_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/insert_title');

            $state = array( 'name' => 'list', 'upload_url' => $upload_url);
            $state['ajax'] = isset($rsegments_array[3]) && $rsegments_array[3] == 'ajax_list'  ? true : false;
            $state['ajax_list_url'] = $ajax_list_url;
            $state['ordering_url'] = $ordering_url;
            $state['insert_title_url'] = $insert_title_url;

            return (object)$state;
        }

好的,这种方式在上传文件时出错。

以前在上传文件时(虽然我们没有第三个 uri 段),它会在第三个 uri 上添加额外的段来定义 image_crud 的状态。我修改它以在有第三个 uri 段时工作。所以在这种情况下,我有两个条件。首先,有第三个 uri 段,第二个,没有第三个 uri 段。所以 $extra_segments 变量设置为 false(对于没有额外的段为 false,即在我的情况下为 localhost/my_site/admin/pictures),而我们没有第三个 uri 段。为了检查 3rd uri 段,函数set_tableimage_crud.php修改为:

function set_table($table_name)
{
    $this->table_name = $table_name;

    if($table_name == $this->ci->uri->segment(3))
    {
        $this->extra_segments = true;
    }

    return $this;
}

变量也将在 image_crud 类中声明(在第 47 行我做了): protected $extra_segments = false; 现在对于实际工作,getState() 函数修改如下。它可以进一步优化以使其干燥和模块化。

protected function getState()
{
    $rsegments_array = $this->ci->uri->rsegment_array();

    if($this->extra_segments == true){

        if(isset($rsegments_array[4]) && is_numeric($rsegments_array[4]))
        {
            $upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/upload_file/'.$rsegments_array[3]);
            $ajax_list_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/'.$rsegments_array[3].'/ajax_list');
            $ordering_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ordering');
            $insert_title_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/insert_title');

            $state = array( 'name' => 'list', 'upload_url' => $upload_url, 'relation_value' => $rsegments_array[4]);
            $state['ajax'] = isset($rsegments_array[5]) && $rsegments_array[5] == 'ajax_list'  ? true : false;
            $state['ajax_list_url'] = $ajax_list_url;
            $state['ordering_url'] = $ordering_url;
            $state['insert_title_url'] = $insert_title_url;


            return (object)$state;
        }
        elseif( (empty($rsegments_array[4]) && empty($this->relation_field)) || (!empty($rsegments_array[4]) &&  $rsegments_array[4] == 'ajax_list'))
        {
            $upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/upload_file');
            $ajax_list_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ajax_list');
            $ordering_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ordering');
            $insert_title_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/insert_title');

            $state = array( 'name' => 'list', 'upload_url' => $upload_url);
            $state['ajax'] = isset($rsegments_array[4]) && $rsegments_array[4] == 'ajax_list'  ? true : false;
            $state['ajax_list_url'] = $ajax_list_url;
            $state['ordering_url'] = $ordering_url;
            $state['insert_title_url'] = $insert_title_url;

            return (object)$state;
        }
        elseif(isset($rsegments_array[4]) && $rsegments_array[4] == 'upload_file')
        {
            #region Just rename my file
                $new_file_name = '';
                //$old_file_name = $this->_to_greeklish($_GET['qqfile']);
                $old_file_name = $this->_convert_foreign_characters($_GET['qqfile']);
                $max = strlen($old_file_name);
                for($i=0; $i< $max;$i++)
                {
                    $numMatches = preg_match('/^[A-Za-z0-9.-_]+$/', $old_file_name[$i], $matches);
                    if($numMatches >0)
                    {
                        $new_file_name .= strtolower($old_file_name[$i]);
                    }
                    else
                    {
                        $new_file_name .= '-';
                    }
                }
                $file_name = substr( substr( uniqid(), 9,13).'-'.$new_file_name , 0, 100) ;
            #endregion

            $results = array( 'name' => 'upload_file', 'file_name' => $file_name);
            if(isset($rsegments_array[5]) && is_numeric($rsegments_array[5]))
            {
                $results['relation_value'] = $rsegments_array[5];
            }
            return (object)$results;
        }
        elseif(isset($rsegments_array[4]) && isset($rsegments_array[5]) && $rsegments_array[4] == 'delete_file' && is_numeric($rsegments_array[5]))
        {
            $state = array( 'name' => 'delete_file', 'id' => $rsegments_array[4]);
            return (object)$state;
        }
        elseif(isset($rsegments_array[4]) && $rsegments_array[4] == 'ordering')
        {
            $state = array( 'name' => 'ordering');
            return (object)$state;
        }
        elseif(isset($rsegments_array[4]) && $rsegments_array[4] == 'insert_title')
        {
            $state = array( 'name' => 'insert_title');
            return (object)$state;
        }
        else
        {
            $upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/upload_file');
            $ajax_list_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ajax_list');
            $ordering_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ordering');
            $insert_title_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/insert_title');

            $state = array( 'name' => 'list', 'upload_url' => $upload_url);
            $state['ajax'] = isset($rsegments_array[4]) && $rsegments_array[4] == 'ajax_list'  ? true : false;
            $state['ajax_list_url'] = $ajax_list_url;
            $state['ordering_url'] = $ordering_url;
            $state['insert_title_url'] = $insert_title_url;

            return (object)$state;
        }
     }
    elseif($this->extra_segments == false)
    {
        if(isset($rsegments_array[3]) && is_numeric($rsegments_array[3]))
        {
            $upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/upload_file/'.$rsegments_array[3]);
            $ajax_list_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ajax_list');
            $ordering_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/ordering');
            $insert_title_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/insert_title');

            $state = array( 'name' => 'list', 'upload_url' => $upload_url, 'relation_value' => $rsegments_array[3]);
            $state['ajax'] = isset($rsegments_array[4]) && $rsegments_array[4] == 'ajax_list'  ? true : false;
            $state['ajax_list_url'] = $ajax_list_url;
            $state['ordering_url'] = $ordering_url;
            $state['insert_title_url'] = $insert_title_url;


            return (object)$state;
        }
        elseif( (empty($rsegments_array[3]) && empty($this->relation_field)) || (!empty($rsegments_array[3]) &&  $rsegments_array[3] == 'ajax_list'))
        {
            $upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/upload_file');
            $ajax_list_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/ajax_list');
            $ordering_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/ordering');
            $insert_title_url  = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/insert_title');

            $state = array( 'name' => 'list', 'upload_url' => $upload_url);
            $state['ajax'] = isset($rsegments_array[3]) && $rsegments_array[3] == 'ajax_list'  ? true : false;
            $state['ajax_list_url'] = $ajax_list_url;
            $state['ordering_url'] = $ordering_url;
            $state['insert_title_url'] = $insert_title_url;

            return (object)$state;
        }
        elseif(isset($rsegments_array[3]) && $rsegments_array[3] == 'upload_file')
        {
            #region Just rename my file
                $new_file_name = '';
                //$old_file_name = $this->_to_greeklish($_GET['qqfile']);
                $old_file_name = $this->_convert_foreign_characters($_GET['qqfile']);
                $max = strlen($old_file_name);
                for($i=0; $i< $max;$i++)
                {
                    $numMatches = preg_match('/^[A-Za-z0-9.-_]+$/', $old_file_name[$i], $matches);
                    if($numMatches >0)
                    {
                        $new_file_name .= strtolower($old_file_name[$i]);
                    }
                    else
                    {
                        $new_file_name .= '-';
                    }
                }
                $file_name = substr( substr( uniqid(), 9,13).'-'.$new_file_name , 0, 100) ;
            #endregion

            $results = array( 'name' => 'upload_file', 'file_name' => $file_name);
            if(isset($rsegments_array[4]) && is_numeric($rsegments_array[4]))
            {
                $results['relation_value'] = $rsegments_array[4];
            }
            return (object)$results;
        }
        elseif(isset($rsegments_array[3]) && isset($rsegments_array[4]) && $rsegments_array[3] == 'delete_file' && is_numeric($rsegments_array[4]))
        {
            $state = array( 'name' => 'delete_file', 'id' => $rsegments_array[4]);
            return (object)$state;
        }
        elseif(isset($rsegments_array[3]) && $rsegments_array[3] == 'ordering')
        {
            $state = array( 'name' => 'ordering');
            return (object)$state;
        }
        elseif(isset($rsegments_array[3]) && $rsegments_array[3] == 'insert_title')
        {
            $state = array( 'name' => 'insert_title');
            return (object)$state;
        }
    }
}

此外url,助手必须自动加载或加载到 uri 检查功能。

4

1 回答 1

0

您可以使用url_helper

在控制器的构造函数中添加一行

 $this->load->helper('url');

public function pictures()
{ 
    $table = $this->uri->segment(3, 0);
    $image = new image_CRUD();
    $image  ->  set_table($table)
            ->  set_url_field('name')
            ->  set_title_field('title')
            //->  set_ordering_field('priority')
            ->  set_image_path('assets/uploads');
    $output = $image -> render();            
    $this->view_output('images/main.php',$image -> render());
}
于 2013-09-30T13:21:51.543 回答