Welcome Guest, Not a member yet? Register   Sign In
The right way to redirect
#1

[eluser]zoonix[/eluser]
Hey all, I'm still having lots of fun learning about CI and I'm hoping there are a couple more kinds souls that might share some pointers with me.

All I'm trying to figure out is how to handle things after calling a function. In my test code below I have tried two ways of loading the view once the function is done.

In delete_file I tried $this->index which works except the list of files still shows the deleted file until you refresh.

In the upload function I tried using redirect('dev/test', 'refresh') which works fine by bringing me back to the index and refreshes the file list nicely, but I lose the data in $data['message'].

I'm sure that I'm making this much more difficult than need be. Any suggestions? What is the recommended way of doing this?

Thanks!!

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends Controller {
    
    function Test() {
        parent::Controller();
        $this->load->helper(array('download', 'file'));
        $this->data['files'] = $this->list_files();
        
        if(!array_key_exists('message',$this->data)){
            $this->data['message']='crap';
        }
    }
    
    function index() {
        $this->load->view('template', $this->data);
    }
    
    // Delete file
    function delete_file($file) {
        unlink('./upload/test/'.$file);
        
        $this->data['message'] = $file.' deleted';

        $this->index();
    }
    
    // List files
    function list_files() {
        $dir = './upload/test';
        
        if($dh = opendir($dir)) {
            while(($file = readdir($dh)) !== false) {
                if ($file != "." && $file != "..") {
                    $files[] = $file;
                }
            }
            closedir($dh);
            if(!isset($files)) {
                $files[] = 'none';
            }
        }
        return $files;
    }
    
    // Upload file
    function upload() {
        $config['upload_path'] = './upload/test/';
        $config['allowed_types'] = 'csv';
        
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload()) {
            $this->data['message'] = $this->upload->display_errors();
        }    
        else {
            $data['data'] = array('upload_data' => $this->upload->data());
            $this->data['message'] = 'Successfully uploaded '.$data['data']['upload_data']['orig_name'];
        }
        
        redirect('dev/test', 'refresh');
    }
}


Messages In This Thread
The right way to redirect - by El Forum - 04-16-2010, 01:28 PM
The right way to redirect - by El Forum - 04-16-2010, 01:37 PM
The right way to redirect - by El Forum - 04-16-2010, 02:34 PM
The right way to redirect - by El Forum - 04-16-2010, 03:46 PM
The right way to redirect - by El Forum - 04-16-2010, 03:49 PM
The right way to redirect - by El Forum - 04-19-2010, 06:56 AM
The right way to redirect - by El Forum - 04-19-2010, 06:58 AM



Theme © iAndrew 2016 - Forum software by © MyBB