0

I've never used a PHP framework, but I wanted to learn how to use one so I could more easily program for my company, so I chose CodeIgniter.

I am following the code igniter tutorial for a news section so I could learn how things work - but I am having some issues/questions.

Now, I am to the point in the tutorial that I have a form that submits and takes me to a success page. In normal PHP this is where I would just redirect the page to the view page of the news item submitted. In CodeIgniter I have a view function in my controller here

        public function view($slug = NULL)
        {
                $data['news_item'] = $this->news_model->get_news($slug);

                if (empty($data['news_item']))
                {
                        show_404();
                }

                $data['title'] = $data['news_item']['title'];

                $this->load->view('templates/header', $data);
                $this->load->view('news/view', $data);
                $this->load->view('templates/footer');
        }

I'm not sure how to get the slug from the form when it is submitted.

Here is my create function (Basically the one that inserts the database info and redirects to news/success)

        public function create()
        {
            $this->load->helper('form');
            $this->load->library('form_validation');

            $data['title'] = 'Create a news item';

            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('text', 'Text', 'required');

            if ($this->form_validation->run() === FALSE)
            {
                $this->load->view('templates/header', $data);
                $this->load->view('news/create');
                $this->load->view('templates/footer');

            }
            else
            {
                $this->news_model->set_news();
                $this->load->view('news/success');
            }
        }

But I want this to redirect to the view page. To do that I need to have the slug I believe. So it should redirect to news/view/$slug - but I'm not sure how to do this.

My set_news model:

        public function set_news()
        {
            $this->load->helper('url');

            $slug = url_title($this->input->post('title'), 'dash', TRUE);

            $data = array(
                'title' => $this->input->post('title'),
                'slug' => $slug,
                'text' => $this->input->post('text')
            );

            return $this->db->insert('news', $data);
        }

1 Answer 1

1

You should use something like that:

redirect('news/view/'.url_title($this->input->post('title'), 'dash', TRUE));
Sign up to request clarification or add additional context in comments.

6 Comments

And that should go on my success.php page? or? Sorry, I'm not really sure why I can't wrap my head around this whole framework thing but I'm sure I will understand eventually.
Also, how do you know that my form is POST?
yes it should be on success.php after news is inserted successfully. regarding the form type, it is just an assumption, If it is GET you should use: redirect('news/view/'.$this->input->get('slug'));
Based on my model, it is POST. However, this does not work for me, it redirects me to news/view but it doesn't add the slug. I don't think the success page gets the POST information and I think that is where I am struggling the most with this
after seeing the function set_news() (I think it wasn't available when I posted my reply), I knew that slug is not available in the form, that is why you should create it - as happening in set_news - therefore my suggested code is: redirect('news/view/'.url_title($this->input->post('title'), 'dash', TRUE));
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.