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

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

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

 

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

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

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.

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

Now create a function to get users from the above table near a certain address (where the event is to be held).

Now just call the above function with 3 parameters as input

  1. Radius => Defines the area you want to set to alert your users e.g. 100 KM
  2. Latitude => Latitude of the place where the event is to be held
  3. Longitude => Longitude of the place where the event is to be held

 

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)

3. Process the above form (procesform.php)

4. Get file (get-file.php)

5. Download a file

Now to download a file just link the file to get-file.php with an id
e.g.

 

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

Now let’s clone the record

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 in a temporary table
3. Update the primary key in the temporary table so that its not duplicate
4. Insert the record from the temporary table into the main table
5. Drop temporary table

And here’s the query

 

LDAP Authentication using PHP

 

LDAP search using PHP

 

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.

The above code first converts the file contents in a string and then executes the PHP from it.
Note that the above can also be done by a simple Include statement i.e.
include(“myotherpage”) and that will also execute the PHP code.
So you would wonder what am I achieving through this method?
Converting the file contents to a string variable, you can do a lot of other things as below

Showing all PDF document links with a PDF icon

You can then style the pdf id in a seperate stylesheet.

The above is probably not a good example to show what you can do with file_get_contents

Have a look at the below piece of code.
It will allow you to get content between a set of tags/text.

Furthermore, you can also replace URLs within the file

There are quite a few other cool things you can do with this.
However, note that these snippets of code will be processed every time you load the page. It first converts it to string and then does the processing and finally evaluates it as PHP. This can add a bit of overhead compared to a single Include statement. However, in my project I am using all the above tricks and I don’t see any performance problems and ofcourse you don’t need to convert to string everytime. You can convert the file to string, stack all the statements and then execute the PHP.

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

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.

Step 3: import.php Import the data

 

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.

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.

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

12