Welcome Guest, Not a member yet? Register   Sign In
Category and Sub-Category
#1

[eluser]mstojanov[/eluser]
Hello guys what i'm trying to accomplish here is to have 3 categories and in each 3 categories to have sub-categories.

What i cant figure out how to insert the parent_id of the sub-category which i'm inserting.


Here is my model :

Code:
$p_id = $this->db->insert_id();
$category= array(

   'sub_cat_name'=>$this->input->post('category'),
   'post_id'=>$p_id,
   'parent_id'=> ???
   );


  $this->db->insert('sub_category',$category);

In my db i have :

Table : POSTS
Table: categories
fields : cat_id(AUTO INCREMENT), cat_name
example :
cat_id = 1 ; cat_name = Cars
cat_id = 2 ; cat_name = Phone

Table: sub_category
fields : sub_id(AUTO INCREMENT), parent_id, sub_cat_name, post_id

As you can see before i try to insert into the category i'm inserting the post first and the i'm inserting the category in the category table
thats why i'm getting the post id like this way mentioned in the begginig

Code:
$p_id = $this->db->insert_id();

And here is my view :

Code:
<select `name="category"  class="form-control" >
<option value=""> --Choose category--</option>
        <option class="cat_head" value="Cars" &lt;?php echo set_select('kategorija', 'Cars'); ?&gt; >Cars</option>
<option value="BMW" &lt;?php echo set_select('category', BMW'); ?&gt; >BMW</option>
<option value="AUDI" &lt;?php echo set_select('category', 'AUDI'); ?&gt; >AUDI</option>
<option value="RENAULT" &lt;?php echo set_select('category', 'RENAULT'); ?&gt; >RENAULT</option>
&lt;!-- Cars End --&gt;

&lt;!-- Phone Start --&gt;
<option class="cat_head" value="Phone" &lt;?php echo set_select('kategorija', 'Phone'); ?&gt; >Phone</option>
<option value="Samsung" &lt;?php echo set_select('category', 'Samsung'); ?&gt; >Samsung</option>
<option value="Nokia" &lt;?php echo set_select('category', 'Nokia'); ?&gt; >Nokia</option>
&lt;!-- Phone End --&gt;
</select>

What i want here is when somebody select example BMW the parent category id to be CARS and that for AUDI too.

So in that way if somebody select BMW i can list my category as Cars or if only select CARS and not select some sub-category to be able to list in CARS category.

How can i accomplish this ? To insert the parent ID to my database.
Or if my database fields or tables are not ordered right, give me the correct answer.

Thanks guys
Waiting for help/


Messages In This Thread
Category and Sub-Category - by El Forum - 12-07-2013, 09:08 PM
Category and Sub-Category - by El Forum - 12-08-2013, 11:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB