0

I am working on a small application that displays data from a database table. I am getting 2 errors and dont know why are there, and I cant figure it out as I am still a noob. I guess its something stupid, please help.

ERRORS

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: querys


Severity: Warning

Message: Invalid argument supplied for foreach()

these errors are located in my view page.

VIEW

    <?php foreach ($queries as $row): ?>
    <table>

       <th>Name</th>
       <th>Surname</th>
       <th>phone</th>
       <th>email</th>

       <td><?php echo $row->name; ?></td>
       <td><?php echo $row->surname; ?></td>
       <td><?php echo $row->phone; ?></td>
       <td><?php echo $row->email; ?></td>

   </table>  
    <?php endforeach; ?>

CONTROLLER

  function display($offset = 0)
    {
           $limit = 20;
            $this->load->model('info_model');

            $results = $this->info_model->search($limit, $offset);

            $data['queries'] = $results['rows'];
            $data['num_results'] = $results['num_rows'];


            $this->load->view('info_view',$data);


    }

MODEL

 function search ($limit, $offset){

          //results query 
          $q = $this->db->select('ID, name, surname, phone, email');
          $this->db->from('tblinfo');
          $this->db->limit($limit, $offset);

          $ret['rows'] = $q->get()->result();
          //count query
          $q = $this->db->select('COUNT(*) as count', FALSE )
          ->from('tblinfo');

          $tmp = $q->get()->result();

          $ret['num_rows'] = $tmp[0]->count;
          return $ret;
      }

EDIT

i fixed the foreach error by inserting

   <?php if(is_array($queries)): ?>
   <?php endif; ?>

the only error i am getting is the

   A PHP Error was encountered

    Severity: Notice

    Message: Undefined variable: queries
user2143150
  • 71
  • 1
  • 8

5 Answers5

5
$data['query'] = $results['rows'];

should be

$data['querys'] = $results['rows']; // although correct spelling is "queries"

Then you can access $querys inside your view.

Mischa
  • 42,876
  • 8
  • 99
  • 111
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
1

The problem seems to be with your models search() method, which is probably not returning any results. I would re-write it as follows to utilize codeigniters active record class:

function search ($limit, $offset){

    //results query 
    $this->db->select('ID, name, surname, phone, email');
    $q =  $this->db->get('tblinfo', $limit, $offset);

    $ret['rows'] = $q->result();

    //count query
    $ret['num_rows'] = $this->db->count_all('tblinfo');

    return $ret;
}

You can use Codeigniters active record class to get a count of all records in a specific table using just $this->db->count_all('table_name');

Jeemusu
  • 10,415
  • 3
  • 42
  • 64
0

Maybe change your search code a little

$q = $this->db->select('ID, name, surname, phone, email')
              ->from('tblinfo')
              ->limit($limit, $offset);
Robert Lee
  • 1,541
  • 1
  • 10
  • 20
0

Personally I think a function should perform only one task. Right now you are trying to do two tasks (return records and return a record count) in one function.

I would rewrite my model like this:

function search($limit, $offset)
{
  $this->db->select('ID, name, surname, phone, email');
  $this->db->from('tblinfo');
  $this->db->limit($limit, $offset);
  return $this->db->get();
}

And your controller could look like this:

function display($offset = 0)
{
  $this->load->model('info_model');
  $results = $this->info_model->search(20, $offset);

  $data['queries'] = $results->result();
  $data['num_results'] = $this->db->count_all('tblinfo'); // Alternatively you could make a separate model function that does this

  $this->load->view('info_view', $data);
}
Mischa
  • 42,876
  • 8
  • 99
  • 111
  • tnx fore answering but its the same error when i tried Undefined variable: queries – user2143150 Mar 13 '13 at 08:27
  • @user2143150, could you try again with exactly the code above? This *should* work. – Mischa Mar 13 '13 at 08:30
  • its the same error tnx for the effort – user2143150 Mar 13 '13 at 08:33
  • @user2143150, sorry, I spotted one more mistake... I forgot `return` in the model. Please try again. – Mischa Mar 13 '13 at 08:34
  • same error again is there a simpler way maybe to retrieve records from table instead of my version of the code ? – user2143150 Mar 13 '13 at 08:38
  • @user2143150, what do you mean? I don't understand your question. – Mischa Mar 13 '13 at 08:40
  • all i am trying is to display the data that is on a table in a database. is there a simpler way of doing this instead of the code that i have ? – user2143150 Mar 13 '13 at 08:44
  • @user2143150, this is pretty basic already and it *should* work. Are you sure your `display` function gets called? Could you add the following to the top of your `display` function? `echo 'this is display'; exit;` and tell me what happens? – Mischa Mar 13 '13 at 08:47
  • @user2143150 - Glad to hear it! Just wondering... what was the problem? – Mischa Mar 13 '13 at 09:49
  • 1
    like u said the display function was not getting called that's why the unused variable there so i changed my code little bit and it worked. tnx for your help bdw – user2143150 Mar 13 '13 at 11:09
0

Change your model code to this

function search ($limit, $offset){

      //results query 
      $this->db->select('ID, name, surname, phone, email');
      $this->db->from('tblinfo');
      $this->db->limit($limit, $offset);
      $q = $this->db->get();
      $ret['rows'] = $q->result();
      //count query
      $q1 = $this->db->select('COUNT(*) as count', FALSE )
      ->from('tblinfo');

      $tmp = $q1->get()->result();

      $ret['num_rows'] = $tmp[0]->count;
      return $ret;
  }
Azam Alvi
  • 6,918
  • 8
  • 62
  • 89