0

I'm new in codeigniter, I'm having a challenge with num_rows on view page Here's the sample of the code - Updated Model

 public function __construct()
{
    parent::__construct();

}

public function total_referred_in()
{


    $query = $this->db->query('SELECT * FROM daily_out_patient');

    return $query->num_rows();

}

Controller

 public function __construct()
{
    parent::__construct();
    $this->load->helper('url');

    $this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}


public function index()
{
    $this->load->helper('url');
    $this->load->view('referrals_form_view');
}

public function referral_in()
{
    $data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
    $this->load->view("referral_form_view", $data);
}

}

View

<span><?php
    echo $query->num_rows();

?></span>

when i run the code, it tell me undefined variable "query"

Please help.

Thank you

1
  • Relevant reading: How to Show counted data to Html using php codeigniter In your case, you shouldn't be using num_rows() because you don't need any of the data, you only need the count. public function total_referred_in() { return $this->db->count_all('daily_out_patient'); } Commented Oct 3 at 4:05

2 Answers 2

1

Please change your code as below.

MODEL:

 public function total_referred_in()
 {
   $query = $this->db->query('SELECT * FROM daily_out_patient');
   return $query->num_rows();
 }

Controller

public function referral_in()
{
   $data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
   $this->load->view("referral_form_view", $data);
}

View :

<span>
   <?php
   echo $total_referred_in;
   ?>
</span>

As you can see in Controller line :

$this->load->view("referral_form_view", $data);

$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks, but displays "Message: Undefined variable: total_referred_in"
How can i define the variable in my view page?
Where is your view file referral_form_view.php placed in codeigniter?
For you to print $total_referred_in in your view using above code, your view template file have to be placed in "application/views/referral_form_view.php"
Thanks, referral_form_view is placed in "application/views/" folder, but im still getting the same error
|
0

You aren't returning anything from your function, you're just echoing. Try this:

public function total_referred_in()
{
    $query = $this->db->query('SELECT * FROM daily_out_patient');

    return $query->num_rows();
}

Controller:

public function referral_in()
{

    $data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
    $this->load->view("referral_form_view", $data);
}

And then in your view:

<span><?=$num_rows?></span>

2 Comments

I just tried that, but an error message display "undefined variable : query"
Not familiar with this framework, but seems like anything you pass in to the view() function should be available. Try with my updates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.