How can I solve "Object of class CI_Table cannot be converted to string"?

I am new to CI, I am following the tutorial from the site then I have this error. Here's my view code:

$data = array('table_open' => '<table class = "table-bordered">');

echo $this->table->set_template($data);

$col1 = array('data' => 'No');
$col2 = array('class' => 'col-md-4', 'data' => 'Agenda');
$col3 = array('class' => 'col-md-2', 'data' => 'Category');
$col4 = array('class' => 'col-md-2', 'data' => 'Date');
$option = array('class' => 'col-md-4', 'data' => 'Option');

echo $this->table->set_heading($col1, $col2, $col3, $col4, $option);
// echo $this->table->set_heading('No', 'Agenda', 'Category', 'Date', 'Option');
$no = 1;
if($agenda > 0) {
    foreach($agenda as $ag) {

        $item = $this->table->add_row($no++, $ag->agenda, $ag->agenda_cat, $ag->due_date,
                anchor('agenda/edit' . $ag->id_agenda, 'Edit', array('class' => 'btn btn-info col-md-offset-1')) . " " .
                anchor('agenda/delete' . $ag->id_agenda, 'Delete', array('class' => 'btn btn-danger'))
            );
    }
    echo $item;
}

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

      

Errors always occur for table->set_heading()

and table->add_rows()

.

And for echo code, $this->table->set_template($data);

it always gives "1" as output. Can anyone please help?

+3


source to share


2 answers


Your Answer:

Delete everything echo

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

this line.

Reasons for errors:

Function

set_template

returns true or false. if you repeat this it will display 1 when it returns.



Remove echo

from this line echo $this->table->set_heading($col1, $col2, $col3, $col4, $option);

It will display this error message.

Function

set_heading

returns an object. You cannot echo an object. uninstall echo

or try print_r

or var_dump

in this function.

For the same reason, you cannot use echo $item;

Function

add_row

returns an object and assigns the value to $ item and you can't echo an object.

+1


source


Load the library into your controller

$this->load->library('table');

      

or Config/autoload.php

$autoload['libraries'] = array('table');

      



Function

set_heading

always returns an object. For useecho

So your code is (Delete echo

)

<?php
    $data = array(
        'table_open' => '<table class="table-bordered">');

     $this->table->set_template($data);

    $col1 = array('data' => 'No');
    $col2 = array('class' => 'col-md-4', 'data' => 'Agenda');
    $col3 = array('class' => 'col-md-2', 'data' => 'Category');
    $col4 = array('class' => 'col-md-2', 'data' => 'Date');
    $option = array('class' => 'col-md-4', 'data' => 'Option');

    $this->table->set_heading($col1, $col2, $col3, $col4, $option);
    // echo $this->table->set_heading('No', 'Agenda', 'Category', 'Date', 'Option');
    $no = 0;

    if(empty($agenda))
    {
        foreach($agenda as $ag)
        {
            $this->table->add_row($no++, $ag['agenda'], $ag['agenda_cat'], $ag['due_date'],
                anchor('agenda/edit' . $ag['id_agenda'], 'Edit', array('class' => 'btn btn-info col-md-offset-1')) . " " .
                anchor('agenda/delete' . $ag['id_agenda'], 'Delete', array('class' => 'btn btn-danger')));
        }
    }
    else
    {

    }

      

codeigniter table

+2


source







All Articles