ashalatha 0 Light Poster
  Controller: 
  function addblogs()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
        $this->form_validation->set_rules('blog_title','Blog Title');
        $this->form_validation->set_rules('description','Blog Description');
        $this->form_validation->set_rules('category_id','Category Name','required');
        $this->form_validation->set_rules('position','Position');
        if($this->form_validation->run()== FALSE)   
        {       
        $data['categorylist']=$this->blogs_model->categories_dropdown();
        $data['mainpage']='blogs';
        $data['mode']='add';
        $this->load->view('templates/template',$data);
        }
        else
        {
            $this -> blogs_model -> insertblogs();
            $this->flash->success('<h2>blogs Added Successfully!</h2>');
            redirect('blogs');
        }
    }

Model:

function categories_dropdown()  
    {       
        $this->table = 'categories';        
        $this->where('status',1);       
        $categorylist=$this->dropdown('category_id','category_name');   
        return $categorylist;
    }

    function insertblogs()
    {
        $options = $this->input->post('category_id');
        $array = explode(",", $options);
        print_r($array);
        exit();
        $data=array(
        'category_id'=>$array,
        'blog_title'=>$this->input->post('blog_title'),
        'description'=>$this->input->post('description'),
        'position'=>$this->input->post('position')
        );
        $this->db->insert('blogs',$data);           
    }

View:

<div id="main">         
    <div class="full_w">
        <div class="h_title">
            <div class="lefttitle fl">
                Add Blogs
            </div>
            <div class="rightbutton fr">
                <a  class="button cancel" href="<?php echo site_url()?>/blogs">Cancel</a>
            </div>
        </div>
        <?php
            $form_attributes  = array('name'=>'adds', 'id'=>'adds', 'enctype' => "multipart/form-data");
            echo form_open('blogs/addblogs',$form_attributes);
        ?>

        <div class="element">
            <label for="blogtitle"><font color ="black">Blog Title</font></label>
            <textarea name="blog_title" id="myArea1" rows="4" cols="173"></textarea>            
        </div>  
        <div class="element">
            <label for="categoryname"><font color="black">Category Name</font></label>
            <?php
                $categorylist['']='--Select Category Name--';
                $category_id="id='category_id[]'  multiple='multiple' ";
                if($this->input->post('category_id')) $selected=$this->input->post('category_id');else $selected='';
                echo form_dropdown('category_id',$categorylist,$selected,$category_id);
            ?>
            <?php echo form_error('category_id', '<div class="error">', '</div>'); ?><br/><br/>     
        </div>
        <div class="element">
            <label for="positions"><font color="black">Position</font></label>
            <select name="position">
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
             <option value="4">Select none</option>           
            </select>
        </div>
        <div class="element">
            <label for="description"><font color ="black">Blog Description</font></label>
            <textarea name="description" id="myArea2" rows="4" cols="173"></textarea>
        </div> <br/>

        <div align="center">
                <input type="submit" id="submit" value="Submit" />
        </div> 
        <div class="clear"></div>
        <?php echo form_close();?>
    </div>
</div>
<div class="clear"></div>

Need to select multiple categories from dropdown and while the data it should be inserted by comma separated.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.