0

i am novice in codeigniter. I am trying to insert multiple news that contains some check-box where its values are looped from a database. But i don't know what should code i write for this in controller & Model to insert values. can any one suggest or help me to write code?

view(content.php)

  <?php

               foreach($result as $aresult)
               {

                ?>
    <input type="checkbox" name="category_name[<?php echo $aresult->category_name; ?>]" value="<?php echo $aresult->category_name;?>" /> <?php echo $aresult->category_name;?> <br>
              <?php
                }
                  foreach($area as $aresult1)
                    {

                ?>   
                <input type="checkbox" name="category_name[<?php echo $aresult->category_name; ?>]" value="<?php echo $aresult1->category_name;?>" /> <?php echo $aresult1->category_name;?> <br>
             <?php 

                    } 
                ?>

   <tr> <td>   <input type="text" required="1" name="content_headline" tabindex="3" placeholder="Content Headline" size="80"/>  </tr> </td>
  <tr> <td>     <textarea name="content_shortdetails"  tabindex="4" maxlength="145" placeholder="Content Short Details" rows="10" cols="78"></textarea>   </tr> </td>
 <tr>
        <td>Picture</td>
        <td>
            <input type="file" name="image" tabindex="8"/>                
        </td>
    </tr>
    <tr>
        <td>Publication Status:</td>
        <td>
            <input type="radio" name="publication_status" value="1" tabindex="3"  />Publish
            <input type="radio" name="publication_status" value="0" tabindex="4"  />Unpublish
        </td>
    </tr>
   <tr>
        <td> <input type="submit" name="btn"  value="Save" tabindex="9"/></td>
    </tr>

 controller(content) :

public function savecontent()
    {    
        $data=array();
        date_default_timezone_set('Asia/Dhaka');
        $now = date("Y-m-d H:i:s");
        $data['admin_id']=$this->session->userdata('admin_id');
        $data['category_name']=$this->input->post('category_name',true);
        $data['content_headline']=$this->input->post('content_headline',true);
        $data['content_shortdetails']=$this->input->post('content_shortdetails',true);
        $data['publication_status']=$this->input->post('publication_status',true);
        $data['date_add']=$now; 
         /* -------------Image Upload---------------- */
            $this->load->library('upload');
            $config['upload_path'] = './images/news_images/';
            $config['allowed_types'] = 'gif|jpg|png|mp3';
            $config['max_size'] = '100000';
            $config['max_width'] = '';
            $config['max_height'] = '';
            $error = '';
            $udata = '';

            $this->upload->initialize($config);
            if (!$this->upload->do_upload('image')) {
                $error = array('error' => $this->upload->display_errors());
                // echo '<pre>';
                // print_r($error);
                //$this->load->view('upload_form', $error);
            } else {

                $udata = array('upload_data' => $this->upload->data());
                $data['image'] = "images/news_images/" . $udata['upload_data']['file_name'];
                $this->load->view('upload_success', $data);
            }
        $this->co_model->save_content($data);
  }

  model(co_model) :

 public function save_content($data)
{
    $this->db->insert('content',$data);
}

Database structure:

 category { cat_id(auto incr.), category_name(varchar 50) } 
 content { id(auto incr.), category_name(varchar 50), content_headline(varchar 50), shortdetails(varchar 50),  publication_status(tinyint 1), date_add(datetime), image(varchar 255) }  

in view page category_name are come from category table where i already inserted some category name and then all news are inserted in content table.
now what should i change to insert multiple row using checkbox ? when i insert one news it can be insert multiple category name(checkbox value) in multiple row in db with other value (other value not changed) .

like: category name (checkbox) =c_1, c_3, c_4 

{      content headline = something
      shortdetails = again something
      mage = pic.jpg
      publication status = 1 (publish)  }--> all this news item as same in all checked category
3
  • 1
    possible duplicate of How to insert multiple checkbox value in mysql using codeigniter Commented Mar 16, 2014 at 19:26
  • what type of error you are getting can u explain little further Commented Oct 20, 2014 at 20:34
  • if you are after inserting multiple check boxes values so it will also create multiple rows in your databases what i suggest you to create another table for check boxes values so that data manipulated in well way means good normalization Commented Oct 20, 2014 at 20:38

1 Answer 1

0

You will have to loop through category names because category_names is an array so it may have different values in it that's why you have to loop through each index and save the record with each index in database i have solved the issues with controller but there is in ambiguity in your view you are using category_names for two arrays which cause the problem so i have also change it.

public function savecontent()
    {  

       $check=$this->input->post('category_name',true);
       $area=$this->input->post('category_area',true);
       for($i=0; $i<sizeof($check); $i++;)
       {
        $data=array();
        date_default_timezone_set('Asia/Dhaka');
        $now = date("Y-m-d H:i:s");
        $data['admin_id']=$this->session->userdata('admin_id');
        $data['category_name']=$check[$i];
        $data['category_area']=$area[$i];
        $data['content_headline']=$this->input->post('content_headline',true);
        $data['content_shortdetails']=$this->input->post('content_shortdetails',true);
        $data['publication_status']=$this->input->post('publication_status',true);
        $data['date_add']=$now; 
         /* -------------Image Upload---------------- */
            $this->load->library('upload');
            $config['upload_path'] = './images/news_images/';
            $config['allowed_types'] = 'gif|jpg|png|mp3';
            $config['max_size'] = '100000';
            $config['max_width'] = '';
            $config['max_height'] = '';
            $error = '';
            $udata = '';

            $this->upload->initialize($config);
            if (!$this->upload->do_upload('image')) {
                $error = array('error' => $this->upload->display_errors());
                // echo '<pre>';
                // print_r($error);
                //$this->load->view('upload_form', $error);
            } else {

                $udata = array('upload_data' => $this->upload->data());
                $data['image'] = "images/news_images/" . $udata['upload_data']['file_name'];
                $this->load->view('upload_success', $data);
            }
          $this->co_model->save_content($data);
        }
        redirect('function name where you want to redirect ')
  }

}

Now your View have several changes the rest is the same loop through the code.

           foreach($result as $aresult)
               {

                ?>
    <input type="checkbox" name="category_name[<?php echo $aresult->category_name; ?>]" value="<?php echo $aresult->category_name;?>" /> <?php echo $aresult->category_name;?> <br>
              <?php
                }
                  foreach($area as $aresult1)
                    {

                ?>   
                <input type="checkbox" name="category_area[<?php echo $aresult->category_name; ?>]" value="<?php echo $aresult1->category_name;?>" /> <?php echo $aresult1->category_name;?> <br>
             <?php 

                    } 
                ?>

   <tr> <td>   <input type="text" required="1" name="content_headline" tabindex="3" placeholder="Content Headline" size="80"/>  </tr> </td>
  <tr> <td>     <textarea name="content_shortdetails"  tabindex="4" maxlength="145" placeholder="Content Short Details" rows="10" cols="78"></textarea>   </tr> </td>
 <tr>
        <td>Picture</td>
        <td>
            <input type="file" name="image" tabindex="8"/>                
        </td>
    </tr>
    <tr>
        <td>Publication Status:</td>
        <td>
            <input type="radio" name="publication_status" value="1" tabindex="3"  />Publish
            <input type="radio" name="publication_status" value="0" tabindex="4"  />Unpublish
        </td>
    </tr>
   <tr>
        <td> <input type="submit" name="btn"  value="Save" tabindex="9"/></td>
    </tr>

Change the Database structure add a column name areas where the different areas are gonna save

Sign up to request clarification or add additional context in comments.

Comments

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.