Skip to content
Advertisement

Variable inner foreach – Codeigniter

I need return listarCliente model to my view, to select a value from another table inside my Foreach, but havent sucess and var_dump return null

Model

function listarCliente($id){
    $this->db->from('ga845_clientes');
    $this->db->join('ga845_cupons', 'ga845_cupons.clientes_id = ga845_clientes.id');
    $this->db->where('ga845_cupons.id', $id);
    $query = $this->db->get();

    return $query->result();
}

Controller

public function index(){
    $cliente_cp = $this->cupons_model->listarCliente($id);

    $data['cliente_cp']    = $cliente_cp;
    $data['registros']      = $this->cupons_model->listar();

    $this->template->set('titulo_pagina', 'Administração | Cupons');
    $this->template->load('adm/template', 'adm/cupons', $data);
}

PHP

foreach($registros as $registro):

    echo "<tr>";
    echo "<td><span style='display:none;'>".$registro->valido_ate."</span>".converteData($registro->valido_ate)."</td>";
    echo "<td>".$registro->tipo_cupom."</td>";
    echo "<td>".$cliente_cp[0]->nome."</td>"; //need return here.
    echo "<td>".($registro->tipo_desconto == 'fixo' ? 'R$ '.ConverteReal($registro->valor) : $registro->porcentagem.'%')."</td>";
    echo "<td>".$registro->codigo."</td>";
    echo "<td>".($registro->enviado == 1 ? 'Sim' : 'Não')."</td>";
    echo "<td>".($registro->status == 1 ? 'Sim' : 'Não')."</td>";
    echo "<td class="toolbar">
            <div class="btn-group" >
                <a class="btn btn-flat" href="".base_url()."adm/cupons/alterar/".$registro->id."">
                    <span class="awe-wrench"></span>
                </a>
                <button class="btn btn-flat" onclick="if(confirm('Deseja realmente excluir o registro?')) window.location.href='".base_url()."adm/cupons/excluir/".$registro->id."';">
                    <span class="awe-remove"></span>
                </button>
            </div>
          </td>";
    echo "</tr>";
    endforeach;

Advertisement

Answer

Add the $id argument to the controller

public function index($id){ //<--here
    $cliente_cp = $this->cupons_model->listarCliente($id);

    $data['cliente_cp']    = $cliente_cp;
    $data['registros']      = $this->cupons_model->listar();

    $this->template->set('titulo_pagina', 'Administração | Cupons');
    $this->template->load('adm/template', 'adm/cupons', $data);
}

Now trying to access the controlle like this

localhost/ci_path/index.php/your_controller/3

Here 3 is registros id.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement