How to use MySQL FIND_IN_SET() Function in Codeigniter

Hello Guys,

Today, I am going to share a very small but useful tutorial with you about how to use MySQL FIND_IN_SET() function in codeigniter. Let's say, you are creating a product listing page and the tag ids  associated with the product are stored in a comma separated column and you want to fetch all records of tag id 5, in this case we use MySQL FIND_IN_SET() function.

Now the question is how to use FIND_IN_SET() function in Codeigniter?

Without wasting your time lets quickly walk through the code:
$this>db->select('name')->from('product')->where("FIND_IN_SET('5',post_tags) > 0")->get()->result();

How to encode URL in JavaScript?

If you are looking to encode an URL that have an query string (like '?name=akhilesh shukla') on one of your page and you don't want your visitor's to see that or in some case like you want to redirect your visitor to a link where the link accept parameters in encoded format then you must have to encode the parameter before redirecting user to that particular link.

Encode URL using encodeURIComponent() Method:
var param = 'Akhilesh Shukla #fhfd 547';
var linkURL = 
       "" + encodeURIComponent(param);

In above example, I have encoded the param variable. Though, encoding URL can also be done using encodeURI() method. However, encodeURI() does not encode special characters like #,/,? etc.

Recommended article: How to remove query string parameter using jQuery

Block bad IP addresses from accessing your website with .htaccess

If you want to block a bad IP address from accessing your website, and you have the IP address, host name or you know ISP of the visitor then you can easily block the visitor accessing your website with .htaaccess file. You can even secure a part of the website from accessing by placing a .htaccess file in the folder and the code into it.

A small tips: If you have WordPress website in which you want placing your code to block users then i would recommend that always place code after or before # BEGIN WordPress  and # END WordPress lines.

htaccess code to ban an IP

To ban a single IP place the below code into your .htaccess file and upload it to the root folder on your server.
order allow,deny
deny from
allow from all

In above example, I have blocked this IP to accessing my website and now this IP cannot visit any part of my website. However, in future if I want to give this IP acccess again, then I can remove the line from my htaccess and the IP can access the website again. The syntax to block a visitor is deny from the IP/ISP.

How to ban multiple IP addresses?

If you want to ban multiple IPs from accessing your website then also the syntax is same and you will need to add them in new line as below:
order allow,deny
deny from
deny from
deny from
allow from all

Ban a range of IPs

If you want to ban a full range of  IPs for example to, you can easily ban as below:
order allow,deny
deny from 106.223.197.
allow from all

Ban an ISP in htaccess

Blocking an ISP is same as blocking an IP, you will need to replace the IP with the ISP. See example:

order allow,deny
deny from
allow from all

Add custom class to WordPress menu using wp_nav_menu function

WordPress has a very good functionality of creating menu by dragging and dropping to required places and creating child or sub child menu in same way, that helps a layman to create menu very easily within a minute or two. However; if you are a developer and you want to go deeper in the functionality such as if you want to add a class to the parent UL or you want to add an ID to the parent UL tag then you will need to edit the source code.

Generally, we call wp_nav_menu() as following:
<?php wp_nav_menu( array('theme_location' => 'primary','menu_id' => 'mainHdrMenuId','menu_class' => 'hdrnavLst pull-right', )); ?>

In Above example, I have used 'primary' navigation in theme_location parameter that i already have created a menu in WordPress admin and also assigned 'mainHdrMenuId' as menu id and added two class in the menu_class parameter However; the classes and ids will be applied to the parent div section and not in UL tag.

If you want to add the class in UL section then you must have to define ul as a container in wp_nav_menu() function and then the defined id and classes will applied only to ul tag not to the parent div that WordPress by default add. See the example below:
<?php wp_nav_menu( array('theme_location' => 'primary','container'=> 'ul', 'menu_id' => 'mainHdrMenuId','menu_class' => 'hdrnavLst pull-right', )); ?>

I Hope this trick will help you. Please share your feedback in the comment box below.

How to get last insert ID in Codeigniter

I assume, you must be creating a website where you are looking to get last insert id of executed INSERT query in parent table and then you want to store the last inserted ID in your child table as a foreign key.

In this post, i am going to share with you guys a small Codeigniter method that will help you to get last inserted id.

Here it is:

Run this peace of code after insert query and this method will return you last inserted id of the executed query.

I hope this post will help.

How to get file name from a URL using PHP

Are you creating an application in which you require a functionality that will copy the file from a given URL and then store into a directory and while coping the file you want to get filename of from given URL and then you want rename that according to your requirement. Am I right? Here, I an going to write a code that will help us to get file name from a URL using PHP inbuilt function basename().

Here is the code:
echo basename("");

Above code will output the file name as 'image.png'.

How to download image from a URL using PHP

Are you creating an application where a candidate can register his profile and can upload his profile picture or you are creating an application where your system will download the file from given URL? if yes then you are at right place because here in this article i am going to writing a code that will copy the file from given URL and will store in a specified directory. So without wasting time, lets do it.

In this example, i am going use PHP inbuilt function copy() to copy the file from URL.
copy('', 'local/folder/local.jpg');

In above code, I have used two path first one for server path (a file URL which you want be copied) and second one is your local server directory path in which you want the file to be stored.

I hope this article will help you a lot.

How to remove query string parameter using jQuery

In this article we will talk about How to remove query string parameters using jQuery. First of all, we will understand why we use query string in a URL. We use query string in a URL to get required parameter value to execute in the current loaded page or to track the source of direction. However; URLs with lots of query strings looks ugly, a normal visitor cannot memorize that and now days search engines are also not preferring this technique also is more secure.

So, here is the code that will help you in removing this dirty query strings from your page URL:
<script type="text/javascript">
    if (window.location.href.indexOf('?') > -1) {
        history.pushState('', document.title, window.location.pathname);

Please note that, above code will remove the query strings from the page URL and the page will not be reloaded and some functionalities running on the page can be stopped. I recommend you before using this code, please check this code is not affecting running functionalities on your page/module etc.

Please feel free to share your feedback in the comment box below or if you have any question, you can ask too. Best regards, Akhilesh