Category Archives: Codeigniter
Codeigniter Search Comma-separated values in MySql
Codeigniter Search Comma-separated values in MySql-There are multiple ways to search Comma-separated values in MySql. We sometimes need to filter some data using comma separated values. Here in this article we are going to explain how you can add filter on column using Active records in Codeigniter for comma-separated values.
Codeigniter Search Comma-separated values in MySql | Find In Set
We can use find in set(find_in_set) in Active records to search comma separated values. Here is simple Example to search value 1,2,5,6 in mysql codeignater–
$searchValues = [1,2,5,6]; $this->db->select('*')->from('users'); $this->db->group_start(); foreach($searchValues as $value) { $this->db->where("find_in_set($value, user_id)"); } $this->db->group_end(); $result = $this->db->get()->result_array();
The above example will give you list of users where user id is – 1,2,5 and 6.
Codeigniter Inner Join With Multiple Tables
We can use following syntax to make inner join with multiple tables.
Codeigniter Inner Join With Multiple Tables
Using codeigniter standard query manager ($this->db->join) we can join with two or more than two tables. Here is syntax for Joins with Multiple Tables.
$this->db->select('*'); $this->db->from('users'); $this->db->join('profile_image', 'profile_image.user_id = users.id'); $this->db->join('city', 'city.user_id = users.id','left'); $this->db->join('post', 'post.user_id = users.id','left'); $this->db->join('friends', 'friends.user_id = users.id','left'); $this->db->where('users.id', $id); $query = $this->db->get();
If you run the above example it will produce the following output.
// Result
Select *from users
join profile_image on profile_image.user_id = users.id
left join city on city.user_id = users.id
left join post on post.user_id = users.id
left join friends on friends.user_id = users.id
where users.id = $id
Codeigniter Redirect HTTP Urls to HTTPS
Codeigniter Redirect HTTP Urls to HTTPS– There are many to redirect from http to https. Here in this tutorial, we are going to tell you the optimized way to redirect the http urls to https urls.
Codiegniter Redirect HTTP Urls to HTTPS
Add the below code to index.php, After adding the below code all http urls will be redirected to https.
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; if (strpos($url, 'http://') !== false) { $finalUrl = str_replace("http://","https://", $url); header("HTTP/1.1 301 Moved Permanently"); header("Location: $finalUrl"); exit(); }
Codeigniter get single row
Codeigniter get single row– We can get single row from codeigniter very easily.
Codeigniter get single row Example
You can get the single row from query result in codeigniter simply as below –
Example:
$query = $this->db->query("YOUR QUERY"); $result = $query->result_array(); $singleRow = $result[0]; |
The above example will return the single row data form the returned result.
Codeigniter get Controller Name from URL
Get Controller Name from URL We can use router to get the current controller name in codiegniter. Here in this article, we are going to explain how you can use $router to get the current controller and method name.
Codeigniter get Controller Name from URL Example
You can get the controller name and method name of current URL simply as below –
Example:
$this->router->class; // will give controller class $this->router->method; // will return method name |
For older version of codeigniter use the following syntax-
$this->router->fetch_class(); $this->router->fetch_method();
Codeigniter get url parameters
Codeigniter get url parameters – You can get url parameters using url helper input get method. You can get the query string value from url using the below method.
Codeigniter get url parameters Example
You can get the url parameters simply as below-
Suppose we have following url and we want to get the url params-
Example:
www.example.com/my_controller/delete/99 |
You can get the controller method delete param value simply as below-
Codeigniter delete multiple rows
Codeigniter delete multiple rows– You can use where condition to delete the multiple rows from the table.
Codeigniter delete multiple rows Query Example
Use where_in and pass array of values that you want to delete. Here is simple example-
Example:
$ids = array(1,8,7,5); $this->db->where_in('id', $ids); $this->db->delete('my_table'); |
Where $ids is array, my_table is table name from which you want to delete the data. The above example will delete the where id is 1,8, 7 and 5.
Codeigniter form_open
Codeigniter form_open – To create forms in Codeigniter form helper and form validation library is avalable which can be used easily. Here in this article we are going to explain how you can create forms and add validation in codeigniter using its helper & library.
Codeigniter form_open | Form | Helper | Library Example
, First of all, you need to load form helper and form validation library in controller function simply following the below steps-
1. Controller Part
In controller load helper & library.
Codeigniter form_open Example :
load->helper('form'); $this->load->library('form_validation'); $this->load->view('login_view'); } } |
After loading helper & library now you are able to create form fields & validations-
View Part | login_view.php
Form is created using form_open & form_close-
Codeigniter form_open Example :
|
Codeigniter form_open Not Working ? | Fatal Error
Make sure you have loaded form helper & library to avoid the errors.
Distinct in Codeigniter Query
Distinct in Codeigniter Query You can use $this->db->distinct() to add the distinct in codiegniter query. Here in this tutorial we are going to create an example for distinct Query.
Distinct in Codeigniter Query Example
Here is an example of distinct query in codeigniter-
Distinct in Codeigniter Query Example:
$where_array = array( 'email'=>'test@gmail.com', 'status'=>'1' ); $table_name = "users"; $limit = 10; $offset = 0; $this->db->distinct(); $query = $this->db->get_where($table_name,$where_array, $limit, $offset); |
On the same you can add distinct in codeigniter in any query.