Search code examples
phpcodeigniteruricodeigniter-3codeigniter-2

Assign a value to Codeigniter URI segment


I am new to codeigniter, I am looking a way to to assign 'reg_uad' => $this->input->post('uad') to uri segment . Could some one please help me with this problem.

public function reg()   
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('index', 'AL Index Number', 'required');  
    $this->form_validation->set_rules('mobile', 'Mobile Number', 'required');  
    $this->form_validation->set_rules('name', 'Name', 'required');     
        if($this->form_validation->run() == FALSE) {
            $this->load->view('reg');
    } else 
        {
     //insert the contact form data into database
        $data = array(
            'reg_index' => $this->input->post('index'),
            'reg_mobile' => $this->input->post('mobile'),
            'reg_name' => $this->input->post('name'),
            'reg_uad' => $this->input->post('uad'),         
            'reg_date' => date('Y-m-d H:i:s')

        );
            if ($this->db->insert('tbl_reg', $data))                    
        {   // success
            $this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');                        
            redirect('main/uad_code');              
        }
        else
        {
            // error
            $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error.  Please try again later!!!</div>');
            redirect('main/reg');
        }     
        }
}

Solution

  • You can do like this To assign while redirecting

    There are two ways to pass variable in URL 1 with variable name in url

    redirect('main/uad_code?reg_id=3');
    

    To access the that value in uad_code method,

    $id=$this->get('reg_id');
    

    2.you can pass as many variable as you wish in url

    redirect('main/uad_code/$regId/3/5');
    

    here you can access variable with uri segment option

    controller name will be First segment in uri,

    methodname will be second segment in uri

    E.g For above

    $contName = $this->uri->segment('1') // assigns main to $conName
    $methName = $this->uri->segment('2') // assigns uad_code to $methName 
    $red_id = $this->uri->segment->('3') // assigns value in $regId to $reg_id
    $exVar = $this->uri->segment->('4') // assigns 3 to $exVar 
    $exVar1 = $this->uri->segment->('5') // assigns 5 to $exVar1