learningbyexample learningbyexample - 6 months ago 18
SQL Question

How do I access data returned from an array in my model to my controller to display with the table library

I am trying to display all the data that I got back from the array in a table but it gives me an error message:

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


The code that I have in my model works since it is returning the data to the controller. I want to know how to access that data since it gives me an error if i put just the field name as I am getting it back in the
var_dump();


$registro['mano_obra'],


or if I put the tablename followed by the fieldname

$registro['cm_valuacion.mano_obra'],


Model:

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
}


in the controller

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


this is what I get after
var_dump($registros);
from the controller

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)

Answer

Change this

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

to this

foreach ($registros as $registro) # Changed

As well make sure this is added $result = $query->result_array();. Its only works with result_array()


And change you CI tables to normal table and try whether its working

Comments