CodeIgniter Forums
Sending ID to a different table after data has been added - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Sending ID to a different table after data has been added (/showthread.php?tid=70258)



Sending ID to a different table after data has been added - kirasiris - 03-14-2018

So I have a method in which I display the categories which are coming from ci_terms table using this:

PHP Code:
            // Select Categories    
            
$categories_options = array();
            
$categories_options[0] = 'Select Categories';
            
            
$categories_list $this->Terms_model->get_list();
            
            foreach(
$categories_list as $cat){
                
$categories_options[$cat->term_id] = $cat->title;
            }
            
            
$data['categories_options'] = $categories_options

After that I do an insert  into my ci_posts table and it works great!; using this:

PHP Code:
    // Page Data
 
          $data = array(
                
'user_id'        => $this->input->post('user_id'),
                
'slug'            => $slug,
                
'title'            => $this->input->post('title'),
                
'post_image'    => $post_image,
                
'body'            => $this->input->post('body'),
                
'status'        => $this->input->post('status'),
                
'is_featured'    => $this->input->post('is_featured'),
                
'is_commented'    => $this->input->post('is_commented'),
                
'order'            => $this->input->post('order'),
                
'type'            => 'post',
 
           );
            
 
           // Insert Page
 
           $this->Post_model->add($data); 

This is what I have in my view to display and post the categories:

PHP Code:
   <!-- Post Categories -->
 
   <?php
        $data 
= array(
            
'class'        => 'form-control js-example-basic-multiple',
            
'multiple'    => 'multiple',
        );
    
?>
    <div class="form-group">
        <?= form_label('Categories','categories'); ?>
        <?= form_dropdown('categories[]'$categories_options0$data); ?>
    </div> 

Now in order to make it possible to post an id without having to pass a parameter in the method(for what I know).


I will have to post whatever data I want(that's the previous block of code in the controller). Now after that insert page(comment in the controller), I will obtain the id of the categories that were passed in the view by doing this(I hope I could make myself clear on this):

PHP Code:
            // Insert Categories into CI_TERMS_TAXONOMY - term_taxonomy_id, term_id, type, body, parent_id, count
            
$categories $this->Terms_model->get_list();
            
            
$data = array(
                
'term_taxonomy_id'    => $this->db->insert_id(),
                
'term_id'            => $categories->id,
                
'type'                => 'category',
            );
            
            
$this->Taxonomy_model->add($data); 


So far, it only trows me an error of term_id cannot be null.

Any feedback would be very appreciated.

If you notice, I'm trying to build something similar to WordPress regarding its relationship between the datatables "wp_terms, wp_terms_taxonomy, wp_terms_relationship".

Thanks in advance.


RE: Sending ID to a different table after data has been added - ciadvantage - 03-14-2018

(03-14-2018, 07:03 PM)kirasiris Wrote: So I have a method in which I display the categories which are coming from ci_terms table using this:

PHP Code:
            // Select Categories    
            
$categories_options = array();
            
$categories_options[0] = 'Select Categories';
            
            
$categories_list $this->Terms_model->get_list();
            
            foreach(
$categories_list as $cat){
                
$categories_options[$cat->term_id] = $cat->title;
            }
            
            
$data['categories_options'] = $categories_options

After that I do an insert  into my ci_posts table and it works great!; using this:

PHP Code:
    // Page Data
 
          $data = array(
                
'user_id'        => $this->input->post('user_id'),
                
'slug'            => $slug,
                
'title'            => $this->input->post('title'),
                
'post_image'    => $post_image,
                
'body'            => $this->input->post('body'),
                
'status'        => $this->input->post('status'),
                
'is_featured'    => $this->input->post('is_featured'),
                
'is_commented'    => $this->input->post('is_commented'),
                
'order'            => $this->input->post('order'),
                
'type'            => 'post',
 
           );
            
 
           // Insert Page
 
           $this->Post_model->add($data); 

This is what I have in my view to display and post the categories:

PHP Code:
   <!-- Post Categories -->
 
   <?php
        $data 
= array(
            
'class'        => 'form-control js-example-basic-multiple',
            
'multiple'    => 'multiple',
        );
    
?>
    <div class="form-group">
        <?= form_label('Categories','categories'); ?>
        <?= form_dropdown('categories[]'$categories_options0$data); ?>
    </div> 

Now in order to make it possible to post an id without having to pass a parameter in the method(for what I know).


I will have to post whatever data I want(that's the previous block of code in the controller). Now after that insert page(comment in the controller), I will obtain the id of the categories that were passed in the view by doing this(I hope I could make myself clear on this):

PHP Code:
            // Insert Categories into CI_TERMS_TAXONOMY - term_taxonomy_id, term_id, type, body, parent_id, count
            
$categories $this->Terms_model->get_list();
            
            
$data = array(
                
'term_taxonomy_id'    => $this->db->insert_id(),
                
'term_id'            => $categories->id,
                
'type'                => 'category',
            );
            
            
$this->Taxonomy_model->add($data); 


So far, it only trows me an error of term_id cannot be null.

Any feedback would be very appreciated.

If you notice, I'm trying to build something similar to WordPress regarding its relationship between the datatables "wp_terms, wp_terms_taxonomy, wp_terms_relationship".

Thanks in advance.

 Why dont you try var_dump($categories) to see what returns.  Check if it is indeed an object or array of objects so on, the chance is id can be serialized out in someway within categories

Regards


RE: Sending ID to a different table after data has been added - kirasiris - 03-14-2018

(03-14-2018, 07:17 PM)ciadvantage Wrote:
(03-14-2018, 07:03 PM)kirasiris Wrote: So I have a method in which I display the categories which are coming from ci_terms table using this:

PHP Code:
            // Select Categories    
            
$categories_options = array();
            
$categories_options[0] = 'Select Categories';
            
            
$categories_list $this->Terms_model->get_list();
            
            foreach(
$categories_list as $cat){
                
$categories_options[$cat->term_id] = $cat->title;
            }
            
            
$data['categories_options'] = $categories_options

After that I do an insert  into my ci_posts table and it works great!; using this:

PHP Code:
    // Page Data
 
          $data = array(
                
'user_id'        => $this->input->post('user_id'),
                
'slug'            => $slug,
                
'title'            => $this->input->post('title'),
                
'post_image'    => $post_image,
                
'body'            => $this->input->post('body'),
                
'status'        => $this->input->post('status'),
                
'is_featured'    => $this->input->post('is_featured'),
                
'is_commented'    => $this->input->post('is_commented'),
                
'order'            => $this->input->post('order'),
                
'type'            => 'post',
 
           );
            
 
           // Insert Page
 
           $this->Post_model->add($data); 

This is what I have in my view to display and post the categories:

PHP Code:
   <!-- Post Categories -->
 
   <?php
        $data 
= array(
            
'class'        => 'form-control js-example-basic-multiple',
            
'multiple'    => 'multiple',
        );
    
?>
    <div class="form-group">
        <?= form_label('Categories','categories'); ?>
        <?= form_dropdown('categories[]'$categories_options0$data); ?>
    </div> 

Now in order to make it possible to post an id without having to pass a parameter in the method(for what I know).


I will have to post whatever data I want(that's the previous block of code in the controller). Now after that insert page(comment in the controller), I will obtain the id of the categories that were passed in the view by doing this(I hope I could make myself clear on this):

PHP Code:
            // Insert Categories into CI_TERMS_TAXONOMY - term_taxonomy_id, term_id, type, body, parent_id, count
            
$categories $this->Terms_model->get_list();
            
            
$data = array(
                
'term_taxonomy_id'    => $this->db->insert_id(),
                
'term_id'            => $categories->id,
                
'type'                => 'category',
            );
            
            
$this->Taxonomy_model->add($data); 


So far, it only trows me an error of term_id cannot be null.

Any feedback would be very appreciated.

If you notice, I'm trying to build something similar to WordPress regarding its relationship between the datatables "wp_terms, wp_terms_taxonomy, wp_terms_relationship".

Thanks in advance.

 Why dont you try var_dump($categories) to see what returns.  Check if it is indeed an object or array of objects so on, the chance is id can be serialized out in someway within categories

Regards

Hey, adfter spending several hours trying and looking for tutorials or some example on Internet, I finally came up with this solution:

SOLUTION ON STACKOVERFLOW

PHP Code:
            $categories $this->input->post('categories[]');
            foreach (
$categories as $category_id){
                
$this->db->insert('ci_terms_relationship', array(
                
'post_id' => $this->db->insert_id(),
                
'term_taxonomy_id' => $category_id
                
));
            }