Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
Monday, April 6, 2015
Thursday, August 15, 2013
PHP Function to Convert seconds to Hour Minute Second format
<?php
echo gmdate("H:i:s", 12600);
?>
Output: 03:30:00
Monday, July 15, 2013
Single digit to Double digit conversion of a number using PHP
<?php
$number = 8;
echo sprintf("%02u", $number); //Outputs 08
?>
For details refer: http://www.w3schools.com/php/func_string_sprintf.asp
Monday, April 15, 2013
PHP functions (substr,rtrim) to remove last character from a string
<?php
$string="apple,ball,cat,";
echo substr($string, 0, -1);
echo "<br/>";
echo rtrim($string, ",");
?>
Output:
apple,ball,cat
apple,ball,cat
rtrim is not the same as substr. Difference is shown below
<?php
$string="apple,ball,cat,,";
echo rtrim($string, ",");
?>
Output:
apple,ball,cat
As seen from the above example rtrim trims all specified character from the end part of the string.
Wednesday, December 26, 2012
Uploading file using PHP
Uploading files to the server.
Now create a page
sms2.php and type the code shown below. After that run page1.php. As shown in
the code below the file is uploded to the folder “upload”. So before running the page create the folder “upload”
in the root folder of the server. Now run the php page page1.php and select the file and click
“SAVE” button.
On clicking the
save button your photo/ any other file you had selected will be in the upload folder
and you will get the output as below.
INCLUDE IN PHP
You can insert the content of one PHP file into another PHP
file before the server executes it, with the include() or require() function.
Assume that we have a set of urls (as circled below) or a
menu to be displayed in all pages of your application.
The below code of particular set of urls should be included in all your files. When
the application consists of many pages it will difficult for you to edit or
insert the code. So to simplify we use the include function of PHP. We save the
below code in a file called sms2.php
The include function can be used as shown below.
Even if we have included
the code in a separate file, when we view the page source of the main
file we get the output as shown below.
Form Handling in PHP
HOW TO GET USER INPUT
The PHP $_GET and $_POST variables
are used to retrieve information from forms, like user input.
The code for the above pages is a s below.
Implementing using GET method – In page1.php change
the method of the form as method=”get”. In page2.php edit $_POST as $_GET. On
running the code you will get the output as shown below. Please check the url
shown in the output. Values will be displayed in the url.
When to use method="post"? Information sent from a
form with the POST method is invisible to others and has no limits on the
amount of information to send.
The PHP $_REQUEST Variable: The
$_REQUEST variable can be used to collect form data sent with both the GET and
POST methods.
Variables in PHP
VARIABLES
- All variables in PHP start with a $ sign symbol.
- PHP is a Loosely Typed Language, i.e. a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.
- A variable name must start with a letter or an underscore "_".
- A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ).
- A variable name should not contain spaces.
The below code shows how to print a variable.
PHP script to print Hello World
BASIC SYNTAX
·
PHP scripting block always starts with <?php and
ends with ?>
·
A PHP scripting block can be placed anywhere in
the document.
· A PHP file normally contains HTML tags, just
like an HTML file, and some PHP scripting code.
·
Each code line in PHP must end with a semicolon.
·
The file must have a .php extension.
AJAX DEPENDENT DROP DOWN
To know the basics of Ajax refer http://w3schools.com/ajax/default.asp
Say your pull down menu Caste is dependent with the pull down menu Religion as given below
<select name="religion" id="religion" onchange="showCaste(this.value)">
// Your options here
</select>
<select name="caste" id="caste">
// Your options here
</select>
In the religion pull down menu we add a property onchange. That is when we select a particular religion the function showCaste will be called. this.value is the value of the selected religion.
The below javascript code should be included in the head section of the page.
// JavaScript Document
<script type="text/javascript">
function showCaste(religion_id)
{
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else if (window.ActiveXObject)
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
else
alert("Your browser does not support XMLHTTP!");
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
var ss=document.getElementById('caste');
ss.innerHTML= xmlhttp.responseText;
}
}
xmlhttp.open("GET","showCaste.php?religion="+religion_id+"&token="+Math.random()*5,true);
xmlhttp.send(null);
}
</script>
The above code calls the showCaste.php page where we have written the code to populate the caste pulldown.
//showCaste.php
<?php
//steps to initiate connection to the database
$religion=$_GET['religion'];
$result = mysql_query("SELECT * FROM caste WHERE religion_id='".$religion."'");
while($row = mysql_fetch_array($result))
{
$pullDown.="<option value=".$row['id'].">".$row['display']."</option>";
}
echo $pullDown;
?>
How to display date in PHP
To display date in PHP we use date() function.
Example-1:
<?php echo date("d-m-Y"); ?>
Output: 13-11-2011
To display time in PHP we can use the same function like this
Example-2
<?php echo date("h:i:s"); ?> //Outputs hour:minute:seconds as below
Output: 06:09:08
Oops don't panic if the time displayed on your screen is different from your clock. To correct it you have to set the default time-zone as below:
<?php
date_default_timezone_set('Asia/Kolkata'); //mine is Asia/Kolkata
echo date("h:i:s");
?>
You may have noticed that in example1 date("d-m-Y") d is small, m is small, Y is capitalized. Is there any relevance in doing so? Yes of-course, there is.
echo date("d-m-y"); //outputs 13-11-11
echo date("d-M-y"); //outputs 13-Nov-11
echo date("D-M-y"); //outputs Sun-Nov-11
To get the complete chart CLICK HERE
Importance of Frameworks in PHP
This is to all PHP developers who are not at all using any frameworks for their development. For those who are just sticking on their PHP basics and developing projects (may be small, medium or large).
May be you have heard about some frameworks in PHP, like Yii, CakePHP, Zend etc. All these are based on MVC Framework. MVC is the abbreviated form of model, view controller.
The moment we hear of framework we definitely search it, we just make a try, but we find it very difficult to follow and then finally we drop it and move towards our core level PHP. Please don't do that. All software company needs PHP Developers with skills in any of the above specified frameworks. So better learn it before you search for a job.
What is MVC Framework?
It is a software architectural pattern used in software engineering. It splits our entire project or application into 3 layers - model layer, view layer and controller layer.
Now you may think internally, why should I split them into layers. My answer is : to increase flexibility and maintainability of code. It turns the application into modular and rapidly developing package. Designers and developers can work simultaneously. New features can be added easily, etc.
![]() |
| General Representation of MVC framework |
Model Layer - It acts as a first layer of interaction with the database you have defined for your application. It take care of tasks such as saving the data, retrieving the data, searching the data etc. It defines relationships between tables of the database etc.
View Layer - Represents the layer where you define how you want to present the data to the users. For example, as the Model layer returns a set of data, the view would use it to render a HTML page containing it. Please note that it does not limit to the HTML representation.
Controller Layer - As you can see in the figure the Controller layer handles requests from users. It gives the response with the help of both Model Layer and View Layer. It actually manages all resources which are needed to complete the task.
This is just an into. I know you may be confused now, and may be you are about to quit the idea of using the frameworks. Please don't quit. If you have never built an application this way, it takes some time getting used to, but once you build your first application using any of the frameworks, may be CakePHP, or Yii, you will never do it any other way.
I am working on CakePHP. It's very simple to understand. Very good for beginners.
As per the survey Yii framework is at the top. There are other frameworks also, like Zend, CodeIgniter, Symphony etc.
Select one of the frameworks, download the stable version of the selected framework and build one application. Then only you will understand the basics. Once you get the idea of one framework you can then master any framework within no time.
So best of luck..
Setting year range of Date drop down in CakePHP 1.3
Setting range of year in Date drop down in CakePHP.
There may be situations where you need to change the default values displayed in the year dropdown of Date field. Use minYear, maxYear for specifying the range.
<?php
echo $this->Form->input('dob', array('minYear' => '1950','maxYear' => date('Y')-15));
?>
In the above example 'dob' is the field name.
Subscribe to:
Posts (Atom)
