2

I'm having trouble understanding how to place grocery_CRUD into a view.

My function in my control looks like this:

function folio()
{
$crud = new grocery_CRUD();
$crud->set_table('rfid');
$crud->columns('rfid','timestamp');
$data['output'] = $crud->render();      
$data['title'] = 'Control Escolar - Folio';
$this->db->where('id', $this->uri->segment(3));
$data['query'] = $this->db->get('alumnos');
$this->load->view('folio_view', $data);
}

The code in my view file looks like this.

        <?php foreach($query->result() as $row) { ?>
    Datos Vitales - <B>Status:</b> <?=$row->status?><hr>
    <B>Nombre:</b> <?=$row->nombre?> <B>Fecha de Nac:</B> <?=$row->fecha_nac?></br>
    <B>Edad:</b> <?=$row->edad?> <B>Sexo:</B> <?=$row->sexo?> <B>Lugar de Nac:</B> <?=$row->lugar_nac?>
    <B>Nacionalidad:</B> <?=$row->nacionalidad?><br><br>
    Datos Escolar<hr>
    <B>Folio:</b> <?=$row->folio?> <B>Grado:</b> <?=$row->grado?> <B>Grupo:</b> <?=$row->grupo?> <B>Turno:</b> <?=$row->turno?> <B>RFID:</b> <?=$row->rfid?><br><br>
    Datos Generales<hr>
    <B>Domicilio:</b> <?=$row->domicilio?> <B>Colonia:</b> <?=$row->colonia?> <B>Codigo Postal:</b> <?=$row->cpostal?></br>
    <B>Telefono Particular:</b> <?=$row->telepart?> <B>Municipio:</b> <?=$row->municipio?></br>
    <B>Telefono de Emergencia:</b> <?=$row->tele_emer?> <B>Tutor:</b> <?=$row->tutor?><br><br>
    Datos Medicos<hr>
    <B>Tipo de Sangre:</b> <?=$row->tipo_de_sangre?></br>
    <B>Condiciones Conocidas:</b> <?=$row->condiciones?></br></br>
    <hr>
    <B>Entradas/Salidas Registradas:</b>
    <?php echo $output; ?>
    <?php } ?>

Which works well, however when I run it I get this where the CRUD should appear:

A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: views/folio_view.php
Line Number: 65

What I'm trying to actually accomplish is for the CRUD to show the only the RFID entries matching the RFID field matching the student.

Frank Barcenas
  • 611
  • 1
  • 5
  • 18

1 Answers1

3

Actually the error describes exactly what is wrong there. So the problem is that you try to echo an object.

So imagine that your $output is an object like this:

stdClass Object
(
    [output] => Your output will appear here....
    [js_files] => Array
        (
            ...
        )

    [css_files] => Array
        (
            ...
        )

)

So the solution to your problem is simply this:

echo $output->output;

Also consider that the only thing that grocery CRUD requires is to show somewhere the js_files and the css_files , so don't forget to have this in your template or view:

<?php foreach($output->css_files as $file): ?>
        <link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>

<?php foreach($output->js_files as $file): ?>
    <script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>

If you want to be more "readable" to you can simply do this:

$data['gcrud'] = $crud->render();

And in your view/template to have something like this:

<?php foreach($gcrud->css_files as $file): ?>
        <link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>

<?php foreach($gcrud->js_files as $file): ?>
    <script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>

...

<?php echo $gcrud->output; ?>

Lastly but not least, consider that the thing that you are trying to do will not work in the first place as I understood you want multiple CRUDs in one page:

<?php foreach($query->result() as $row) { ?>
    ...
    <B>Entradas/Salidas Registradas:</b>
    <?php echo $output->output; ?>
<?php } ?>

However you will realize that for the time being it will not work well for you as grocery CRUD doesn't support multiple CRUD forms in one page for the moment. So if you actually want to use grocery CRUD you have to change the logic that the view will show it

John Skoumbourdis
  • 3,041
  • 28
  • 34