PHP scripts and programs for Beginner to Master

php programs, php ready to use scripts & functions, css tutorials, html, javascripts, ajax examples for Beginner to Master

Hi,

Do you want your visitors to go to a particular page when they select any option from the dropdown list or from select box ?
Then here is the select element which will redirect users to the particular url when the select any option from it.










Now whenever a user selects any option from the dropdown at that time we are calling our javascript function gotourl which will process the current request and redirects users to that particular page.

Here is our Javascript function gotourl to do processing









Here is full code







If you know any other way to do the same then comments are always welcome.
Dont forget to tell your friends about http://programming-in-php.blogspot.com

How can we know the number of days between two given dates using MySQL ? or find the difference between dates using MySQL ?

Do you ever faced this question in technical interview or technical test?

I know many of you have faced this....... :)

Here is a simple function which can be used to find the number of days between two given dates in MySQL.

Syntax - DATEDIFF(firstdate,seconddate)

so internally it will calculate the difference in MySQL by doing "firstdate minus seconddate", so if firstdate is greater than sencoddate then output will be positive and if firstdate is less than seconddate then output will be negative.

eg. if you use this function as

SELECT DATEDIFF('2009-11-19', '2009-10-15');

in MySQL
then you will get output as 35.

And if you pass parameters as

SELECT DATEDIFF('2009-10-15','2009-11-19')

then you will get output as -35.


If you know any other way to find the difference between dates then comments are always welcome.
Dont forget to tell your friends about http://programming-in-php.blogspot.com

Many times when you go for an interview or technical test you may find this question 

"What is the difference between the sort() and asort() function in php ?"

Basically sort() and asort() functions are used to sort the array in php.

* sort() function syntax - sort(array,sorttype)


sorttype is Optional. Specifies how to sort the array values.

* SORT_REGULAR - Default. Treat values as they are (dont change types)
* SORT_NUMERIC - Treat values numerically
* SORT_STRING - Treat values as strings
* SORT_LOCALE_STRING - Treat values as strings, based on local settings



This function assigns new keys for the elements in the array. Existing keys will be deleted.

eg. See the code below when we create array $myarray we have assigned keys as a b & c.
Now we are applying sort function and printing the array.





so the output of the above code will be -
initial array contents -

Array ( [a] => chaprak [b] => programming [c] => dexter )

and after applying sort function -

Array ( [0] => chaprak [1] => dexter [2] => programming )

its keys are changed to numeric values and sorting is done.

View php manual for the array sort function


* asort() function syntax - asort(array,sorttype)

The asort() function sorts an array by the values. The values keep their original keys (index).


sorttype is Optional. Specifies how to sort the array values.

* SORT_REGULAR - Default. Treat values as they are (don't change types)
* SORT_NUMERIC - Treat values numerically
* SORT_STRING - Treat values as strings
* SORT_LOCALE_STRING - Treat values as strings, based on local settings






when we execute above code then it will produce the following output

initially array contents are - Array ( [a] => chaprak [b] => programming [c] => dexter )

after applying asort() function the contents are -

Array ( [a] => chaprak [c] => dexter [b] => programming )


see here sorting is done and even index is maintained.

View php manual for the array sort function

Whenever it is required to get some input url from user then the first things comes is check whether that url is working and valid or it is not working.

Here is a simple function which can be used to verify the liveness of the url.
Just pass the url to this function and it will tell you whether the url is working and valid or invalid.

In the function "check_url" we are going to open the given url with "fopen".
If its opened then return true else return false.



Now we are going to use this function.
you can pass the url from the GET or POST method to the function.
Here we are going to specify it directly in the code.
So we are asking our code to open the url http://www.chaprak.com with our function.



so after execution it will show the message as

http://www.chaprak.com is a valid URL.

See its very simple to check the validity of the url in php.

Full source code to try is here -




If you have any ideas and suggestions to improve this function then comments are always welcome.
Dont forget to tell your friends about http://programming-in-php.blogspot.com

Here is a simple function which can be used to count the total number of words present in a string in php.

For this function we are going to use inbuilt functions in php like count, explode, trim.

Function is very simple, it accepts the text.
Then we are checking if there are any extra spaces before and after the string, if it is there then removing that with trim function.

Then we are using count and explode function.

Explode will divide the text into array. (for more information on how explode works visit our post Split string with php explode function and print array)

Then we are counting the total number of array elements by using count function.



Now we are actually going to use this function.



Here is a full source code to try it out.



If you have any ideas and suggestions to improve this function then comments are always welcome.