0

I am trying to get two values 'total number of movies' and 'details of movies' from database. For total number of movies, i am returning with php error "undefined_index:num_films". please help me.

Sakila_control

<?php

class Sakila_control extends CI_Controller
{
    function display($offset=0)
    {
        $limit = 20;
        $this->load->model('films_model');
        $result = $this->films_model->search($limit,$offset);
        $data['films']=$result['rows'];
        $data['num_results']=$result['num_films'];



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

    }
    }

Films_model

<?php

class Films_model extends CI_Model
{
    function search($limit,$offset)
    {
       $qu = $this->
               db->select('FID,title,description,category,price,length,rating,actors')
                ->from('film_list')
                ->limit($limit,$offset);
       //rows comes from controller  
        $ret['rows']= $qu->get()->result();
        return $ret;


        $q = $this->db->select(`COUNT(*) as count`, FALSE)
                ->from('film_list');
        //num_rows comes from controller  
        $tmp= $q->get()->result();

       $ret['num_films']=$tmp[0]->count;

       return $ret;
    }
}

Films_view

<body>

        <table>
            <div>
                Found <?php echo $num_results;?> Films

            </div>
            <thead>
            <th>ID</th>
            <th>Title</th>
            <th>description</th>
            <th>category</th>
            <th>price</th>
            <th>length</th>


            </thead>
            <tbody>


               <?php 
               foreach($films as $film):?>
            <tr>
                <td><?php echo $film->FID;?> </td>
                <td><?php echo $film->title;?></td>
                <!-- <td><?php echo $film->description;?></td> -->
                <td><?php echo $film->category;?></td>
                <td><?php echo $film->price;?></td>
                <td><?php echo $film->length;?></td>
                <td><?php echo $film->rating;?></td>
               <!-- <td><?php echo $film->actors;?></td> -->
            </tr>

            <?php endforeach;?>
            </tbody>
        </table>

    </body>
</html>
Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

0

According to your code you are returning the value just after the first query. So your function is not returning the num_films.

class Films_model extends CI_Model
{
    function search($limit,$offset)
    {
       $qu = $this->
               db->select('FID,title,description,category,price,length,rating,actors')
                ->from('film_list')
                ->limit($limit,$offset);
       //rows comes from controller  
        $ret['rows']= $qu->get()->result();
       // return $ret;


        $q = $this->db->select('COUNT(*) as count`, FALSE)
                ->from('film_list');
        //num_rows comes from controller  
        $tmp= $q->get()->result();

       $ret['num_films']=$tmp[0]->count;

       return $ret;
    }
}

Check multiple return in same function

Community
  • 1
  • 1
urfusion
  • 5,528
  • 5
  • 50
  • 87
0

First you will need to comment out this:

 // return $ret;

Then, you need to fix another error with backticks that you didn't get yet because of the return statement...

Backticks in PHP will attempt to run code from the command line.

Change this:

$q = $this->db->select(`COUNT(*) as count`, FALSE)

To this:

$q = $this->db->select('COUNT(*) as count', FALSE)

Final result:

<?php

class Films_model extends CI_Model
{
    function search($limit,$offset)
    {
       $qu = $this->
               db->select('FID,title,description,category,price,length,rating,actors')
                ->from('film_list')
                ->limit($limit,$offset);
       //rows comes from controller  
        $ret['rows']= $qu->get()->result();

        $q = $this->db->select('COUNT(*) as count', FALSE)
                ->from('film_list');
        //num_rows comes from controller  
        $tmp= $q->get()->result();

       $ret['num_films']=$tmp[0]->count;

       return $ret;
    }
}

Read more on backticks here: http://php.net/manual/en/language.operators.execution.php

Execution Operators

PHP supports one execution operator: backticks (``). Note that these are not single-quotes! PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned (i.e., it won't simply be dumped to output; it can be assigned to a variable). Use of the backtick operator is identical to shell_exec().

Community
  • 1
  • 1
Clay
  • 4,700
  • 3
  • 33
  • 49
  • when i put the code q = $this->db->select('COUNT(*) as count', FALSE) ->from('film_list'); it gave error Message: Cannot access empty property Filename: models/films_model.php Line Number: 15 – zeeshan haider Jan 12 '16 at 08:41
0

What about this

In Model

class Films_model extends CI_Model
{
    function search($limit,$offset)
    {
        # Just pass all data to controller
        $query = $this->db->query("SELECT * FROM film_list LIMIT $limit, $offset");
        $result = $query->result_array(); # Changed
        return $result;
    }
}

In Controller

class Sakila_control extends CI_Controller
{
    function display()
    {
        $limit = 20;
        $offset=0

        $this->load->model('films_model');
        $data['films'] = $this->films_model->search($limit,$offset);

        $count = count($data['films']); # get count on here 
        $data['num_results'] = $count;
        $this->load->view('films_view',$data);
    }
}

In View

All Fine and its up to you

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85