In code igniter, you can connect database and make queries in controller like:
$this->db->query(Your query);
$this->db->get();
How can I connect database from file in view folder?Is that possible? Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can, but you shouldn’t.
Even in a view file,
$thisstill refers to the current controller so$this->db->get('my_table')will work exactly the same as it would in the controller.However, as surely you must have heard, it’s recommended to pass variables to the view from the controller instead of preforming non view-related tasks in the view file itself, and doing database manipulation (getting data, updating, deleting, etc.) in the model layer. This is one of the fundaments of MVC architecture.