Posted by: uttamkumar | July 20, 2007

USING MYSQL String Function

you can use combination of string function to get the complex result needed in some project.String function in mysql helps us to extract what we want from a string and that can avoid lot of code.The common String functions available in MYSQL can be found on the link http://dev.mysql.com/doc/refman/5.1/en/string-functions.html

 

Use of MYSQL String function.

 

consider the following table which contains caseId and you have to extract maximum number at the last of caseId, those belong to id 1.

 

caseId id Value
JET/2005-2006/1 1 15
JET/2005-2006/2 1 25
JET/2005-2006/1 2 15
JET/2005-2006/2 1 2
JET/2005-2006/2 2 12
JET/2005-2006/3 1 5

 

you can use the following query to get the result.

 

SELECT max(substring(caseId,locate(”/”,caseId,locate(”/”,caseId)+1)+1)) as maxCaseId FROM `test` WHERE id=1

 

The result of the query is as follows.

 

maxCaseId
3

 

This query work as follows.

 

inner locate function get’s first occurrence of “/” [let's 4].and outer locate function get first occurrence of “/” [let's 17] in the string starting from position 4+1=5.so finally in subString function will give all the string values after position 17+1=18.So by this we will have all numbers those belongs to id 1.now finally max function will get max number within all the numbers found by substring function.

 

So This is just an example to explain uses of string functions available in MySql. There may be lot’s of other situation where you can use string function provided by mysql and get your work done rather than getting result by writing complex logic in programming.

Posted by: uttamkumar | July 17, 2007

Fusion chart

FusionCharts Free is a flash charting component that can be used to render data-driven & animated charts for your web applications and presentations.It is a cross-browser and cross-platform solution that can be used with  PHP, JSP simple HTML pages or even PowerPoint Presentations to deliver interactive and powerful flash charts.you just need to download fusion chart code in the language you are using and then use the code.

In PHP funsion chart accept all data from the database table in xml format to render fusion chart [Animated one].and  then this  data is passed in respective flash chart and finally chart get displayed.

Fusion Chart example

Posted by: uttamkumar | July 17, 2007

Design Pattern in PHP

Design patterns present useful ways for developing robust software and provide a way of encapsulating large ideas in friendly terms. For example, writing a messaging system to provide for loose coupling.

loose coupling simply means to develope codes such that they can be less dependent on each other.for ex. if a class name is rename due to some reason then it should not affect other pages or code where you are using it.Design pattern in PHP solve such problems.

there are lot’s of design pattern available in php.we will see few of them.

FACTORY pattern

The problem is tight coupling. Functions and classes in one part of the system rely too heavily on behaviors and structures in other functions and classes in other parts of the system. You need a set of patterns that lets these classes talk with each other, but you don’t want to tie them together so heavily that they become interlocked.

In large systems, lots of code relies on a few key classes. Difficulties can arise when you need to change those classes. For example, suppose you have a User class that reads from a file. You want to change it to a different class that reads from the database, but all the code references the original class that reads from a file. This is where the factory pattern comes in handy.

The factory pattern is a class that has some methods that create objects for you. Instead of using new directly, you use the factory class to create objects. That way, if you want to change the types of objects created, you can change just the factory. All the code that uses the factory changes automatically.

FactoryExample.php shows an example of a factory class. The server side of the equation comes in two pieces: the database, and a set of PHP pages that let you add feeds, request the list of feeds, and get the article associated with a particular feed.
FactoryExample.php

<?php
interface IUser
{
  function getName();
}

class User implements IUser
{
  public function __construct( $id ) { }

  public function getName()
  {
    return "uktrips007";
  }
}

class UserFactory
{
  public static function Create( $id )
  {
    return new User( $id );
  }
}

$uo = UserFactory::Create( 1 );
echo( $uo->getName()."n" );
?>

An interface called IUser defines what a user object should do. The implementation of IUser is called User, and a factory class called UserFactory creates IUser objects. This relationship is shown as UML in Figure 1.
Figure 1. The factory class and its related IUser interface and user class
The factory class and its related IUser interface and user class

This will give you result as ‘uktrips007′

to be continued….

Posted by: uttamkumar | June 28, 2007

Imaptool

Image tool is a good tool to develop Image map.it’s linux tool can be downloaded using the command as follows.

username@machineName:~$ sudo apt-get install imaptool

once you installed it you can use the imaptool using the command as follows

username@machineName:~$  imaptool path/of/your/image/file

this command will open a new window with GUI where you can select any shape withing the image for image map.then click on dump html.image map code will be displayed in shell that you can use in your webpage.

Imap illustration

Posted by: uttamkumar | June 7, 2007

Photo Gallary

Thinking for future Me in GOA Goa Fort
Me with Rupesh Drawing by me…. A Ship in the ocean
Posted by: uttamkumar | May 23, 2007

USE of filter_var

filter_var() is provided in php5.2 which allow you to filter out input data entered by the user.the example below use filter_var() to strip out HTML tags if entered in the string(May be entered by the user).

<?php

echo filter_var(‘Hi <Strong>Uttam</strong>’, FILTER_SANITIZE_STRING);

?>

this function will output  “Hi  Uttam” and will strip out HTML tags.the option FILTER_SANITIZE_STRING, which takes input and removes any HTML tags and optionally encodes or removes special characters.

Similerly you can use filter_var function for logical test.see the example below.

<?php

$age = filter_var($_GET['age'], FILTER_VALIDATE_INT, array("options" =>
array("min_range"=>18, "max_range"=>60)));
If ($age === FALSE){
Echo "<h1>WRONG! age</h1>";
}else{
 echo "your age is " . $age . ".\n\n";
}

?>

this code will check for age entered by the user to be greater than 18 and less than 60, if not true it will Wrong age as output.

Posted by: uttamkumar | May 23, 2007

Memory Management in PHP


Posted by: uttamkumar | May 19, 2007

Yahoo Pipe

Yahoo has released “Pipes” which is Based on the concept of unix pipes, it allows you to filter and process RSS feed data in a chain – by a nice GUI. The result can be spit out in a variety of formats that can then be used in your RSS reader,you can also apply lot’s of filter to filter out you RSS data you want to display

This is really an awesome effort by yahoo to create RSS feed based on filteration.

This is just a snapshot of the sample pipe created using yahoo pipe.

Yahoo Pipe Example

In the above pipe I have created RSS feed from two different URL and displayed these RSS feed in two diiferent language.I hope this example will give you an idea about how yahoo pipe is benificial for us.

Posted by: uttamkumar | May 17, 2007

Interview Questions

Diiference between session and cookies?

Ans:- 1. session stored at server while cookie get stored at client’s webbrowser.

2. session are stored as an object on server side on the path specified in the php.in file for session_savepath variable while cookies are passed as header and stored on client’s web browser as text file.

3. session variables are exists only when session doesn’t expires while cookies[presistent not session] can be stored for future time also and can be used to handle user’s preference.

what are encryption methods available in php and mysql?Difference between sha1 and md5?

Ans:- some encryption methods availavle in php are md5,sha1,encrypt,password.Difference between sha1 and md5 encryption is that sha1 take more space than md5 in terms of storing information in the database.Some encryption available in mySql are password,MD5,encrypt.

What is Ajax & how it works?

Ans:- AJAX stands for asyncronous javascript and xml.It’s asyncronous this script doesn’t wait for the response.Regardless of this it can make another request without waiting for the response.Fist of all it creates XMLHttpRequest object by which it makes a request.and when response comes back script handover it to another function which will check for the response readyStatus and returns the response.

OOPs principles?

Ans:- Polymorphism [not supported in PHP]

Encapsulation

Abstraction

Dynamic binding

class and Object

for more detail please refer http://in2.php.net/manual/en/language.oop5.php

Mysql Joins?

Ans:- I don’t have idea but know about types.Cross,Left,right,inner and outer join.

Strings and array functions.array_walk,in_array?

Ans:- check functionlist available on

http://in2.php.net/manual/en/ref.array.php [for array]

http://in2.php.net/manual-lookup.php?pattern=string&lang=en [for strings function]

Difference between include,include_once,require,require_once?

Ans:- Unlike include(), require() will always read in the target file, even if the line it’s on never executes. If you want to conditionally include a file, use include(). The conditional statement won’t affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.include() may give warning and proceed further but require() will hault whenever warning/error faced in the script.include_once(),require_once() as name suggest can be used to include a file only one.[useful in case we include a class file]

How can we submit a form without a submit button?

Ans:- you can call a function in javascript onclick event of any form element/link and in the function you amy use document.formName.submit(); to submit the form

What is the difference between mysql_fetch_object, mysql_fetch_rows and mysql_fetch_array?

Ans:- mysql_fetch_object will return an object by which we can access the database fields records while mysql_fetch_aaray and mysql_fetch_rows return array of database records.mysql_fetch_row will not return associative array while mysql_array will return associative array too.

What is the difference between $message and $$message?

$message is used as a veriable while $$message can be used to assign a value as variable.for eg:

$message=’uttam’;

$$message=’kumar’;

in this case $kumar will give you value as ‘uttam’.

What is meant by nl2br()?

Returns string with ‘<br />’ inserted before all newlines(\n).

$msg=”these are \n interview question”;

$nl2brmsg=nl2br($msg);

$nl2brmsg will return value as “these are </br> interview question”.

Posted by: uttamkumar | May 15, 2007

Mod Rewrite

Different options available with mod rewrite are as follows.

RewriteEngine

This directive turns the rewrite engine on or off.
Syntax : RewriteEngine (on|off)
Example: RewriteEngine on

If you wish to disable rewriting, use RewriteEngine off instead of commenting out all the other lines. Since the rewrite engine is off by default, you have to turn it on every time you wish to use the mod_rewrite module. So the first line will be always…

RewriteEngine on

RewriteCond

The ‘if’ statement of mod_rewrite. The commands given below a RewriteCond line will be evaluated only if this returns a true value.
Syntax : RewriteCond TestString ConditionPattern [Flags]
Example: RewriteCond %{HTTP_REFERER} ^http://www.google.com [NC]

The RewriteCond has two arguments plus an optional third one.

TestString

There are three main types of strings that can be used in TestString part of the directive.

RewriteRule Regular Expression Captures – $N

Here N is a number between 0 and 9. If you use the string $1 in the TestString part of the RewriteCond directive, it will be replaced by the first capture from the regular expression for the corresponding RewriteRule – ie. the RewriteRule that will follow this condition.

RewriteCond Regular Expression Captures – %N

For this string, N is a number between 1 and 9. If you use the $1 in the TestString part of the RewriteCond directive, it will be replaced by the first capture from the regular expression for the corresponding RewriteCond – ie. the RewriteCond above this condition.

Server Variables

This are given in the format…

%{VARIABLE_NAME}

The most commonly used Server variables are given below. To see the full list of available server variables, see the apache manual page on mod_rewrite.

HTTP_USER_AGENT
A string containing the name of the browser the visitor is using. Eg ‘Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8) Gecko/20051111 Firefox/1.5′
HTTP_REFERER
The URL of the page from where the current visitor came form.
REMOTE_ADDR
The IP of the visitor.
REMOTE_HOST
The visitor’s host name.
SCRIPT_FILENAME
The filename of the page that was requested.
QUERY_STRING
The queries that was passed to the page.
REQUEST_URI
The called URI
REQUEST_FILENAME
The path of the file – ie. the URL of the page without the domain.

CondPattern

CondPattern is a regular expression – with some extra features…

!
You can prefix the ‘!’ operator to any condition pattern to make RewriteCond return true if the pattern does NOT match.
<CondPattern
Compares the two string and return true if the TestString is lexically lower than the given pattern
>CondPattern
Return true if the TestString is lexically greater than the given pattern.
=CondPattern
Return true if the TestString and pattern are equal.
-d
Return true if TestString is a directory(folder)
-f
Return true if TestString is a file

Flags

Flags are given inside squire brackets([FLAGS]). Two flags are available for RewriteCond…

NC
No Case – the case is ignored in matching.
OR
Use this to combine two consecutive RewriteCond with a logical OR – if this is not given, an AND condition is implied

RewriteRule

Provides the rules for rewriting URLs.
Syntax : RewriteRule Pattern Substitution [Flags]
Example: RewriteRule ^/user/(.*) get_data.php?user=$1 [NC]

This is were the URL rewriting actually happens. This directive has two arguments plus an optional third one.

Pattern

The pattern is a perl compactable regular expression used to match the current URL. You can capture this by using the parenthesis ie. ‘(‘ and ‘)’. All text between these will be captured and could be used in the Substitution part of RewriteRule.

Substitution

The actual URL that is to be called is given here. This involves the path of the script and the parameters that should be given to it. This supports all the special features of TestString(like $N, %N and Server variables) besides plain text. It also supports the ‘!’,'<’,'>’ and ‘=’ prefix operators as we saw in the CondPattern.

Flags

Flats are the third argument to the RewriteRule directive. This should be given inside square brackets([FLAGS]). Flags is a comma-separated list of these flags…

R – Redirect
The URL in the address bar will change if this flag is used – as the server will use the HTTP response of 302 (MOVED TEMPORARILY) when redirecting the page.
F – Forbidden
Using this flag immediately sends a HTTP response of 403 (FORBIDDEN). Use this flag with appropriate RewriteConds to conditionally block some URLs – for example image hot linking from external sites.
G – Gone
Sends a HTTP response header of 410 (GONE). This flag is used to mark pages which no longer exist as gone.
L – Last
Stop the rewriting process in this rule and don’t apply any more rules. Think Perl’s last command or the break command of C.
NC – No Case
Makes the Pattern case-insensitive.
QSA – Query String Append
This flag will append the query string from the current URL to the substitution string.

Eg.

RewriteRule ^/user/(.*?)/ get_data.php?user=$1 [NC,QSA]

Please keep in mind that this is just the most commonly used Flags – for the full list, go to the apache manual page on mod_rewrite.

 Treat html page as php page using .htaccess

<Files *.htm>
ForceType application/x-httpd-php
</Files>
<Files *.html>
ForceType application/x-httpd-php
</Files>
RewriteEngine On

« Newer Posts - Older Posts »

Categories