Fetching information from the database MYSQL & Codeigniter

后端 未结 2 2025
Happy的楠姐
Happy的楠姐 2021-01-26 11:55

the previous question is here incase you guys needed a other information:

Fetching information from the database

ANOTHER UPDATE

Although

2条回答
  •  不要未来只要你来
    2021-01-26 12:36

    There are a few things that could be clarified:

    • A model is a entity that persists in the database. In your case: Membership
    • A controller is in charge of load the model, and pass it to the view to be showed
    • A view only should have html markup, no functions, no logic.

    Database

    Assuming that your database have the structure below.

    +---------------+-----------+------------+--------------------+
    | id_membership | username  | name       | email              |
    +---------------+-----------+------------+--------------------+
    |             1 | Marishka  | marishkapv | marishka@email.com |
    |             2 | johndoe   | John       | john@doe.com       |
    |             3 | dennisv   | Dennis     | dennis@v.com       |
    +---------------+-----------+------------+--------------------+
    

    Model

    You could build your model class Membership extends CI_Model at /application/models/membership.php file.

    class Membership extends CI_Model {
    
        function __construct()
        {
            parent::__construct();
        }
    
        function member_here()
        {
            $this->db->select('');
            $this->db->from('membership');
            $this->db->where('username',$this->input->post('username'));
            $q = $this->db->get('');
    
            if($q->num_rows() > 0) 
            {
                $data = array();
                foreach($q->result() as $row) 
                {
                    $data=$row;
                }
                return $data;
            }
        }
    }
    

    Controller

    Supposing that your have a login page at example.com/index.php/login, this should resides at /application/controllers/login.php as below:

    class Login extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
        }
    
        public function index()
        {
            $this->load->view('login_view');
        }
    
        public function result()
        {
            $this->load->model('membership');
            $data['member'] = $this->membership->member_here(); 
            $this->load->view('result_view', $data);
        }
    }
    

    Look like the models is loaded throws this two lines:

    //Load the model in make use of its functions
    $this->load->model('membership'); 
    
    //load the row whit the given username
    //the result is assigned to array with the key "member", in the view you access this
    //value with $member var
    $data['member'] = $this->membership->member_here(); 
    

    Views

    As you noted in controller code, there are two views files, login_view and result_view. The first one will show a html form where you can put the username in order to trigger a select query. The result_view show the informacion of the member according to the selected membership

    login_view.php

    
        
        
        
    Type your username:

    result_view.php

    Details:

    Id: id_membership?>
    Username: username?>
    Email: email?>

提交回复
热议问题