Rizqi C
Rizqi C

Reputation: 33

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

I am new in CI, I follow a tutorial from website then I've got 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 the code echo $this->table->set_template($data); it's always give '1' as an output. Is anyone can help?

Upvotes: 3

Views: 406

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38672

Load the library in your Controller

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

or Config/autoload.php

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

set_heading function always returns object.So no use of echo

So Your Code(Remove 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

Upvotes: 2

Shaiful Islam
Shaiful Islam

Reputation: 7134

Your Answer:

Remove all echo except echo $this->table->generate(); this line.

Error Reasons:

set_template function returns true or false. if you echo this it will display 1 as it is returning true.

Remove echo from this line echo $this->table->set_heading($col1, $col2, $col3, $col4, $option); It will display that error message.

set_heading function returns object.You cannot echo object. remove echo or try to print_r or var_dump on that function.

For same reason you cannot use echo $item;

add_row function returns object and you assigning it to $item and You cannot echo object.

Upvotes: 1

Related Questions