slim framework

There are more than 10 frameworks for php, and each one has its own pros and cons. But one framework attracted me most. It is ‘Slim Framework‘.

It follows front controller design pattern, and since it is a micro framework, it is extremely light.

Developing apps using this framework is pretty simple. This framework can be used to implement REST based or backend service calls with ease. Just define a route and prepare the logic for it. The route can handle GET/POST/PUT/DELETE calls.

Here is an example of a REST based service in Slim:

When the end user hits the route /’users’, the corresponding function ‘getUsers’ is called, and the output is returned, which can be json, xml or plain text.

Now it is easy to call the above route with AJAX as follows:

Here is a reference for Hello World example.

FacebookTwitterGoogle+LinkedInEmailShare
Tagged with: , , ,
Posted in PHP

php parse chrome bookmarks

If you are using a Windows system and if you know the location where Google Chrome stores the bookmarks, you can setup a php script to parse the bookmarks and display them in a tabular format.

Generally Google Chrome stores the bookmarks in a file named ‘Bookmarks’ (without any extension). The file contains the bookmarks data in json format.

On Windows 7 or Windows Vista, this file will be present at a location like:
C:\Users\{Username}\AppData\Local\Google\Chrome\User Data\Default

And on Windows XP, the path will be something like:
C:\Documents and Settings\{Username}\Local Settings\Application Data\Google\Chrome\User Data\Default

Once you came to know the absolute path to this file, note it down.
Now lets setup a php script that parses the bookmarks and displays them in a tabular format, in the descending order of the date they were added.

Note: Here I am using the bookmarks path as: C:\Users\admin\AppData\Local\Google\Chrome\User Data\Default\Bookmarks. It can vary based on your Windows OS version and username.

FacebookTwitterGoogle+LinkedInEmailShare
Tagged with: , ,
Posted in PHP

php ajax xml example

Lets develop a scenario where the PHP sends an XML response for AJAX requests, which is later parsed at the client side to display result.

AJAX XML Response looks like this:

AJAX XML Response

For this tutorial, first create a table, “xml_users”:

Insert few records in the table:

Now write the index.php file, that just displays the dropdown with names of users from the table. The onChange event of this select box will fire a javascript function, that sends the id of the selected user to the server (a php file) using Jquery Ajax.

Now write the php file: responsexml.php, that processes the AJAX request and sends XML response.

Finally process the AJAX XML response. Modify the javascript function (showUser) above:

Now test the application. When you select a user from the dropdown, you can view the corresponding user’s details as shown in Demo.

DEMO

FacebookTwitterGoogle+LinkedInEmailShare
Tagged with: , ,
Posted in PHP

wordpress database architecture

This post explores the database architecture of WordPress and explains the relationships between various tables inside wordpress, and how the posts, categories, tags and comments are stored inside the database.

When writing this post, i considered the latest version of WordPress, 3.8.1. This may vary in future versions.
Considering the default wordpress installation, all the table names start with a prefix of ‘wp_‘.

For a detailed view of the E-R diagram, refer this.

  1. All the site information and admin panel settings are stored in ‘wp_options‘ table.
  2. The plugins data is stored under ‘wp_options‘ table, with option_name = ‘active_plugins‘.
  3. The users’ data is stored under ‘wp_users‘ table and some information is split into ‘wp_usermeta‘ table.
  4. WordPress stores the posts inside ‘wp_posts‘ table.
  5. The comments for a post are stored under ‘wp_comments‘ table.
  6. The metadata of a post, like the information about its category and tags, (referred to as taxonomy), is stored under the tables: wp_terms, wp_term_taxonomy and wp_term_relationships.

Let’s write a sample post in WordPress.

  • Its title is: Hello World
  • Its content is: Welcome to WP
  • Its id is 1 in the database
  • It was under the category: test
  • It was tagged: hello world
  • It has a comment: Hi

Then it is stored in the database tables as shown below (Click image to enlarge).

wordpress database architecture

Note that i am ignoring the table fields which are not necessary.

As you can see from the above diagram, the post with id=1 has 2 associated taxonomies (a category and a tag).

This information is stored in wp_term_relationships table as 2 records. The object_id in this table refers to the id column in wp_posts table. But in this table, only the taxonomy id is stored. The corresponding taxonomy names (category, post_tag) are stored in wp_term_taxonomy table. These 2 tables are related by term_taxonomy_id.

Finally the taxonomy values are stored in wp_terms table, which is related to wp_term_taxonomy table by term_id.

And the comments for the post are stored in wp_comments table, where the comment_post_id in this table refers to the id column in wp_posts table.

So that’s my analysis. Hope you enjoyed it. Now you may try few queries to retrieve posts by tag or categoryLaughing

FacebookTwitterGoogle+LinkedInEmailShare
Tagged with: , , ,
Posted in Wordpress

wordpress useful sql queries

Here is a list of useful queries in WordPress:

1. Get the posts that are published (not revisions or drafts)

2. Get posts which contain tags like ‘medicine’

3. Get tags for a post with id=1

4. Get posts by post_type

5. Sample way of retrieving data from database

6. Get attachment images belonging to posts with ids 1,2

7. Get all posts in a category with slug ‘education’

Here is another useful reference.

FacebookTwitterGoogle+LinkedInEmailShare
Tagged with: , , , ,
Posted in Wordpress

wordpress cheat sheet

1. bloginfo() holds more useful information about the WordPress site and the settings configured in admin panel.

For example,

a. bloginfo(‘url’) points to the site http url.

b. bloginfo(‘template_url’) or get_template_directory_uri() or bloginfo(‘template_directory’) points to the theme url path, which comes handy when including any css/js/image files.

c. dirname( get_bloginfo(‘stylesheet_url’)) points to the child template url.

More information can be found in the original manual page.

2. If you want to check whether the user is on the front page or inner page of your site, you can use this snippet:

if(is_home() || is_front_page()) {  }

3. Here is a list of frequently used functions of WordPress. Better make a note of this list, or memorize it.

Function Output Example
home_url() Home URL http://www.example.com
site_url() Site install URL http://www.example.com or http://www.example.com/wordpress
 admin_url()  Admin URL  http://www.example.com/wp-admin
 includes_url()  Includes directory URL  http://www.example.com/wp-includes
 content_url()  Content directory URL  http://www.example.com/wp-content
 plugins_url()  Plugins directory URL  http://www.example.com/wp-content/plugins
 theme_url()  Themes directory URL (from wp 3.5)  http://www.example.com/wp-content/themes
get_stylesheet_directory() Child template directory
 wp_upload_dir()  Upload directory URL  Returns an array like: 

[path] => C:\xampp\htdocs\wp/wp-content/uploads/2014/01
[url] => http://www.example.com/wp-content/uploads/2014/01
[subdir] => /2014/01
[basedir] => C:\xampp\htdocs\wp/wp-content/uploads
[baseurl] => http://www.example.com/wp-content/uploads

FacebookTwitterGoogle+LinkedInEmailShare
Tagged with: , ,
Posted in Wordpress

wordpress ajax example

Here we discuss about the 2 ways of sending AJAX request in wordpress, using jQuery.

The first example sends AJAX request to admin-ajax.php file, while the second one sends AJAX request to a custom php file.

We are trying to get the title for a post, by its id. So we pass the post id in the AJAX request, and then display the title after the response. For this, we take the twentyfourteen template of wordpress.

Firstly, in the twentyfourteen/header.php file, place a button that triggers an AJAX call. Look for h1 class=”site-title” tag under ‘body’ section, and below it, add this code:

Then add the below code before closing ‘head’ tag in above file.

Example-1

1. Modify the ajax_call() method above:

2. Edit twentyfourteen/functions.php file and add the below code at the bottom of it.

Example-2

1. Here, we send the AJAX request to a custom php file. So modify the ajax_call() method in header.php above.

2. Now create the custom php file, that handles the AJAX request, and sends response. We are creating a file called ‘get_post_title.php’ under ‘wp-admin’ directory.

Now go ahead and test these examples in your wordpress site. After you click on the button, you will notice the AJAX response getting added below the button. The response is nothing but the title of the post with id=1. You can also modify the codes above and test various other scenarios.

Note: Both the above examples produce the same output. But the second example requires the user to be logged in. The first approach is the standard one and is more recommended.

Output showing header before and after AJAX call:

Wordpress AJAX

FacebookTwitterGoogle+LinkedInEmailShare
Tagged with: ,
Posted in Wordpress

php get time by time zone

We can get the current time at various timezones of the world, by setting the timezone to the DateTime object.

Output:

2014-01-10 16:49:52 :Asia/Kolkata
2014-01-10 11:19:52 :Europe/London
2014-01-10 05:19:52 :America/Chicago
2014-01-10 06:19:52 :America/New_York
2014-01-10 12:19:52 :Europe/Paris
2014-01-10 15:19:52 :Asia/Dubai
2014-01-10 20:19:52 :Asia/Tokyo

FacebookTwitterGoogle+LinkedInEmailShare
Tagged with: , ,
Posted in PHP

Like My School :)

Translate