Message: Undefine variable: exam

Course Queries Competitions/Entrance Exams 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Competitions/Entrance Exams related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

controller: Test.php

php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller 
{
    function __construct() 
    {
        parent :: __construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model('Fetch_data');
    }
    public function agriculture_exam()
    {
        $stream = 'agriculture';
        $data['exam'] = $this->Fetch_data->top_agriculture_exams($stream);
        $this->load->view('header',$data);
    }
}

view: header.php

 class="list">
php 
    foreach($exam as $row) 
    {
       echo "
  • ".$row['exam_name']."
  • "
    ; } ?>

    model: Fetch_data.php

    php  
        class Fetch_data extends CI_Model  
        {  
            function __construct()  
            {   
                parent::__construct();  
            }  
            public function top_agriculture_exams($stream)
            {
                $this->db->select('exam_name');
                $this->db->from('all_exams_details');
                $this->db->where('field',$stream);
                $this->db->order_by('exam_name');
                $this->db->limit('10');
                $query = $this->db->get(); 
                $result = $query->result_array();
                return $result;
            }
        }

    I am new in ci. In controller i.e. Test.php I am defining $stream='agriculture' and pass $stream variable to top_agriculture_exams model. Now, when I am fetching data on header.php file it show me a Message: Undefine variable: $exam I don't know why. So, how can I fix it ?please help me.

    Thank You

    profilepic.png
    manpreet 2 years ago

     

    Please check my below code. This will help you.

     class="list">
        php
        if ($exam !== NULL):
            foreach ($exam as $row):
                echo "
  • " . $row['exam_name'] . "
  • "
    ; endforeach; endif; ?>

    Also cna you please put this code in View and share result.

    php
    var_dump($exam);
    ?>

    Also check your model code below.

    php  
        class Fetch_data extends CI_Model  
        {  
            function __construct()  
            {   
                parent::__construct();  
            }  
            public function top_agriculture_exams($stream)
            {
                $this->db->select('exam_name');
                $this->db->from('all_exams_details');
                $this->db->where('exam_name',$stream);
                $this->db->order_by('exam_name');
                $this->db->limit('10');
                $query = $this->db->get(); 
                $result = $query->result_array();
                return $result;
            }
        }

    0 views   0 shares

    No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.