【问题标题】:Variable inner foreach - Codeigniter变量内部 foreach - Codeigniter
【发布时间】:2020-05-07 19:48:37
【问题描述】:

我需要将 listarCliente 模型返回到我的视图中,以便从我的 Foreach 中的另一个表中选择一个值,但没有成功并且 var_dump 返回 null

型号

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();
}

控制器

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;

【问题讨论】:

  • $id 定义在哪里?
  • 因为你的$id没有定义。
  • @KayoBruno 我可以在哪里定义我的视图?我需要在 foreach 内完成

标签: php codeigniter


【解决方案1】:

$id 参数添加到控制器

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);
}

现在尝试像这样访问控件

localhost/ci_path/index.php/your_controller/3

这里3registros id。

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 2015-11-05
    • 2023-03-19
    • 1970-01-01
    • 2017-08-27
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    相关资源
    最近更新 更多