Welcome Guest, Not a member yet? Register   Sign In
Passing URI segments to functions
#1

[eluser]Isern Palaus[/eluser]
Hello,

I'm newbie using CodeIgniter and I started this week programing my own administration page for a website. I have a couple of doubts but my one of the most critical is how to pass URI segments to functions well.

(I've already read http://ellislab.com/codeigniter/user-gui...llers.html)

I want to have my administration controller in one file (it's the best way, no?). At this moment i have the index() that checks if a user is logged, the function news() that lists and call a view showing me the news on the db, a function newadd() that allows me to add a notice and now I want to try to edit it.

To edit a post i want to pass the ID via URI segment like this: www.example.tld/index.php/admin/news/edit/ID

The first problem i had is to control the term edit/add/delete on the function news() and I solved using a routes.php like:

Code:
<?php
$route['admin/news/add'] = "admin/newadd";
$route['admin/news/edit/:num'] = "admin/newedit";
$route['admin/news/delete/:num'] = "admin/newdelete";

?>

I don't know if is it the best way, if anyone else know a best way to split "add", "edit", "delete" on news() function please explain me.

Well, the newadd() function works well. Looks like this:
Code:
function newadd()
{
    $this->load->library('validation');
    
    $rules['title'] = 'trim|required|min_length[3]|max_length[64]';
    $rules['body'] = 'trim|required|min_lenght[50]';
    $this->validation->set_rules($rules);
    
    $fields['title']         = 'títol';
    $fields['body']         = 'notícia';
    $this->validation->set_fields($fields);
    
    if ($this->validation->run()) {
        $this->load->helper('date');
        
        $insert = array(
            'title'         => $this->input->post('title'),
            'body'             => $this->input->post('body'),
            'slug'            => url_title($this->input->post('name')),
            'user_id'        => $this->session->userdata('logged_in'),
            'created_on'     => now()
        );
        
        $this->db->insert('posts', $insert);
        
        redirect('admin/news');
    }
    
    $this->load->view('admin/news/add');
}

It's working fine, it adds the post well and show the view admin/news/add if it's not validated. With the ID i have some troubles. When I pass to the function with the real id at the end of the URI (like /admin/news/edit/1) they redirects me like its no ID defined. Here is the code for my newedit():

Code:
function newedit($id)
{
    if($id) {
        
        $this->load->library('validation');
    
        $rules['title'] = 'trim|required|min_length[3]|max_length[64]';
        $rules['body'] = 'trim|required|min_lenght[50]';
        $this->validation->set_rules($rules);
        
        $fields['title']         = 'títol';
        $fields['body']         = 'notícia';
        $this->validation->set_fields($fields);
        
        if ($this->validation->run()) {
        
            $update = array(
                'title'         => $this->input->post('title'),
                'body'             => $this->input->post('body'),
                'slug'            => url_title($this->input->post('name')),
                'user_id'        => $this->session->userdata('logged_in')
            );
        
            $this->db->where('id', $id);
            $this->db->update('posts', $update);
        
            redirect('admin/news');
        }
            
        $query = $this->db->where('id', $id);
        $query = $this->db->get('posts');
        foreach ($query->result() as $row) {
            $data['edit']['title']         = $row->title;
            $data['edit']['body']         = $row->body; /*auto_typography($row->body);*/
        }
        
    $data['edit']['id'] = $id;
        
        $this->load->view('admin/news/edit',$data);
        
    } else {
        redirect('admin/news');
    }
}

How can I solve this problem? If I pass a ID the script redirects me to admin/news, if I don't pass a ID the script redirects me to admin/news...

Anyway, it's possible to have this functions (add, edit, delete) inside a function called news()? Possible with models? (I've never used models and I don't know how to use it (i have the documentation open every time I code)).

Thank you in advance.

See you,
-- Isern Palaus
#2

[eluser]LuckyFella73[/eluser]
Hej,

a possible approach could be this:

Code:
function newedit()
{
    # instead of
    # if($id) {

    $id = $this->uri->segment(4, 0);

    if ( $id != 0 )
    {
        $this->load->library('validation');

        $rules['title'] = 'trim|required|min_length[3]|max_length[64]';
        $rules['body'] = 'trim|required|min_lenght[50]';
        $this->validation->set_rules($rules);

        # and the rest of your code goes here too
    }
    else
    {
        redirect('admin/news');
    }
}
#3

[eluser]Isern Palaus[/eluser]
[quote author="LuckyFella73" date="1198366864"]Hej,

a possible approach could be this:

Code:
function newedit()
{
    # instead of
    # if($id) {

    $id = $this->uri->segment(4, 0);

    if ( $id != 0 )
    {
        $this->load->library('validation');

        $rules['title'] = 'trim|required|min_length[3]|max_length[64]';
        $rules['body'] = 'trim|required|min_lenght[50]';
        $this->validation->set_rules($rules);

        # and the rest of your code goes here too
    }
    else
    {
        redirect('admin/news');
    }
}
[/quote]

Now it's working fine. Thanks!
#4

[eluser]Grahack[/eluser]
Just a piece of advice: you could use the language class for retrieving strings like 'títol' and 'notícia' instead of hardcoding them in the controller.
Then you'll be able to easily reuse your code for another project in another language.
#5

[eluser]Isern Palaus[/eluser]
[quote author="Grahack" date="1198518881"]Just a piece of advice: you could use the language class for retrieving strings like 'títol' and 'notícia' instead of hardcoding them in the controller.
Then you'll be able to easily reuse your code for another project in another language.[/quote]

Hahaha!

Hello Grahack,

When I was finishing my administration section I saw that I wrote 700 that could be 200 if I designed well the administration.... In next project Tongue

PS: Thank you I haven't seen the language library and I'll need soon!

Regards,
-- Isern Palaus




Theme © iAndrew 2016 - Forum software by © MyBB