Madhu Kumar
Madhu Kumar

Reputation: 175

How to fetch values from two different tables in codeIgniter 3.x.x

I am came up with another query here, I was googled for it but no luck. Now I hope I will get solution here. My problem is,

I have two tables images and categories. images table is shown below.this is images table . In this table both category Image and Icon will be stored in image_name column only, where id of icon and image will be stored in other table as foreign key.

categories table is shown below.

So my problem here is, I want to show all the category names with the category image and category Icon. but When I am writing the model query, It is fetching only images but not the icons.

My model query:

 public function getAllCatsForAdminPanel(){
       //extract($data);

       $this->db->select('categories.id AS cat_id, categories.cat_name AS cat_name, categories.display_order AS cat_dis_order, images.image_name as imageName');
       $this->db->from('categories');
       $this->db->where('categories.status =', 'a');
       $this->db->join('images', 'images.id = categories.cat_icon', 'left');
       $this->db->join('images as im', 'im.id = categories.cat_image', 'left');

      $query = $this->db->get();

       //$query = $this->db->get('categories');
       //print_r($query);
       if($query->num_rows()>0){
        return $query->result();
       }else{
        return false;
       }
}

my controller:

 public function category(){
    logActivity();
    $cats_data['all_cats'] = $this->cat_model->getAllCatsForAdminPanel();
       //print_r($cats_data);
    $this->load->view('admin/category', $cats_data);

}//close category

my view code:

<?php 
            if(count($all_cats) > 0){
              //print_r($all_cats);
              foreach($all_cats as $all_cat){
              ?>  
              <tr>
              <td><?php echo ++$counter; ?></td>
              <td><?php echo $all_cat->cat_name; ?></td>
              <td><?php echo $all_cat->cat_dis_order; ?></td>
              <td><?php echo $all_cat->imageName; ?></td>
              <td><?php echo $all_cat->imageName; ?></td>
              <td>
                <a href="<?php echo base_url('admin/admin/modifyCategory/'.$all_cat->cat_id); ?>"><i class="fa fa-pencil" aria-hidden="true"></i></a>
              </td>
              <td><button class = "btn btn-danger" onclick="deleteCategory(<?php echo $all_cat->cat_id; ?>);"><i class="fa fa-trash" aria-hidden="true"></i></button></td>

              </tr>
            <?php 
                }
              }
              else{
                  echo "no records found";
                }
            ?>

screenshot of my view table:

Category table UI Image

Here in this table, the spotted red color names should be different but they are same. Please help me out!!! Thanks in advance...

Upvotes: 0

Views: 1839

Answers (4)

Anupriya Malaiya
Anupriya Malaiya

Reputation: 11

Replace your controller Line:

$this->db->select('
  categories.id AS cat_id,
  categories.cat_name AS cat_name,
  categories.display_order AS cat_dis_order,
  images.image_name as imageName'); 

with

$this->db->select('
  categories.id AS cat_id, 
  categories.cat_name AS cat_name,
  categories.display_order AS cat_dis_order,
  images.image_name AS cat_icon,
  im.image_name AS cat_image');

In View use this code :

<?php 
    if(count($all_cats) > 0){
      //print_r($all_cats);
      foreach($all_cats as $all_cat){
      ?>  
      <tr>
      <td><?php echo ++$counter; ?></td>
      <td><?php echo $all_cat->cat_name; ?></td>
      <td><?php echo $all_cat->cat_dis_order; ?></td>
      <td><?php echo $all_cat->cat_image; ?></td>
      <td><?php echo $all_cat->cat_icon; ?></td>
      <td>
        <a href="<?php echo base_url('admin/admin/modifyCategory/'.$all_cat->cat_id); ?>"><i class="fa fa-pencil" aria-hidden="true"></i></a>
      </td>
      <td><button class = "btn btn-danger" onclick="deleteCategory(<?php echo $all_cat->cat_id; ?>);"><i class="fa fa-trash" aria-hidden="true"></i></button></td>

      </tr>
    <?php 
        }
      }
      else{
          echo "no records found";
        }
?>

Upvotes: 1

Heartine Lazar
Heartine Lazar

Reputation: 153

Try this out:

Model Modifications

$this->db->select('categories.id AS cat_id, categories.cat_name AS cat_name, categories.display_order AS cat_dis_order, images.image_name as iconName, im.image_name as imageName');

View modifications

<td><?php echo ++$counter; ?></td>
<td><?php echo $all_cat->cat_name; ?></td>
<td><?php echo $all_cat->cat_dis_order; ?></td>
<td><?php echo $all_cat->imageName; ?></td>
<td><?php echo $all_cat->iconName; ?></td>

Upvotes: 2

Vikas Umrao
Vikas Umrao

Reputation: 2615

Change your controller as (you need to select the icon from second join from images table as im , we have named it iconName) (Please note , i have not tested it):

public function getAllCatsForAdminPanel(){
       //extract($data);

       $this->db->select('categories.id AS cat_id, categories.cat_name AS cat_name, categories.display_order AS cat_dis_order, images.image_name as imageName,im.image_name as iconName');
       $this->db->from('categories');
       $this->db->where('categories.status =', 'a');
       $this->db->join('images', 'images.id = categories.cat_icon', 'left');
       $this->db->join('images as im', 'im.id = categories.cat_image', 'left');

      $query = $this->db->get();

       //$query = $this->db->get('categories');
       //print_r($query);
       if($query->num_rows()>0){
        return $query->result();
       }else{
        return false;
       }
}

Then in our view we need to write icon as iconName as:

<?php 
            if(count($all_cats) > 0){
              //print_r($all_cats);
              foreach($all_cats as $all_cat){
              ?>  
              <tr>
              <td><?php echo ++$counter; ?></td>
              <td><?php echo $all_cat->cat_name; ?></td>
              <td><?php echo $all_cat->cat_dis_order; ?></td>
              <td><?php echo $all_cat->imageName; ?></td>
              <td><?php echo $all_cat->iconName; ?></td>
              <td>
                <a href="<?php echo base_url('admin/admin/modifyCategory/'.$all_cat->cat_id); ?>"><i class="fa fa-pencil" aria-hidden="true"></i></a>
              </td>
              <td><button class = "btn btn-danger" onclick="deleteCategory(<?php echo $all_cat->cat_id; ?>);"><i class="fa fa-trash" aria-hidden="true"></i></button></td>

              </tr>
            <?php 
                }
              }
              else{
                  echo "no records found";
                }
            ?>

Upvotes: 1

Naga
Naga

Reputation: 2168

If I am not mistaken, Please update the below lines,

In Model

$this->db->select('categories.id AS cat_id, categories.cat_name AS cat_name, categories.display_order AS cat_dis_order, images.image_name as imageName, images.image_label as imageLabel');

In View,

<td><?php echo ++$counter; ?></td>
<td><?php echo $all_cat->cat_name; ?></td>
<td><?php echo $all_cat->cat_dis_order; ?></td>
<td><?php echo $all_cat->imageName; ?></td>
<td><?php echo $all_cat->imageLabel; ?></td>

Upvotes: 1

Related Questions