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_options, 0, $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.
I do Front-End development most of the time