El Forum
09-29-2009, 11:29 AM
[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.
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.
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);
}
}
}
}