如何将用户名放入codeigniter中的个人资料链接

时间:2013-08-18 13:46:14

标签: php codeigniter

我有控制器打开用户行的个人资料: -

public function index() {
        if(!isset($_SESSION['user_id']) || empty($_SESSION['user_id'])){
            redirect('./home');
        }
        else {$user = new User($_SESSION['user_id']);}


        $user = new User($_SESSION['user_id']);

        if($this->input->post()){


            $user->username = $this->input->post('name');
            $user->address = $this->input->post('address');
            $user->phone = $this->input->post('phone');
            $user->skype = $this->input->post('skype');
            $user->facebook = $this->input->post('facebook');
            $user->mobile = $this->input->post('mobile');
            $user->tall = $this->input->post('tall');
            $user->fullname = $this->input->post('fullname');
            $user->wieght = $this->input->post('wieght');
            $user->fittnes = $this->input->post('fittnes');
            $user->fat = $this->input->post('fat');
            $user->email = $this->input->post('email');
            $user->birthdate = $this->input->post('birthdate');
            $user->gender = $this->input->post('gender');
            if(strlen($_FILES['pic']['name']) > 0){
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2048';
                $config['encrypt_name'] = true;
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload('pic'))
                {
                    $error = $this->upload->display_errors();
                    if(is_array($error)){
                        foreach($error as $er){
                            $this->errors[] =$er;
                        }
                    }else{
                         $this->errors[] =$error;
                    }
                }
                else
                {
                    $updata =$this->upload->data();
                    $imagePath = './uploads/'.$user->pic;
                    if(file_exists($imagePath)){
                         @unlink($imagePath);
                    }
                    $user->pic = $updata['raw_name'].$updata['file_ext'];
                }
        }

此Controller扩展了MY_Controller,MY_Controller代码为: -

class MY_Controller extends CI_Controller {

    var $data;



    function __construct() {
        parent::__construct();
        session_start();
        $this->defualt_template();

        $options = new Option();
        $options->get();
        $oppArray = array();
        foreach($options as $opt){
            $oppArray[$opt->name]= $opt->value;
        }
        $this->data['options'] = $oppArray;
        if(isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0){
            setcookie("logged", 1, time()+86400);
        }else{
            setcookie("logged", 0, time()+86400);
        }
    }



    function defualt_template() {
        $this->template->append_metadata('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
        $this->template->append_metadata('<link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />');
        $this->template->append_metadata('<link href="js/themes/default/default.css" rel="stylesheet" type="text/css" media="screen" />');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery-1.7.min.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/sortable.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.nivo.slider.pack.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.vibrate.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.cookie.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.tipsy.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/admin/ajaxupload.3.5.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/script.js"></script>');


        $this->layout();
    }

    function layout() {
        $this->template->set_layout('default');
        $this->template->set_partial('menu', 'partials/menu');
        $this->template->set_partial('header', 'partials/header');
        $this->template->set_partial('main_header', 'partials/main_header');
        $this->template->set_partial('right', 'partials/right');
        $this->template->set_partial('rightAccount', 'partials/rightAccount');
        $this->template->set_partial('leftAccount', 'partials/leftAccount');
        $this->template->set_partial('main_left', 'partials/main_left');
        $this->template->set_partial('left', 'partials/left');
        $this->template->set_partial('footer', 'partials/footer');
        $this->template->set_partial('main_footer', 'partials/main_footer');
    }

}


class MY_Admin extends CI_Controller {

    public $title = "";
    public $model = "";
    public $objects;
    public $cols = array();
    public $form_cols = array();

    var $page_url ;
    function __construct() {


        parent::__construct();
          session_start();
        $this->page_url = base_url()."administrator/".$this->uri->segment(2);
        $this->default_template();

        if(empty($_SESSION['userID'])){
            $this->session->set_flashdata('redirect_url', current_url());
            redirect('./administrator/login');
            exit();
        }
    }

    function default_template() {
        $this->template->append_metadata('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');

        $this->template->append_metadata('<link href="css/colorbox.css" rel="stylesheet" type="text/css" media="screen" />');
        $this->template->append_metadata('<link href="css/jquery-ui.css" rel="stylesheet" type="text/css" media="screen" />');
        $this->template->append_metadata('<link href="css/icon-library.css" rel="stylesheet" type="text/css" media="screen" />');
        $this->template->append_metadata('<link href="css/paginate.css" rel="stylesheet" type="text/css" media="screen" />');
        $this->template->append_metadata('<link href="css/admin.css" rel="stylesheet" type="text/css" media="screen" />');

        $this->template->append_metadata('<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.ui.min.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.visualize.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.visualize.tooltip.js"></script>');

        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.ui.selectmenu.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.datatables.min.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.potato.menu.min.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.masonry.min.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.lightbox.min.js"></script>');

        $this->template->append_metadata('<script type="text/javascript" src="js/jquery.ui.totop.js"></script>');

        $this->template->append_metadata('<script type="text/javascript" src="js/colorbox/jquery.colorbox-min.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/admin/jquery.ba-outside-events.js"></script>');
        $this->template->append_metadata('<script type="text/javascript" src="js/admin/ajaxupload.3.5.js"></script>');

        $this->template->append_metadata('<script type="text/javascript" src="js/admin/script.js"></script>');
        $this->layout();
    }

    function layout() {
        $this->template->set_layout('adminlayout');
        $this->template->set_partial('afooter', 'admin/adminfooter');
        $this->template->set_partial('aheader', 'admin/adminheader');
    }

    public function get_object($page, $per_page) {
        $this->objects = new $this->model();
        $this->objects->get_paged($page, $per_page);
    }

    public function view() {
        $uri = $this->uri->uri_to_assoc(4);

        $page = 1;
        $per_page = 10;
        extract($uri);

        $this->get_object($page, $per_page);

        /// urls ///
        $admin_url = site_url('admin');
        $current_url = $admin_url . $this->uri->slash_segment(2, 'both');

        /// paged ///
        $uri['page'] = 1;
        $this->objects->paged->first_url = $current_url . 'view/' . $this->uri->assoc_to_uri($uri);
        $uri['page'] = $this->objects->paged->previous_page;
        $this->objects->paged->previous_url = $current_url . 'view/' . $this->uri->assoc_to_uri($uri);
        $uri['page'] = $this->objects->paged->next_page;
        $this->objects->paged->next_url = $current_url . 'view/' . $this->uri->assoc_to_uri($uri);
        $uri['page'] = $this->objects->paged->total_pages;
        $this->objects->paged->last_url = $current_url . 'view/' . $this->uri->assoc_to_uri($uri);


        //$this->template->set_partial('footer','head');


        $this->template->build('admin/list', array(
            'objects' => $this->objects->all,
            'paged' => $this->objects->paged,
            'admin_url' => $admin_url,
            'current_url' => $current_url,
            'cols' => $this->cols
        ));
    }

    public function form($id = 0) {
        $this->objects = new $this->model($id);

        $form = $this->objects->render_form(
                        $this->form_cols
                        , 'admin' . $this->uri->slash_segment(2, 'both') . 'add/' . $this->objects->id
        );

        $this->template->build('admin/form', array('form' => $form));
    }

    function add($id = 0) {


        $this->objects = new $this->model($id);

        foreach ($this->file_cols() as $file) {
            $data = $this->do_upload($file);
            $_POST[$file] = $data['upload_data']['file_name'];
        }

        $save = $this->objects->from_array($_POST, $this->add_cols(), TRUE);

        if (!$save) {
            echo $c->error->string;
        }
        else
            redirect('admin' . $this->uri->slash_segment(2, 'both') . 'view/');
    }

    function do_upload($field) {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';

        $this->load->library('upload', $config);

        if (!$this->upload->do_upload($field)) {
            $error = array('error' => $this->upload->display_errors());

            return $error;
        } else {
            $data = array('upload_data' => $this->upload->data());

            return $data;
        }
    }

    function delete($id) {
        $this->objects = new $this->model($id);

        if (!$this->objects->delete()) {
            echo $c->error->string;
        }
        else
            redirect('admin' . $this->uri->slash_segment(2, 'both') . 'view/');
    }

    function add_cols() {
        $cols = array();
        foreach ($this->form_cols as $key => $value) {
            if (is_int($key))
                $cols[] = $value;
            else
                $cols[] = $key;
        }
        return $cols;
    }

    /*
      public $form_cols = array(
      0 => 'id',
      1 => 'post',
      'attach' => array('type' => 'file'),
      3 => 'body'
      );


     */

    function file_cols() {
        $cols = array();

        foreach ($this->form_cols as $key => $value) {
            if (isset($this->form_cols[$key]['type'])) {
                if ($this->form_cols[$key]['type'] == 'file')
                    $cols[] = $key;
            }
        }
        return $cols;
    }

}

在这里,我可以打开个人资料: - http://www.mysite.com/myaccount,但我需要打开我的个人资料: - http://www.mysite.com/username

我怎么能这样做,打开我的个人资料,如上面的链接。

我的观点如下: -

<a href="./myaccount/" >My Account</a>

1 个答案:

答案 0 :(得分:0)

在这种情况下,请将控制器的名称从myaccount更改为username

编辑:澄清之后

啊,好的,所以你想把用户名作为参数传递。

请参阅此链接Passing URI Segments to your Functions

上的此部分