0

I am trying to display data from a database table I am using codeigniter elementary thing, but I do not understand what the problem is

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: record

Filename: views/main.php

Line Number: 55

controller Main.php

public function index()
{
    $data['records'] = $this->PhoneBook->get_records();
    $this->load->view('main',$data);
}

model PhoneBook.php

public function get_records($conditions = null, $limit = null, $offset = 0, $order = null)
{
    if ($conditions != null) {
        $this->db->where($conditions);
    }
    if ($order != null) {
        $this->db->order_by($order);
    }
    $query = $this->db->get('phonebook', $limit, $offset);
    return $query->result();
}

view main.php

<table class="table table-bordered">
  <tr>
    <td class="active">Name</td>
    <td class="active">Discription </td>
    <td class="active">Phone</td>
    <td class="active">Actions</td>
  </tr>

  <? foreach ($records as $record) {?>
  <tr>
    <td class="active"><?= $record->name ?></td>
    <td class="active"><?= $record->discription ?></td>
    <td class="active"><?= $record->phone ?></td>
    <td class="active">Actions</td>
  </tr>
  <? } ?>

</table>

Line Number: 55 is

 <td class="active"><?= $record->name ?></td>

array $data

Array
(
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1
                    [name] => фффф
                    [discription] => ыыыы
                    [phone] => 8454
                )

            [1] => stdClass Object
                (
                    [id] => 2
                    [name] => ппапрпр
                    [discription] => аааааааааааа
                    [phone] => 645645
                )

        )

)
  • Are you sure that your query returns some records? – Franco Oct 04 '16 at 08:38
  • Yes, query returns: Array ( [records] => Array ( [0] => stdClass Object ( [id] => 1 [name] => фффф [discription] => ыыыы [phone] => 8454 ) [1] => stdClass Object ( [id] => 2 [name] => ппапрпр [discription] => аааааааааааа [phone] => 645645 ) ) ) – Jennifer Vasylenko Oct 04 '16 at 08:47
  • Try to change $data['records] to $this->data['records]. Additionally you need to use – Franco Oct 04 '16 at 08:51
  • And use name ; ?> for your vars – Franco Oct 04 '16 at 08:58
  • If I using I have unexpected end of file, change $data['records] to $this->data['records] not working, name ; ?> not change enything – Jennifer Vasylenko Oct 04 '16 at 09:08
  • You need to do that also here – Franco Oct 04 '16 at 09:12
  • Thank you, very match! I do not understand why it is so but in other files, there was no need to use the full form... it works now – Jennifer Vasylenko Oct 04 '16 at 09:16
  • Great :) Glad it helps. Anyway as additional note You should before you make the foreach() loop check if the $records is not empty otherwise you will get an error. It is a good practice to check for this and if thera are no records in the databse to show a message saying i.e "There are no records yet" – Franco Oct 04 '16 at 09:20

0 Answers0