Welcome Guest, Not a member yet? Register   Sign In
Would like to condense some code.
#1

[eluser]Samuurai[/eluser]
Hi Everyone,

I'm trying to graduate from n00b to the next level up. At n00b level, you just want to get it working, which I have done, but this code i've written feels extremely bloated. Can someone please cast an experienced eye on this code and tell me how I can condense some of this duplicated code from my controller which handles contact information.

The view is set up to display values in the input areas if they're set, which is why there's lots of ternary operations there.

Code:
function contact()
    {
        $data->title = "Contact Us";
        $data->heading = "Contact Us";
        if($this->input->post('submitform'))
        {
            $this->form_validation->set_rules('name','Name', 'required');
            $this->form_validation->set_rules('email','Email', 'required|valid_email');
            $this->form_validation->set_rules('subject','Subject', 'required');
            $this->form_validation->set_rules('text','Message', 'required');
            
            if($this->form_validation->run())
            {
                $name = $this->input->post('name', TRUE);
                $email = $this->input->post('email', TRUE);
                $subject = $this->input->post('subject', TRUE);
                $text = $this->input->post('text', TRUE);
                $config['protocol'] = 'sendmail';
                $this->email->initialize($config);
                
                $this->email->from($email, $name);
                $this->email->to('[email protected]');
                $this->email->subject($subject);
                $this->email->message($text);
                $this->email->send();

                $this->session->set_flashdata('message', "Successfully sent email. We will get back to you as soon as possible.");

                redirect('site/home');
            }
            else
            {
                ($this->input->post('name')) ? $data->name = $this->input->post('name') : $data->name = '';
                ($this->input->post('email')) ? $data->email = $this->input->post('email') : $data->email = '';
                ($this->input->post('subject')) ? $data->subject = $this->input->post('subject') : $data->subject = '';
                ($this->input->post('text')) ? $data->text = $this->input->post('text') : $data->text = '';
                $this->load->view('header', $data);
                $this->load->view('contact', $data);
                $this->load->view('footer', $data);
            }
        }
        else
        {
            if($this->userlib->logged_in())
            {
                $this->load->model('User_model', 'user_m');
                $userid = $this->session->userdata('userid');
                $result = $this->user_m->name_and_email($userid);
                ($this->input->post('name')) ? $data->name = $this->input->post('name') : $data->name = $result['name'];
                ($this->input->post('email')) ? $data->email = $this->input->post('email') : $data->email = $result['email'];
                $this->load->view('header', $data);
                $this->load->view('contact', $data);
                $this->load->view('footer', $data);
            }
            else
            {
                ($this->input->post('name')) ? $data->name = $this->input->post('name') : $data->name = '';
                ($this->input->post('email')) ? $data->email = $this->input->post('email') : $data->email = '';
                ($this->input->post('subject')) ? $data->subject = $this->input->post('subject') : $data->subject = '';
                ($this->input->post('text')) ? $data->text = $this->input->post('text') : $data->text = '';
                $this->load->view('header', $data);
                $this->load->view('contact', $data);
                $this->load->view('footer', $data);
            }
        }
    }
}
edit -- Btw, I'm not after someone to rewrite my code for me, I'd just like it if someone could show me an approach that's a bit DRYer.


Messages In This Thread
Would like to condense some code. - by El Forum - 09-29-2009, 10:29 AM
Would like to condense some code. - by El Forum - 10-01-2009, 08:53 AM
Would like to condense some code. - by El Forum - 10-02-2009, 10:16 AM
Would like to condense some code. - by El Forum - 10-08-2009, 05:50 AM
Would like to condense some code. - by El Forum - 10-08-2009, 06:10 AM
Would like to condense some code. - by El Forum - 10-08-2009, 07:01 AM
Would like to condense some code. - by El Forum - 10-08-2009, 08:30 AM
Would like to condense some code. - by El Forum - 10-09-2009, 06:42 AM
Would like to condense some code. - by El Forum - 10-09-2009, 09:35 AM
Would like to condense some code. - by El Forum - 10-09-2009, 12:27 PM
Would like to condense some code. - by El Forum - 10-09-2009, 01:19 PM
Would like to condense some code. - by El Forum - 10-09-2009, 02:51 PM
Would like to condense some code. - by El Forum - 10-09-2009, 05:41 PM
Would like to condense some code. - by El Forum - 10-09-2009, 05:44 PM
Would like to condense some code. - by El Forum - 10-11-2009, 12:37 PM
Would like to condense some code. - by El Forum - 10-11-2009, 12:47 PM
Would like to condense some code. - by El Forum - 10-11-2009, 02:58 PM
Would like to condense some code. - by El Forum - 10-13-2009, 08:31 AM
Would like to condense some code. - by El Forum - 10-14-2009, 10:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB