preg_grep – find keys in an array that match a pattern

Sometimes there is a need to find a key/s of an array that matches a particular pattern. array_search searches the array for a given value and returns the corresponding key but it matches the entire word. Here's a scenario. You have a list of tags and you want to provide a user to seach a particular tag which closely matches to their search. So here are some of the tags in an array [crayon-59c674b142a4f066668304/] If the user enters Wordpress we want to show Wordpress Plugins as well as Wordpress Tweaks. So this is how it can be done using preg_grep [crayon-59c674b142a5a301168854/] where $key is the user input i.e. Wordpress in this case In our case $filtered_tags will be an array and will return 0=>Wordpress Plugins 1=>Wordpress Tweaks  ...
Read More

How to correctly get visitor/client IP address

For security reasons and to prevent spam it is always recommended to log visitor/client IP address in your database atleast for important transactions like posting a comment, registration, etc. However it is important that we store the real IP of the visitor. Visitor/Client is not using proxy In almost all cases i.e. when the visitor/client is not behind the proxy we can get the real IP address of the visitor/client using [crayon-59c674b14440b584062441/] Visitor/Client is behind a proxy server In some cases the visitor/client could be behind a proxy server. In that case we can get the real IP using [crayon-59c674b144415268500809/] However using some tools one can easily pretend to be behind a proxy server. In that case we cannot get the real IP using above method Also we cannot be sure if the visitor/client is using a proxy or not. So it is best to store both the values in different fields in your database....
Read More

How to automatically alert your users about events happening in their area

This can be easily done if you are already geocoding users addresses (i.e. getting their latitude and longitude) while they register on your website. If you are not doing so then you can go back and geocode all the previous addresses. Now lets assume you have geocoded all the addresses and are storing them in a separate table as shown below Table: user_lat_long [crayon-59c674b146435960308397/] Now create a function to get users from the above table near a certain address (where the event is to be held). [crayon-59c674b146440433320689/] Now just call the above function with 3 parameters as input Radius => Defines the area you want to set to alert your users e.g. 100 KM Latitude => Latitude of the place where the event is to be held Longitude => Longitude of the place where the event is to be held [crayon-59c674b146445799001438/]  ...
Read More

Storing files and images in mysql database

Biggest advantage of this approach is, you are not required to maintain a seperate directory for documents or images and this proves to be really handy in case you move directories or change servers. You need not worry about the links from the database. Also you need not worry about any complex version control systems to store documents with a same name. 1. Create a table 1. id - INT (6) 2. filename - VARCHAR (100) 3. filetype - VARCHAR (50) 4. filesize - VARCHAR (20) 5. file - BLOB or MEDIUM BLOB 2. File upload (uploadfile.php) [crayon-59c674b146d45152137103/] 3. Process the above form (procesform.php) [crayon-59c674b146d52270299246/] 4. Get file (get-file.php) [crayon-59c674b146d5b604683970/] 5. Download a file Now to download a file just link the file to get-file.php with an id e.g. [crayon-59c674b146d6a777248049/]  ...
Read More

Cloning a database record in MySQL

I thought that cloning a database record should be easy until I decided to write a query for it. I then realized that there is no simple query which clones a record. The steps though are pretty simple: 1. Select the record you want to clone 2. Insert the values in the table Example Lets consider you have an events management database where the primary key is the eventid and you would like to clone the events frequently First of all, here is the query you would use to select the event you want to clone [crayon-59c674b147676312177074/] Now let's clone the record [crayon-59c674b147682995729794/] Thus, we have combined INSERT and SELECT query in one query. But there is a problem in this query. When you try running this query, you might get a "duplicate key" error. That's because the record clones everything including the primary key which is the eventid. To avoid that, we need to change our steps as follows 1. Create a temporary table 2. Select the record to be cloned and store it...
Read More

PHP file_get_contents usage

Below is a standard command in PHP to get contents of a file in a string variable. file_get_contents($url) Lot of people might wonder why would you get contents of a file in a string if you can get the contents using a simple Include statement. I would like to add that it is not just about getting the content but also converting the content or processing it as per your requirements. file_get_contents is a powerful tool and you can do a lot of things with it. I have only listed a few of them which I have tried using in my project. Evaluating PHP code within the file I have a CMS environment which does not execute a PHP code. I used the CMS to store my PHP code and used file_get_contents() to get the file contents in a string variable and then execute the file as PHP using the below method. [crayon-59c674b14814c320583982/] The above code first converts the file contents in a string and then executes the...
Read More

Import CSV data to MySQL table

Here is a simple code to import CSV data into a MySQL table. There is no need to use MySQL frontend. Below is web based form to select a CSV file and import the records in a MySQL table Step 1: add.php First of all create a form to select the file Note that the values of the select box should match the table names in the MySQL database e.g. table_name_A, table_name_B, etc [crayon-59c674b148653199098629/] Step 2: config.php Create a config file Always a good idea to create a config file to declare your database connection and file import settings. [crayon-59c674b14865f900392531/] Step 3: import.php Import the data [crayon-59c674b148668791793518/]  ...
Read More

How to create a Prestashop module

Creating a new prestashop modules is not difficult as it seems. To create a simple module there are 4 easy steps to follow. Let us create an image slider for the home page 1. Add a new row for the new module in the ps_module table name: blockslider active: 1 2. Create the .php file Create a folder named blockslider in the prestashop modules directory and create blockslider.php file within the directory with below code. [crayon-59c674b148b0d346414120/] 3. Create the .tpl file Here you can use any JQuery based slider or create your own. Just remember to copy the relevant JS and CSS files along with any images to the modules folder and just call the JS and CSS files in the .tpl file. Create blockslider.tpl file in the same directory as above. [crayon-59c674b148b1a778534181/] 4. Transplant the module Once the module is created, it will appear in the Front Office Features section. Make sure it is installed and enabled Just transplant the module to Homepage content...
Read More
12