PHP

Displaying multiple fields in a single line in drupal7 views

Suppose you have three fields (Title, field_city, field_country) to display in your view. By default they will display in three separate lines as follows:

Benjamin Franklin
Philadelphia 
USA

To display this information in a single line, you need to edit the third field. Expand Rewrite Results, then type in the following:

[Title], [field_city], [field_country]

This will result in the following:

Benjamin Franklin, Philadelphia, USA 

Fatal error: Allowed memory size exhausted

Drupal can be very memory intensive CMS, especially if you have lots of menus, blocks and modules. If you exhaust the allowed memory size, you would get an error such as the following

Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 41 bytes) in somefile

To fix this problem, you need to allocate more memory.

1. Open you php.ini file If you don't know where this file is, run locate php.ini. Mine is located at /etc/php.ini. Make sure you back up this file before editing it.

Quick way to find which log file is logging errors

On development systems it is common practice to have different log file to log PHP error logs for different tools and different versions of the same tool. Generally it is a good idea to use meaningful names to make it easy to distinguish between the log files. If you have doubts, you can either look inside the code to see which error log file is being called or do the following:

$ cd ~
$ ls -al /var/log/httpd before.diff

Run the tool such that it would write something to the log file. Then

PHP date arithmetic

strtotime() function can be used for date arithmetic. Following are some examples:

Get next date

print date('Y-m-d', strtotime('+1 day', strtotime('2013-09-30')));

Prints

2013-10-01

Get previous month

print date('Y-m-d', strtotime('-1 month', strtotime('2013-09-30')));

Prints

2013-08-30

Go back 5 years

print date('Y-m-d', strtotime('-5 year', strtotime('2013-09-30')));

Prints

Laravel 5

Introduction

Laravel is a free and open-source PHP web framework like CodeIgniter and Zend. It is built on the Model-View-Controller (MVC) architectural pattern. It has very good documentation and training material. The community is also significant enough. I find it to be much easier than Zend and about as easy to use as CodeIgniter. Better training material and documentation makes Laravel easier for beginner than CodeIgniter.

MongoDB

MongoDB is document based NoSQL database. This means that:

  • it has no schema
  • it has no relations
  • it is agile and scaleable
  • stores information in nested documents of two-dimensional key/value pairs.

What are the benefits of using MongoDB?

  • you do not have to pre-define your schema or data model
  • works nicely with JavaScript
  • ideal for client-side applications

Installing MongoDB

Installing MongoDB on Mac with Homebrew

Creating a view that lists nodes tagged by a taxonomy term

Following is what we will be creating. A view that will create a page with a list of nodes (titles + teasers) and a block (with node titles only) of all nodes tagged by a taxonomy term.

Assumption: - You have multiple nodes tagged with term "PHP" - The nodes are of type Article

Go to Structure > Views > Add a new view and fill in the form as follows:

View name: PHP Show Content of type Article tagged with PHP sorted by Newest first check Create a page check Create a block Save & Exit

Then go to Structure > Blocks to enable the block.

Type casting in PHP

PHP does not support explicit type definition in variable declaration. For example, you CANNOT declare a variable as follows:

int $count = 10;

In PHP, a variable's type is determined by the context in which the variable is used.

Pages