Welcome Guest, Not a member yet? Register   Sign In
form validation question
#5

[eluser]rsmarsha[/eluser]
@djvoc It's not finished yet which is why I left that bit out.

@SitesByJoe Thanks for the tips, not sure I follow the redirect bit though. Your redirect directs to the insert method in your model? Does the model then redirect to a view? Oh and I moved the validation into a config file.

@cideveloper Thanks for the advice, I've moved the categories now. I do have code in place to remember the fields, as you can see from the view.

Maybe you can all look at the new code?

Controller
Code:
public function write () {
        if ($this->form_validation->run('write_blog_post') === FALSE)
        {
            $data = array(
                'categories' => $this->blog_model->get_categories()
            );
            $this->_view('admin/blog_write_post', $data);
        }
        else
        {
            $this->blog_model->insert_post();            
            $this->_view('admin/blog_post_submitted', $data);
        }
    }

Model
Code:
public function get_categories() {
        $this->db->select("id, name", FALSE);
        $this->db->order_by("name asc");
        $query = $this->db->get('blog_categories');
        if ($query->num_rows() > 0)
        {
            $categories = array();
            foreach ($query->result() AS $result) {
                $categories[$result->id] = $result->name;
            }
            return $categories;
        }
        else
        {
            return FALSE;
        }
    }
    
    public function insert_post() {
        $data = array(
            'title'        => $this->input->post('title'),
            'content'    => $this->input->post('content'),
            'user_id'    => '1',
            'posted'    => date('Y-m-d H:i:s'),
            'category'    => $this->input->post('category')
        );
        $this->db->insert('blog_posts', $data);
    }

View
Code:
<h3>Write a blog post</h3>
&lt;?php echo validation_errors(); ?&gt;
&lt;?php
$data = array(
    'id' => 'write_post'
);
echo form_open('admin/blog/write', $data);
$data = array(
    'name' => 'title',
    'placeholder' => 'Title',
    'value' => set_value('title')
);
echo form_input($data);
$data = array(
    'name' => 'content',
    'placeholder' => 'Content',
    'value' => set_value('content')
);
echo form_textarea($data);
echo form_label('Category', 'category');
echo form_dropdown('category', $categories, set_value('category'));
echo form_submit('submit', 'Submit Post');
?&gt;

The view looks a bit messy and I'm sure there's a much cleaner way of doing it so any tips/advice welcome. One idea I had was to move the $data arrays used to create the form fields into one large array built in the controller and then just access them in the view when echo'ing the create form code.

I can't seem to get the order by clause in the get_categories method in the model, to work right. At the moment it's just ordering by name but I setup a default field in the table with 0 or 1 allowed and 1 sets the default category.

I want to order like
Code:
$this->db->order_by("default desc, name asc");

But if I do that the num_rows bit below it throws errors as a none object.

Also I am not sure if I'm running the insert method from the model right. I moved the data array from the controller into the model and as it's post data I don't need any parameters. If I'm getting it right that is.

Very new to CI so all the advice is much appreciated, just be patient if I'm doing something stupid. Wink


Messages In This Thread
form validation question - by El Forum - 06-09-2011, 01:40 AM
form validation question - by El Forum - 06-09-2011, 09:09 AM
form validation question - by El Forum - 06-09-2011, 09:26 PM
form validation question - by El Forum - 06-09-2011, 10:48 PM
form validation question - by El Forum - 06-10-2011, 01:47 AM
form validation question - by El Forum - 06-10-2011, 08:19 AM
form validation question - by El Forum - 06-13-2011, 02:06 AM
form validation question - by El Forum - 06-13-2011, 07:35 AM
form validation question - by El Forum - 06-13-2011, 11:22 AM
form validation question - by El Forum - 06-14-2011, 01:14 AM
form validation question - by El Forum - 06-14-2011, 01:33 AM
form validation question - by El Forum - 06-15-2011, 09:11 AM
form validation question - by El Forum - 06-15-2011, 10:23 AM
form validation question - by El Forum - 06-16-2011, 03:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB