Inner Join in Codeigniter

0

I'm trying to show the name of a provider, as well as the name of the garden instead of its id from the table entries.

The problem is that I do not know how the syntax is in Codeigniter to bring and show that data.

Model:

public function getEntradas(){
     $this->db->join('proveedor', 'proveedor.id = entrada.PROVEEDOR');
     $result = $this->db->get('entrada');
     return $result->result();
}

Controller:

$data = array(
    'entradas' => $this->Modelfruta->getEntradas()
);
$this->load->view('content/head');
$this->load->view('content/aside');
$this->load->view('pages/proceso/fruta/viewfruta',$data);
$this->load->view('content/footer');
$this->load->view('content/scrips/proceso/srcontrols');

View:

<?php if(!empty($entradas)): ?>
  <?php foreach($entradas as $entradas): ?>
    <tr role="row" class="odd">
     <td tabindex="0" class="sorting_1"><?php echo $entradas->id; ?></td>
     <td><img src="<?php echo base_url(); ?>assets/images/profile.png" class="avatar" alt="Avatar"><span style="margin-left:10px;"></span><?php echo $entradas->PROVEEDOR; ?></td>
     <td><?php echo $entradas->peso; ?></td>
     <td><?php echo $entradas->cajas; ?></td>
     <td><?php echo $entradas->hora; ?></td>
     <td><a href="" class="btn btn-danger btn-xs btn-remove">No impreso</a></td>
     <td><button type="button" class="btn btn-primary btn-xs">Ingresado</button></td>
     <td><a href="" class="btn btn-warning btn-xs">Modificar</a>
         <a href="" class="btn btn-danger btn-xs btn-remove">Eliminar</a>    
     </td>
    </tr>
  <?php endforeach; ?>
  

Columns of my table:

<thead>
   <tr role="row">
   <th class="sorting_asc" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 100px;" aria-sort="ascending" aria-label="Name: activate to sort column descending">#</th>
  <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 200px;" aria-label="Position: activate to sort column ascending">Proveedor</th>
  <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 100px;" aria-label="Office: activate to sort column ascending">Peso</th>
  <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 100px;" aria-label="Age: activate to sort column ascending">Cajas</th>
  <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 100px;" aria-label="Start date: activate to sort column ascending">Hora</th>
  <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 100px;" aria-label="Start date: activate to sort column ascending">Impresion</th>
 <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 100px;" aria-label="Start date: activate to sort column ascending">Etapa procesada</th>
 <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 100px;" aria-label="Salary: activate to sort column ascending">Herramientas</th></tr>
</thead>

How can I show the name? As I currently have it, it does not show me anything.

    
asked by DoubleM 11.04.2018 в 01:35
source

1 answer

1

Perform a INNER JOIN in Codeigniter you can do it in the following way:

Controller:

public function getEntradas()
{
    $data['entradas'] = $this->Data_model->getEntradas();
    $this->load->view('admin/nombre', $data);
}

Model:

public function getEntradas()
{
    $this->db->select('a.*,d.*');
    $this->db->from('entradas a');
    $this->db->join('proveedor d', 'a.proveedor = d.id');

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

    if(!$aResult->num_rows() == 1)
    {
        return false;
    }

    return $aResult->result_array();
}

View:

<table>
  <tr>
    <th>ID</th>
    <th>campo 1</th>
    <th>campo 2</th>
    <th>campo 3</th>
  </tr>
  <?php foreach($entradas as $entrada){ ?>
  <tr>
    <td><?php echo $entrada['id']; ?></td>
    <td><?php echo $entrada['campo1']; ?></td>
    <td><?php echo $entrada['campo2']; ?></td>
    <td><?php echo $entrada['campo3']; ?></td>
  </tr>
  <? } ?>
</table>

This way you print all the information that you bring of your query, going through the arrangement and showing it by the name of your field. I guessed what you had since you do not show the tables in your database to tell you exactly how to do it, but this is more than enough and it is very simple.

    
answered by 11.04.2018 / 07:27
source