Hi. Im new to codeigniter, so im not sure if its something wrong with the codeigniter or my php. what im trying to do is if the records from a database table is empty echo a message. The message which ,"No records exist!", does display but then underneath is is a php error message box:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

list.php

<body>
  <ol>
    <?php
    foreach ($result as $record): ?>
      <li>
      Name : <?php echo $record->user; ?><br>
      Email : <?php echo $record->email; ?><br>
      Date / Time : <?php echo $record->postdate." / ".$record->posttime; ?><br>
      Comment : <?php echo nl2br($record->comment); ?><br>
      Action : <a href="<?php echo base_url('myguestbook/edit/'); echo $record->id; ?>">Edit</a>
      / <a href="<?php echo base_url('myguestbook/remove/'); echo $record->id; ?>">Delete</a>
      <hr>
      </li>
    <?php endforeach; ?>
  </ol>
  <div align="center">
    [ <a href="<?php echo base_url('myguestbook/create/'); ?>">Add</a> ]
  </div>
</body>

model

function read($where, $length, $start) {
        $this->db->limit($length, $start);
        $query = $this->db->get_where('myguestbook', $where);
        if ($query->num_rows() > 0) {
          return $query->result();
        }
        else{
            echo "No records exist!";
        }
    }

controller

public function view() {
      $data['result'] = $this->myguestbook_model->read(null, null, null);
      $data['title'] = 'List of All Comments';
      $this->load->view('list', $data);
    }

Hi,

according to the source of the get_where() method in system/database/DB_query_builder.php, sending NULL is acceptable as it would skip to set the WHERE conditions. So looking at your code:

$data['result'] = $this->myguestbook_model->read(null, null, null);

It seems it should work fine. So, in order to debug, change the configuration so that it returns to you the last performed query: in application/config/database.php set save_queries to boolean TRUE, then go back to your model and right after line 3, i.e. where you perform the $query, write:

print $this->db->last_query();
die;

And see what you get. Then try to run the query into a MySQL client and see if you get results. Once you end the debugging process set save_queries to FALSE again, as it slows down the execution of the code. Also, for a better debug process I suggest you to add Kint:

Can I ask why you are using CodeIgniter? It's for a new project or just maintaining legacy?

the query u gave me

print $this->db->last_query();
die;

resulted in displaying the SELECT query coz thats the query being used for the page? Anyway, I ended up just redirecting the page to the main menu instead :

if ($query->num_rows() > 0) {
          return $query->result();
        }
        else{
            $this->load->helper('url');
            redirect(base_url('myguestbook'));
        }

for ur question, its a lab exercise and following my lecturers instructions we had to use codeigniter. just to introduce us to it i suppose? thank you anyway cereal!

Whoops, now I see , sorry I was distracted by the null values. The redirect does not solve the problem, it just overrides. The warning happens because in your view the loop starts anyway and you are suppling a string when instead it expects an array or an object:

foreach ($result as $record):

Instead of $query->result() from the model return the object:

function read($where, $length, $start) {
    $this->db->limit($length, $start);
    return $this->db->get_where('myguestbook', $where);
}

this way you can cache it, use the custom result object and, in your controller, use the attached methods like num_rows():

$li = [];
$int = 0;
$template = '<li>user: %s<br>email: %s<hr></li>';

if(0 < $result->num_rows())
{
    foreach ($result as $record)
    {
        $li[$int++] = sprintf($template, $record->user, $record->email);
    }
}

$data['result'] = $li;
$data['result_count'] = $result->num_rows();

And in the view just run:

if(0 < $result_count)
{
    echo implode(PHP_EOL, $result);
}

else
{
    echo '<p>No record exists</p>';
}

Thanks to you for your reply, bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.