Codeigniter Select the table alias in the where clause

CodeIgniter defines models, views and controllers

CodeIgniter is a powerful PHP framework that can significantly speed up web application development. It's small in size and performing very well due to the modular approach to loading its libraries, and it does a great job of separating the logic of the presentation by using a dynamic model-view controller (MVC).

In this case, we will use a simple example to see how the code works in a clearer and more orderly manner:

Assume the following table employees a database

CREATE TABLE WHEN NO EMPLOYEES ARE AVAILABLE (`id` int (11) NOT NULL AUTO_INCREMENT,` name` varchar (50) NOT NULL, `salary` int (11) NOT NULL, PRIMARY KEY (` id`)) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 5;

The model

When we create the data model, this will be the function that will query data in the database

<? php class Employees_model erweitert CI_Model { Funktion __construct () { $ this-> load-> database (); } Funktion receive_employees () { $ query = $ this-> db-> get ('employee'); return $ query-> result_array (); } } ?>

In the __construct function, we load the database so that we can access it with $ this-> db to query data, as we do in the receive_employees function. To restore all entries in the Employees table, we execute the get () function, which receives as a parameter the table from which the records are to be retrieved. Therefore this call is translated in the SQL query SELECT * FROM 'employee'. The get () function is available thanks to CodeIgniter's Active Record class.

So that we have our model linked to the database, the next thing will be to create the controller to process this data. To do this, we create the workers.php file in controllers and define the list_employees function.

Function list_employees () {$ this-> load-> model ('employee_model'); // I get the list and keep it in a matrix $ Employee list = $ this-> employee_model-> receive_employees (); // I map the matrix to a data matrix that will be sent to the view $ data ['employee'] = $ listingEmployees; $ this-> load-> view (employee list ', $ data); }

We load the model we created in the first line using the load-> model function and give the name of the model we need, then call the function we created and get an array with the Information from the database. Copy the received matrix that we send to the Vista in the $ data mark. At sight we just send a matrix with different data that we will display later, for example:

$ data ['employee'] = $ listingEmployees; $ data ['ttitulo_pagina'] = 'Employee';

Finally, we prepare the view to properly display the information:

<html> <body> <h1> <? Geben Sie $ titulo_pagina?> </ H1> zurück <table> <tr> <th> Name </ th> <th> Jahresgehalt </ th> </ tr> <? php foreach ($ employee as $ employee):?> <tr> <td> <? php echo $ used ['name']; ?> </ td> <td> <? php echo $ employee ['Gehalt']; ?> </ td> </ tr> <? php endforeach; ?> </ table> </ body> </ html>

To go to the page to view it:

www.dominio.com/controlador/funcion

In this case we should call it like this:

www.mydomain.com/employees/listar_employees

: By default, Codeigniter uses the URL:

www.mydomain.com/index.php/controller/function

To remove the index.php from the URL, we apply the following code in the .htaccess- File

<IfModule mod_rewrite.c> RewriteEngine ein RewriteCond% {REQUEST_FILENAME}! -F RewriteCond% {REQUEST_FILENAME}! -D RewriteRule. * Index.php / $ 0 [PT, L] </ IfModule>