DEV Community

Gurpinder Singh for CodeIgniter

Posted on

How can display data from a table (patient_category) in the database using php-mysql and codeIgniter?

To display data from a table (e.g., patient_category) in a MySQL database using CodeIgniter and PHP, you'll need to follow these steps:

Configure CodeIgniter Database Settings: Make sure your CodeIgniter application is properly configured to connect to your MySQL database. Set the database connection parameters in the config/database.php file.

Create a Model: Create a model to interact with the database. This model will handle querying the patient_category table.

// application/models/Patient_category_model.php

class Patient_category_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function getPatientCategories() {
        $query = $this->db->get('patient_category');
        return $query->result();
    }
}

Enter fullscreen mode Exit fullscreen mode

Create a Controller: Build a controller that will load the model and fetch data from the patient_category table.

// application/controllers/PatientCategoryController.php

class PatientCategoryController extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('Patient_category_model');
    }

    public function displayPatientCategories() {
        $data['patient_categories'] = $this->Patient_category_model->getPatientCategories();
        $this->load->view('patient_categories_view', $data);
    }
}

Enter fullscreen mode Exit fullscreen mode

Create a View: Create a view file to display the fetched data (patient_categories_view.php).

<!-- application/views/patient_categories_view.php -->

<!DOCTYPE html>
<html>
<head>
    <title>Patient Categories</title>
</head>
<body>
    <h1>Patient Categories</h1>
    <table border="1">
        <tr>
            <th>Category ID</th>
            <th>Category Name</th>
            <!-- Add other table headers as needed -->
        </tr>
        <?php foreach ($patient_categories as $category) { ?>
            <tr>
                <td><?php echo $category->category_id; ?></td>
                <td><?php echo $category->category_name; ?></td>
                <!-- Display other table data fields as needed -->
            </tr>
        <?php } ?>
    </table>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Access the Controller Method: Finally, access the displayPatientCategories() method of the PatientCategoryController from your browser.

For example, if your CodeIgniter application is running locally:

http://localhost/yourapp/index.php/PatientCategoryController/displayPatientCategories
Enter fullscreen mode Exit fullscreen mode

Ensure that you've replaced yourapp with the actual name of your CodeIgniter application directory.

This basic setup will fetch data from the patient_category table and display it in a tabular format using a view file in CodeIgniter. Adjust the view file and database query methods according to your table structure and requirements.

I hope this information will be useful for you. If you have any questions please share with me.

Thanks for reading,
Dgi Host.com

Top comments (0)