Tuesday, November 15, 2011

PHP : Getting filename from a URL

Some time using PHP, you might need to get the path from a URL, leaving behind just the filename at the end of the URL. You can achieve this with a regular expression pattern of course, but here is a much simple way to strip the file name from url using PHP.

Example:


      $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];


      $filename = basename($url);

     echo $filename;
?>
Hope you will enjoy this... :)

Tuesday, April 19, 2011

Changing URL with javascript Element setAttribute( )


Example

Search Engine


Here goes the code


Wednesday, April 6, 2011

Getting the recent one month or year records from MySQL table

All the below contents taken from http://www.plus2net.com

Getting the recent one month or year records from MySQL table

Some time we have to collect last 7 or 15 days or X days (or month, year or week) data from MySQL table. For example let us find out who are the new members joined in our forum in last week. One shop may be interested in knowing new products added in last one month. What are the books arrived in last one year. Here irrespective of the date values we want the records of last X days from today, or we can say that the records between today and last X days ( month , year or week) are required.

We will use the MySQL function CURDATE() to get the today's date.

To get the difference in today date and previous day or month we have to use the MySQL function DATE_SUB

DATE_SUB is a MySQL function which takes date expression, the interval and the constant to return the date value for further calculation.

Here are some sample queries on how to get the records as per requirements .

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 15 DAY)


The above query will return last 15 days records. Note that this query will return all future dates also. To exclude future dates we have to modify the above command a little by using between query to get records. Here is the modified one.

SELECT * FROM dt_tb WHERE `dt` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 15 DAY ) AND CURDATE( )


Let us try to get records added in last one month

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)


Here also future records will be returned so we can take care of that by using BETWEEN commands if required.

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)


You can easily make out what the above query will return.

We can collect records between a particular date ranges by using between command and DATE_SUB. Here are some queries to generate records between two date ranges.

select * from dt_tb where `dt` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 3 MONTH ) AND DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH )


This query will return records between last three months. This query again we will modify to get the records between three moths and six months.

select * from dt_tb where `dt` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 6 MONTH ) AND DATE_SUB( CURDATE( ) ,INTERVAL 3 MONTH )


Now let us change this to get records between 6 month and 12 month.

select * from dt_tb where `dt` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 12 MONTH ) AND DATE_SUB( CURDATE( ) ,INTERVAL 6 MONTH )


With this you can understand how the records between a month range or a year range can be collected from a table. Note that the months ranges are calculated starting from current day. So if we are collecting records of last three months and we are in 15th day of 9th month then records of 15th day of 6th month we will get but the records of 14th day of 6th month will be returning on next query that is between 3 months and 6 months.

Now let us try a different requirement. How to get the records of the working days of the week so far ? If today is Thursday then records from Monday to Thursday should be returned. We will discuss this in our next section >>.

Here is the sql code to create and fill the table with records

CREATE TABLE `dt_tb` ( `id` int(2) NOT NULL auto_increment, `dt` datetime NOT NULL default '0000-00-00 00:00:00', `dt2` date NOT NULL default '0000-00-00', UNIQUE KEY `id` (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

INSERT INTO `dt_tb` VALUES (1, '2007-02-15 00:00:00', '2005-01-25');
INSERT INTO `dt_tb` VALUES (2, '2007-02-12 23:56:54', '2005-06-12');
INSERT INTO `dt_tb` VALUES (3, '2005-12-08 13:20:10', '2005-06-06');
INSERT INTO `dt_tb` VALUES (5, '2005-02-10 00:00:00', '2006-01-02');
INSERT INTO `dt_tb` VALUES (6, '2006-11-26 00:00:00', '2006-12-25');
INSERT INTO `dt_tb` VALUES (7, '2006-11-26 00:00:00', '2007-02-25');
INSERT INTO `dt_tb` VALUES (8, '2007-10-20 00:00:00', '2007-10-25');
INSERT INTO `dt_tb` VALUES (9, '2007-02-11 00:00:00', '2007-01-25');
INSERT INTO `dt_tb` VALUES (10, '2007-01-22 00:00:00', '2007-01-15');

Records of the weekdays

 Here we will develop a query to get records of weekdays of the present week. To get the records we will try by using dayofweek function of MySQL.

This function dayofweek returns values 1 to 7 based on the weekday starting from Sunday as 1, Monday as 2 and …so on for others. So if today is Thursday then dayofweek function will return 5. So we need before three days record ( excluding today ) from today to get the records stating from Monday. So we will deduct 2 from the weekday figure. Here is the query to get the records of all weekdays of a week till today.



SELECT dt,id,dayofweek(CURDATE()) as c FROM dt_tb WHERE `dt` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL (dayofweek(CURDATE())-2) DAY ) AND CURDATE( )

All the Above contents taken from http://www.plus2net.com

Monday, April 4, 2011

Twitter Like More Button with jQuery and Ajax

This is an interesting tutorial. we developed this using jQuery and Ajax. Later on twitter added a new feature like 'more' button.

Monday, March 28, 2011

MySQL DATE_FORMAT() Function

Definition and Usage

The DATE_FORMAT() function is used to display date/time data in different formats.

Syntax

DATE_FORMAT(date,format)

Where date is a valid date and format specifies the output format for the date/time.
The formats that can be used are:
Format Description
%aAbbreviated weekday name
%bAbbreviated month name
%cMonth, numeric
%DDay of month with English suffix
%dDay of month, numeric (00-31)
%eDay of month, numeric (0-31)
%fMicroseconds
%HHour (00-23)
%hHour (01-12)
%IHour (01-12)
%iMinutes, numeric (00-59)
%jDay of year (001-366)
%kHour (0-23)
%lHour (1-12)
%MMonth name
%mMonth, numeric (00-12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss AM or PM)
%SSeconds (00-59)
%sSeconds (00-59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00-53) where Sunday is the first day of week
%uWeek (00-53) where Monday is the first day of week
%VWeek (01-53) where Sunday is the first day of week, used with %X
%vWeek (01-53) where Monday is the first day of week, used with %x
%WWeekday name
%wDay of the week (0=Sunday, 6=Saturday)
%XYear of the week where Sunday is the first day of week, four digits, used with %V
%xYear of the week where Monday is the first day of week, four digits, used with %v
%YYear, four digits
%yYear, two digits




Example

The following script uses the DATE_FORMAT() function to display different formats. We will use the NOW() function to get the current date/time:

DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')

DATE_FORMAT(NOW(),'%m-%d-%Y')

DATE_FORMAT(NOW(),'%d %b %y')


DATE_FORMAT(NOW(),'%d %b %Y %T:%f')

The result would look something like this:
Nov 04 2008 11:45 PM

11-04-2008

04 Nov 08

04 Nov 2008 11:45:34:243

Tuesday, February 22, 2011

How to get dropdownlist Label value in Javascript ?






Monday, February 14, 2011

CSS3 support for Internet Explorer 6, 7, and 8

Get Full URL

The PHP super global $_SERVER contains all the information you need to access various information about the URL and can even carry variables. Often you need to get the full URL of the page you are on and this requires piecing together several of the $_SERVER variables to get the whole URL.
Here we have done the work for you so you do not need to go searching through $_SERVER to find the correct super global variables to work with.

/****
How to get Full URL
***/
 function getFullURL()
 {
    /* Here we check for https*/
    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
    /* return the full address */
    return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
 }

 /* TO GET URL CALL FUNCTION LIKE THAT*/

 echo getFullURL();
?>

Thursday, February 10, 2011

How to Submit a Form Using JavaScript

Generally, a form is submitted when the user presses a submit button. However, sometimes, you may need to submit the form programmatically using JavaScript.
JavaScript provides the form object that contains the submit() method. Use the ‘id’ of the form to get the form object.
For example, if the name of your form is ‘myform’, the JavaScript code for the submit call is:
document.forms["myform"].submit();

But, how to identify a form? Give an id attribute in the form tag
<form id='myform' action='formmail.pl'>
Here is the code to submit a form when a hyperlink is clicked:
<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
<a href="javascript: submitform()">Searcha>
form>
<script type="text/javascript">
function submitform()
{
  document.myform.submit();
}
script>
if you want to want to study this in brief please follow the below link:
http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml


How to backup and download Database using PHP

< ?php $mysqlUserName = 'databaseusername' ; $mysqlPassword = 'databasepassword' ; $mysqlHostNa...