Tuesday, February 2, 2010

Whats new in PHP5

There are somany changes were added to php5 and now it occupies 3rd position in the worlds famous programing languages. see the all new features added to php5 @ http://devzone.zend.com/article/1714

Joins in mysql

There are different joins available in mysql to retrieve results from multiple tables.
Join, left join, right join, inner join, outer join

Here i explained each one by taking two tables as an example.

mysql>Products Table;
+----+--------------+--------------+
| id | product_name | manufacturer |
+----+--------------+--------------+
| 1 | Shoes | Company1 |
| 2 | Laptop | Company2 |
| 3 | Monitor | Company3 |
| 4 | DVD | Company4 |
+----+--------------+--------------+
4 rows in set (0.00 sec)

Table buyers:
mysql> SELECT * FROM buyers;
+----+------+------------+----------+
| id | pid | buyer_name | quantity |
+----+------+------------+----------+
| 1 | 1 | Steve | 2 |
| 2 | 2 | John | 1 |
| 3 | 3 | Larry | 1 |
| 4 | 3 | Michael | 5 |
| 5 | NULL | Steven | NULL |
+----+------+------------+----------+
5 rows in set (0.00 sec)

Normal Join

mysql> SELECT buyers.buyer_name, buyers.quantity, products.product_name FROM buyer
s join products on buyers.pid=products.id;
(OR)
mysql> SELECT buyers.buyer_name, buyers.quantity, products.product_name FROM buyer
s,products WHERE buyers.pid=products.id;

+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
+------------+----------+--------------+
4 rows in set (0.00 sec)

The above query displays results only with matching pids in both tables.



Left Join

mysql> SELECT buyer_name, quantity, product_name FROM buyers LEFT JOIN products ON
buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
| Steven | NULL | NULL |
+------------+----------+--------------+
5 rows in set (0.00 sec)

Explaination:
Mysql starts with the left table (buyers) as main source then For each row from the table buyers mysql scans the table products with pid (i.e id of the product in products table) and returns the product name. Then the product name is joined with the matching row to the table buyers. For unmatched rows it returns null.

The above query is same as below (except with null values)

mysql> SELECT buyers.buyer_name, buyers.quantity, products.product_name FROM buyer
s,products WHERE buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
+------------+----------+--------------+
4 rows in set (0.00 sec)

The above query displays results for only matching pids in both tables.

Right Join

mysql> SELECT buyer_name, quantity, product_name FROM buyers RIGHT JOIN products ON
buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
| NULL | NULL | DVD |
+------------+----------+--------------+
5 rows in set (0.00 sec)

Explaination:
Mysql starts with the Right table (products) as main source then For each row from the table products mysql scans the table buyers with id (i.e pid of the buyers table) and returns the product name. Then the product name is joined with the matching row to the table products. For unmatched rows it returns null. From my example above it returns NULL for DVD because no one bought DVD.

Inner join is same as join.

The word OUTER may be added after the word LEFT or RIGHT in the left/right joins - it's provided for ODBC compatibility and doesn't add an extra capabilities.

Sql Difference between where clause and Having clause

Hi, below are differences between where clause and having clause in mysql select statements.

1. Having clause is usually used with Group By clause although it can be used without it too.
2. 'Having' is just an additional filter to 'Where' clause.
3. 'Where' clause applies to the individual rows whereas 'Having' clause is used to test some condition on the group(usually aggregate methods) rather than on individual rows.
4. WHERE clause is used to impose condition on SELECT statement as well as single row function and is used before GROUP BY clause where as HAVING clause is used to impose condition on GROUP Function and is used after GROUP BY clause in the query. For example queries look at http://www.allinterview.com/showanswers/6252.html

Monday, February 1, 2010

Awesome rounded cornered blocks using CSS only - nifty corners

Hi Friends,
To display rounded cornered block in your webpage, most probably we are designing rounded cornered blocks in photoshop then converted the image using css by taking small corner images as source for displaying rounded corner effect, But we can do this by using only css not any images or javascript support needed. The concept is called nifty corners see the demo http://acko.net/files/nifty-aa.html

You can earn money while visitors leaving your website

Hi, you can earn money while visitors leaving your website, the exitjunction will suggest visitors with some ads before leaving your website, So if users clicked on any ad, then your will get income. Why don't you look once, just click on below banner to see more details...

ExitJunction.com  - Make Money From Your Exit Traffic!

Hide default navbar menu and search from your blog

Hi, if you want to hide the default navbar, search and other menus displayed at top, do follow below three stes.

1. Login to Blogger then klick "Layout --> Edit HTML".
2. Copy the code below and insert into your tag styles section.

#navbar-iframe {
display: none !important;
}

Good Luck., happy bloging

Friday, January 29, 2010

Awesome Taxonomy module for categorise content in drupal

Taxonomy is pretty cool for creating categories in drupal. You can create multiple categories and sub categories for each node type. These categories are applied to the partucalar nodes as you mention. So the same category set can be set to multiple content types and also each content type have multiple categories lists. See all about taxonomy at http://groups.drupal.org/taxonomy

A small Tip

A small tip to call methods dynamically using Ruby's send method.

["foo", "bar"].each do |method|
MyClass.send(method)
end

The above code is same as calling MyClass.foo and MyClass.bar.

Make your rails application DRY with constants

Constants help with consistency, they easily allow you to keep your code DRY.
One of the ways I utitilise constants is to standardise the date formatting I use across an app.
For example, in environment.rb I will create a module to contain my date format constants:

module Dates
DATE_FORMAT = "%d% %b %Y"
DATE_TIME_FORMAT = "#{DATE_FORMAT} %H:%M"
end

The module just groups them nicely together. To use the constants I will write the following:

@object.created_at.strftime(Dates::DATE_FORMAT)
@object.created_at.strftime(Dates::DATE_TIME_FORMAT)

Remember you will need to restart your server after making any changes to environment.rb

Helpers usage in ROR

Helpers are basically small snippets of code that can be called in your views to help keep your code DRY - i.e. Any code that you are repeating regularly can most likely be moved into a helper.

Using helpers is simple, each controller has it's own helper file or you can write helpers in the application helper file if it will be used across the entire application. For example a pagination helper might go in there.


def my_helper(opts={}) #args defined as hash
"output something" if opts[:method_passed_in] == "foo"
end

You can then use it in your view with:


<%= my_helper('foo') %>

Ruby on Rails User multiple layouts in single controller

Use multiple layouts in single controller using helpers, one layout per some actions and other for some actions etc..

layout :choose_layout

private
def choose_layout
if [ 'signup', 'login' ].include? action_name
'login'
else
'admin'
end
end

Alternate row colors for table rows in Ruby on Rails

Use the rails builtin helper "cycle" to add alternate row colors to the table rows in a loop.

<%- for item in @items do -%>

tr class="<%= cycle("even", "odd") %>"
... use item ...
/tr

<%- end -%>


Then in your stylesheet add below

tr.even td{
background-color:#CCC;
}

tr.odd td{
background-color:#EEE;
}

Thursday, January 28, 2010

Solution to Undefined method `cache_template_extensions=' in Ruby on Rails

When you upgrade to rails version 2.2.x or later then some times you might get
the below error
Undefined method 'cache_template_extensions='

To fix this problem check your environment.rb and environemts/development.rb, production.rb, test.rb files and comment/remove the below line of co
de.

config.action_view.cache_template_extensions = false

Restart the server and see output.

Solution to uninitialized constant FileColumn::ClassMethods::Inflector in Ruby on Rails

When you install file column plugin in latest version of rails, you might got this noname or uninitialized error. To fix this error open the plugin/lib/file_colum.rb file goto line number 619 and add ActiveSupport:: before the Inflector. See the final code should be like below at line number 619.

my_options = FileColumn::init_options(options,
ActiveSupport::Inflector.underscore(self.name).to_s,
attr.to_s)

Also at line number 79 find and change as below

url << size="4">Restart the server and see the output.

Wednesday, January 20, 2010

Solution to Google checkout payment gateway oops error

When google checkout responds with oops error, you can see the actual problem
by login with your merchant details then Goto

Tools -> Integrated console
There you can see the log of what problems occured while users try to pay you.

Linux chage folder permision to a group

Hi, In linux root have the full permission over entire os and other users will have some previliages only. So when you loggin to the system and want to have write permission to one folder all you have to do, first see the permission of that folder, it contains three blocks,

OWNER - root

GROUP - root

OTHERS --

Here this means all all writes are permitted to root only. If you want to have write permission to this folder, First change the group permission to your group, then provide the write permission to the group. For this follow the below commands

First login as superuser that i.e root for this run command 'su' in the terminal

sudo su

enter root password:

chgrp -R venkat /var/www

#the above command will change the group

chmod -R g+w /var/www

#the above command will provide the write permission to the group

Now the user who logged in as venkat can have the read write permission to the folder var/www





r = read permission
w = write permission
x = execute permission
- = no permission

Ref: http://www.tuxfiles.org/linuxhelp/filepermissions.html

http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilesp.html

Tuesday, January 19, 2010

Remove N/A option for radio buttons and select boxes in Drupal using CCK

Whenever you create a custom form fields with CCK In drupal, it is irritating us by displaying N/A for radio buttons and Selectboxes. You can remove this only through a code. goto modules/cck/modules/optionwidgets.module and find the function named optionwidgets_options() and comment the below code

if (!$field['required']) {
if ((in_array($field['widget']['type'], array('optionwidgets_buttons', 'nodereference_buttons', 'userreference_buttons')) && !$field['multiple'])
|| (in_array($field['widget']['type'], array('optionwidgets_select', 'nodereference_select', 'userreference_select')))) {
$options = array('' => theme('optionwidgets_none', $field)) + $options;
}
}

Now the N/A option is not displayed for radio buttons and select boxes.

Allow users to select Roles while Registration in Drupal

If you want to provide an option for the user to select roles in the registration form, use the module called user selectable roles. The module source url is
http://drupal.org/project/user_selectable_roles
After install it in your application, you should goto administer->user mangement-> user_select_roles
There you can set which roles are displayed in the registration form and other options to select multiple roles selection or single etc.

Restrict accessing your site for some users based on their IP Address

In .htaccess add below lines

Order allow, deny
Deny from 192.168.0.10
Deny from 1.2.3.4 5.6.7.8 127.0.0.1
Allow from all


It is allowed accessing for all except fom IPs 92.168.0.10,
127.0.0.1,127.0.0.2,127.0.0.3


--------------------------------------------------------------
OR if you want to access your site from your IP only add below lines in .htaccess file

Order deny, allow
Deny from all
Allow from 192.178.0.10


This will be usefull for your site administrators.
If you set this for site admin, then the administrator can access admin area from the mentioned IP only.


Run PHP Code in HTML files

If you want to run PHP Code in your static HTML pages, you need to create .htaccess file (if it is not already exists) in the directory of where all your html files exists and add the below lines of code

For web servers using PHP as apache module:

AddType application/x-httpd-php .html .htm

For web servers running PHP as CGI:


AddHandler application/x-httpd-php .html .htm

If your requirement is only for one page then use below code

AddType application/x-httpd-php .html