Search code examples
selectnestedcodeigniter-2

get separate result from returning array in codeigniter


I would like to tell you that I am stuck in the simple logic.I know it is so simple but I need a help this time.

I have an array <strong>$allCategories</strong> that returns data from db table. ( select * from tablename) 
Structure is like this:

==================================================================
ID     CATEGORY NAME    DESCRIPTION    PARENT CATEGORY
==================================================================
1             ABCD                   lorem ipsum                   2
2               JKL                    lorem ipsum                    0
3             WXYZ                  lorem ipsum                    1


I want to show Category name instead of digits.Actually The digits are the Id of the Cateogry name. 
Like JKL comes instead of 2 and ABCD instead of 1.
<?php $i =1;
    foreach($allCategories as $ab){
 ?>
  <tr class="pointer">
  <td class=" "><?php echo $i ?></td>
   <td class=" "><?php echo $ab['category_name'] ?></td>
   <td class=" "><?php echo $ab['category_description']; ?></td>
   <td class=" ">
    <?php
        $parent_category = $ab['parent_category'];
        return $this->db->select('category_name')
        ->from('categories')
         ->where('id', $parent_category)
        ->get()
        ->row();
        //echo $ab['parent_category'] 
    ?>
    </td>
    </tr>
    <?php
       $i++;
       }                            
    ?>  

If you want any information or query regarding my ques. ask me. Thanks in advance :)


Solution

  • Don't use "return" here. Change it like this:

    echo $this->db->select('category_name')
        ->from('categories')
         ->where('id', $parent_category)
        ->get()
        ->row()["category_name"];