Srrijita Dasgupta
Srrijita Dasgupta

Reputation: 35

codeigniter admin panel automatic page creation

I want to create one controller file which is creating automatically a function if i create a menu dynamically and also want to create view page which is connencted to this main controller.. how to do that?

Current code:

public function our_history() 
      { 
          $data['category']= $this->menu_model->getCategory('$lang'); 
          $data['subcategory']= $this->menu_model->getSubCategory('$lang'); 
          $this->load->view('vwMain',$data);//Left Menu } 
      }

Upvotes: 2

Views: 1787

Answers (2)

Abhishek
Abhishek

Reputation: 1561

Follow below steps Hope that makes sense. -- Admin section --

/*content.php -- controller starts here */

class Content extends VCI_Controller {

# Class constructor
function __construct()
{
    parent::__construct();
    $this->load->model('content_model');
}

/*
Add page logic
*/
function edit_page($id = null)
{

    $this->_vci_layout('your_layoutname');
    $this->load->library('form_validation');
    $view_data = array();

    //Set the view caption
    $view_data['caption'] = "Edit Content";

    //Set the validation rules for server side validation
    // rule name editcontent should be defined

    if($this->form_validation->run('editcontent')) {

        //Everything is ok lets update the page data
        if($this->content_model->update(trim($id))) {
            $this->session->set_flashdata('success', "<li>Page has been edited successfully.</li>");
            $this->output->set_header('location:' . base_url() . 'content/manage_content');
        } else {
            $this->session->set_flashdata('error', "<li>Unknown Error: Unable to edit page.</li>");
            $this->output->set_header('location:' . base_url() . 'content/manage_content');
        }

    } else {

        $page = $this->content_model->get_content_page(trim($id)); 
        $view_data["id"] = $page->id;
        $view_data["page_title"] = $page->page_title;
        $view_data["page_menu_slug"] = $page->page_menu_slug;
        $view_data["page_name"] = $page->page_name;
        $view_data["page_content"] = $page->page_content;
        $view_data["status"] = $page->status;
        $this->_vci_view('content_editpage', $view_data);
    }
}

/*
Edit page logic
*/
function add_page()
{

    $this->_vci_layout('your_layoutname');
    $this->load->library('form_validation');

    $view_data = array();

    $view_data['caption'] = "Edit Content";

    if($this->form_validation->run('editcontent')) { 
        // after passing validation rule data to be saved
        // editcontent rule must be defined in formvalidations file

        //Everything is ok lets update the page data
        if($this->content_model->add()) {
            $this->session->set_flashdata('success', "<li>Page has been edited successfully.</li>");
            $this->output->set_header('location:' . base_url() . 'content/manage_content');
        } else {
            $this->session->set_flashdata('error', "<li>Unknown Error: Unable to edit page.</li>");
            $this->output->set_header('location:' . base_url() . 'content/manage_content');
        }

    } else {

        $page = $this->content_model->get_content_page(trim($id)); 
        $view_data["id"] = $page->id;
        $view_data["page_title"] = $page->page_title;
        $view_data["page_menu_slug"] = $page->page_menu_slug;
        $view_data["page_name"] = $page->page_name;
        $view_data["page_content"] = $page->page_content;
        $view_data["status"] = $page->status;
        $this->_vci_view('content_editpage', $view_data);
    }
}

} /** * content.php -- controller ends here */

/* Content_model starts here */

class Content_model extends CI_Model {

// update logic goes here
function update($id = null) {
    if(is_null($id)) {
        return false;
    }

    $data = array(
        'page_title' => htmlspecialchars($this->input->post('page_title',true)),
        'page_name' => htmlspecialchars($this->input->post('page_name',true)),
        'page_content' => $this->input->post('page_content',true),
        'page_menu_slug' => htmlspecialchars($this->input->post('page_menu_slug',true)),
        'status' => htmlspecialchars($this->input->post('status',true))
    );

    $this->db->where('id', $id);
    $this->db->update('content', $data);
    return true;

}

// Add logic goes here
function add() {
    $data = array(
        'page_title' => htmlspecialchars($this->input->post('page_title',true)),
        'page_name' => htmlspecialchars($this->input->post('page_name',true)),
        'page_content' => $this->input->post('page_content',true),
        'page_menu_slug' => htmlspecialchars($this->input->post('page_menu_slug',true)),
        'status' => htmlspecialchars($this->input->post('status',true))
    );

    $this->db->where('id', $id);
    $this->db->insert('content', $data);
    return true ;
}

} /* Content_model ends here # Admin section changes ends here */ -- Add view files also to admin section content_editpage.php

Now go to your routes.php file for front section -- add below line at last --

$route['(:any)'] = 'page/view_usingslug/$1';

This will be for all urls like --- http://yourdomainname/your_slug_name

// create again a controller in front section page.php --

class page extends VCI_Controller {

    function __construct()
    {
        parent::__construct();

    }

    function view_usingslug($slug='')
    {
        // retrieve the data by slug from content table using any model class and assign result to $view_dat
        $this->_vci_view('page',$view_data);
        //page.php will be your view file
    }

}

Upvotes: 5

Bugfixer
Bugfixer

Reputation: 2617

Suppose Your URL is

www.example.com/controllername/methodname/menutitle1

or

www.example.com/controllername/methodname/menutitle2

So this is how you handle these pages.

public function method()
{
   $menutitle = $this->uri->segment(3);
   $query = $this->db->get_where('TableName',array('Menutitle'=>$menutitle))
   $data['content'] = $query->row()->page_content;
   $this->load->view('common_page',$data);

} 

Upvotes: 1

Related Questions