如何访问从模型中的数组返回到控制器的数据以显示表库

时间:2016-05-11 15:54:40

标签: php mysql arrays codeigniter codeigniter-3

我试图在表格中显示从数组中返回的所有数据,但它给出了一条错误消息:

A PHP Error was encountered Severity: WarningMessage: Illegal string offset 'mano_obra'Filename: controllers/control.phpLine Number: 1475 A PHP Error was encountered Severity: WarningMessage: Illegal string offset 'nomina'Filename: controllers/control.phpLine Number: 1476 A PHP Error was encountered Severity: WarningMessage: Illegal string offset 'nombre'Filename: controllers/control.phpLine Number: 1477 A PHP Error was encountered Severity: WarningMessage: Illegal string offset 'hojalateria'Filename: controllers/control.phpLine Number: 1478 A PHP Error was encountered Severity: WarningMessage: Illegal string offset 'pintura'Filename: controllers/control.phpLine Number: 1479 A PHP Error was encountered Severity: WarningMessage: Illegal string offset 'mecanica'Filename: controllers/control.phpLine Number: 1480 A PHP Error was encountered Severity: WarningMessage: Illegal string offset 'refaccion'Filename: controllers/control.phpLine Number: 1481

我的模型中的代码有效,因为它将数据返回给控制器。我想知道如何访问该数据,因为如果我只填写字段名称,因为我在var_dump();

中找回了该字段,这会给我一个错误
$registro['mano_obra'],

或者如果我将tablename后跟fieldname

$registro['cm_valuacion.mano_obra'],

型号:

function get_reg3($id){
$query = ('
    SELECT DISTINCT
        cm_valuacion.mano_obra,
        cm_nomina.nomina,
        cm_empleado.nombre,
        cm_proveedor.nombre,
        cm_valuacion.hojalateria,
        cm_valuacion.pintura,
        cm_valuacion.mecanica,
        cm_valuacion.refaccion,
        cm_valuacion.tipo,
        cm_valuacion.shojalateria,
        cm_valuacion.spintura,
        cm_valuacion.smecanica,
        cm_valuacion.costoHojalateria,
        cm_valuacion.costoPintura,
        cm_valuacion.costoMecanica,
        cm_valuacion.pv_hojalateria,
        cm_valuacion.pv_pintura,
        cm_valuacion.pv_mecanica,
        cm_valuacion.pc_hojalateria,
        cm_valuacion.pc_pintura,
        cm_valuacion.pc_mecanica,
        cm_compras.precio,
        cm_compras.status
    FROM cm_valuacionr,
        cm_nomina
    INNER JOIN cm_empleado
        ON cm_nomina.id_empleado = cm_empleado.id
    INNER JOIN cm_valuacion
        ON cm_valuacion.id_siniestro = cm_nomina.id_siniestro
    INNER JOIN cm_compras
        ON cm_compras.id_siniestro = cm_valuacion.id_siniestro
    INNER JOIN cm_proveedor
        ON cm_compras.id_proveedor = cm_proveedor.id
    WHERE cm_valuacion.id_siniestro = ?
        AND cm_valuacion.id_complemento = 0
    GROUP BY mano_obra
        ');
    $query =  $this->db->query($query, $id); # Changed
    $result = $query->result_array(); # Added
    return $result; # Added
}
控制器中的

    $registros = $this->joins_model->get_reg3($id);
    var_dump($registros);
    if (empty($registros)) 
    {
        echo "No data recived from Database";
    }else{
        $this->load->library('table');
        $this->table->set_empty(" ");
        $this->table->set_heading('D',
                              'CANT.',
                              'PROVEEDOR',
                              'NOTA',
                              'DESCRIPCION',
                              'COSTO',
                              'VENTA',
                              'EMPLEADO',
                              'NOMINA',
                              'TOTAL'
                            );  # Changed

        foreach ($registros[0] as $registro) # Changed
        {  
        $this->table->add_row(
            $registro['mano_obra'],
            $registro['nomina'],
            $registro['nombre'],
            $registro['hojalateria'],
            $registro['pintura'],
            $registro['mecanica'],
            $registro['refaccion'],
            $registro['tipo'],
            $registro['shojalateria'],
            $registro['spintura'],
            $registro['smecenica'],
            $registro['costoHojalateria'],
            $registro['costoPintura'],
            $registro['costoMecanica'],
            $registro['pv_hojalateria'],
            $registro['pv_pintura'],
            $registro['pv_mecanica'],
            $registro['pc_hojalateria'],
            $registro['pc_pintura'],
            $registro['pc_mecanica'],
            $registro['precio'],
            $registro['status']


            );
        }
        echo $this->table->generate();
    }

这是我在控制器{/ 1>}之后得到的var_dump($registros);

array (size=1)
  0 => 
    array (size=22)
      'mano_obra' => string 'CAMBIO DE RADIADOR' (length=18)
      'nomina' => string '89.00000000000000' (length=17)
      'nombre' => string 'Proveedor' (length=15)
      'hojalateria' => string '' (length=0)
      'pintura' => string '' (length=0)
      'mecanica' => string '250' (length=3)
      'refaccion' => string 'REDIADOR' (length=8)
      'tipo' => string '' (length=0)
      'shojalateria' => string '' (length=0)
      'spintura' => string '' (length=0)
      'smecanica' => string '' (length=0)
      'costoHojalateria' => string '0' (length=1)
      'costoPintura' => string '0' (length=1)
      'costoMecanica' => string '0' (length=1)
      'pv_hojalateria' => string '' (length=0)
      'pv_pintura' => string '' (length=0)
      'pv_mecanica' => string '' (length=0)
      'pc_hojalateria' => string '' (length=0)
      'pc_pintura' => string '' (length=0)
      'pc_mecanica' => string '' (length=0)
      'precio' => string '1850.0000' (length=9)
      'status' => string '1' (length=1)

2 个答案:

答案 0 :(得分:2)

更改此

foreach ($registros[0] as $registro) # Changed

到这个

foreach ($registros as $registro) # Changed
  

同时确保添加$result = $query->result_array();。它仅适用于result_array()

  

然后将CI表格更改为普通表格并尝试是否正常工作

答案 1 :(得分:1)

我认为问题出在这一行:

foreach ($registros[0] as $registro)

应该是:

foreach ($registros as $registro)