April 2014 ~ PinoySourceCode

Friday, April 25, 2014

Array in PHP

Suppose we want to store 100 data item. So we need 100 different variable. So this is very typical work for programmer to manage 100 different variable.To overcome this problem we create a special type variable known as array.

array is a special type of variable that store one more values in one single variable. And each element in the array has its own index.

In PHP, there are three kind of arrays:
Numeric array - An array with a numeric index starting 0,1 etc.
Associative array - An array with a string index you can start like sun,mon,tue etc.

Multidimensional array - An array containing one or more arrays

1. we can create an array by array function.

<?php

$data=array("Sun","Mon","Tue","Wed");

?>

2. we can create each array element directly.

<?php

$data[0]="Sun";
$data[1]="Mon";
$data[2]="Tue";
$data[3]="Sun";

?>

A numeric array stores each array element with a numeric index.
In the following example the index are automatically assigned (the index starts at 0):

<?php

$data=array("Sun","Mon","Tue","Wed");

for($i=0;$i<4;$i++)
{
echo $data[$i]."  ";
}
?>
Output:

Sun  Mon  Tue  Web
Read More

PHP Loop

Loop execute some statement many times till the condition is fullfill.

There are three type or loops. these loops do same work execute statement many times.
  • While loop
  • Do While loop
  • For Loop
while loop

while loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a while statement is:
===========================
while (expr)
    statement
===========================
In the while loop it first check the condition then body of the loop will be executed many times untill the condition is fullfill.

Syntax
===========================
while (condition)
 {
  Code here.
 }
===========================
Exercise 1:
===========================
<?php
$i=1;
while($i<=5)
  {
  echo "Number".$i;
  $i++;
  }

?> Output

Number 1

Number 2

Number 3

Number 4

Number 5

do while

Exercise 2:

===========================
<?php

$i=1;
do
  {
  echo "Number".$i;
  $i++;
  }while($i<=5)
?>
Output

Number 1

Number 2

Number 3

Number 4

Number 5
===========================

For loop

In For loop then statement will be excuted till the condition is fullfill.

===========================
for (initilization; condition; increment)
{
  code here...;
}

===========================
Exercise 1:

<?php

for ($i=1; $i<=5; $i++)

  {

  echo "Number".$i;

  }

?>
Output

Number 1
Number 2
Number 3
Number 4
Number 5
Read More

HTML Form tag

When creating textbox. labels,textarea , radio button etc. So this necessary to send these control value to the server.

code:

<form name="formanme" action="action.php" method="post">
your input box, textbox etc..
</form>

name  name of the form
action write page name on which page you want to send values. if you take action blank like action=""               then values send on same page.
method  there are two method post and get.
post send value to the server but values not show on URL.
get send value to the server but values show on URL.

Exercise:

<html>
<head>
<title>Exercise</title>
</head>
<body>
<form name="formanme" action="action.php" method="post">
User Name<input name="username" type="text" /><br />
</form>
</body>
</html>

save this php script and name it of index.php.


In this code we use action="action.php" then these control value send to index.php page We reterive these value on index.php page and can print it. Now make another page action.php for reterive these values

====================================
To Retrieve the value of form
For reterive value of Form tag use super global variable like $_GET and $_POST
$_POST['fieldname']         when method is post on form tag.
$_GET['fieldname']           when method is get on form tag.
====================================

This the code to retrieve the value of the form index.php

<html>
<head>
<title>test1</title>
</head>
<body>
<?php

$user=$_POST['username'];
echo $name;

?>

</body>

</html>

Save this script action.php

How to use get method?

a.php

<html>
<head>
<title>exercise1</title>
</head>
<body>
<form name="frm" action="b.php" method="get">
Enter name:<input name="name" type="text"/><br />
<input name="submit" type="submit" value="Submit" />
</form>
</body>
</html>

v.php

<html>
<head>
<title>exercise1</title>
</head>
<body>

<?php

$name=$_GET['name'];
echo $name;

?>
</body>
</html>

isset()

isset is very usefull function when you send value of the form to the server and reterive value.
isset function is use to check variable exists or not exist.

 This return true if variable exists otherwise this return false.

Why we use isset function and benefit of isset function.


When we send value of the server we check $_POST['fieldname'] in isset function. when we click on submit button then this variable exits on page and write code inside this section. if variable exits our code run otherwise code not run.
 if we are not use isset function then code is run without submit the page to another page.

<?php

if(isset($_POST['submit']))
{
echo $_POST['name'];
}

?>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<form name="" action="" method="post">
<input name="name" type="text"><br>
<input name="submit" type="submit" value="Submit" />

</form>
</body>
</html>
Read More

If else statement in php

if statement is use to check condition if condition is true then body of your code will be  execute.

Syntax
if (condition)
{
Code body
}

Exercise 1:

<?php
$x=20;
if($x>0)
echo "Number is true";
?>

Output
===============

Number is true
===============
if else statement

if else statement is use check the condition, if condition is true then the result will be execute,else if the condition is false.

Syntax

if (condition)
{
Code;
}
else
{
Code;
}

Exercise 2:

<?php

$x=25;
if($x>0)
echo "true";
else
echo “False”;
?>

Output
===============
true
===============

elseif statement

if you have one more condition then use elseif statement.

Syntax:

if (condition)
{
Code;
}
elseif (condition)
{
Code;
}
else
{
Code;
}


Exercise 3:

<?php

$x=25;
$y=45;
if($x>$y)
echo "First Number is Greater";
elseif($x<$y)
echo "Second Number is Greader";
else
echo "Both Number is equal";

?>

Output:
====================
Second Number is Greader
====================
Read More

PHP Variable

Variable is just like a container for data. In PHP, there are two type of variable. Predefine variable(SuperGlobal Variable) and User define Variable.

SuperGlobal Variable
 This is predefine variable and these variables are automatically available to any PHP program.  There is no need to declare and define these variable in the program.And this re the example of superglobal variable

$_GET
$_POST
$_COOKIE
$_SESSION
$_FILES
$_ENV
$_REQUEST
$_SERVER

2. Define Variable
 This variable is declare and define

Example:
$x=34;
$x is a variable and the value of $x is 34 and 34 is a data and this data store in variable $x.
Code to print value of $x

echo $x;

here the echo statement print variable data.
===================
output
30
===================
another statement to print the value of $x:
echo "Number is $x";

output
===================
30
===================

Exercise 1:
<?php
$x=60;
echo $x;
?>
Output
===================
60
===================
Exercise 2:
<?php
$x=20;
echo "Number is $x";
?>
Output
===================
Number is 20
===================


Exercise 3:
<?php
$x=10;
$y=20;
$ans=$x+$y;
echo "Answer is $ans";
?>
Output
===================

Answer is 30
===================
Read More

What is PHP?

What is PHP?

PHP is basically use for web developing dynamic website.
Example of dynamic website are facebook.com,google.com etc.
About Web Development

Website has two type which is Static web site and Dynamic website.

-Static Website
In Static website webpage alway show in the same page content.
we use html and css for create static webpage.you can use static webpage
like company profile website.

-Dynamic Website
In Dynamic website the webpage content change automatically.
We use php with html for create dynamic page.
php work with mysql and change webpage content automatically.
php work with server base you can use xampp or wamp.
Advantage of PHP

PHP is openspurce software. For PHP Software,we can download xampp and wamp.
We can run PHP Script to many Operating system, like Windows,Linux,Unix,mac and more.
We can run PHP Script to many server, like Apache server.
We can use many database, like MySQL.
Download xampp

-Go to you browser
-and search for xampp or wampp or
-Copy and paste this url http://www.apachefriends.org.
To install xampp

-Double click on xampp installer
-After Successfully Install. Open xampp Control Panel.if the control panel not show after installing just go to windows key and search xampp control panel.
-Click on Apache and Mysql and also click on Start in front of Apache and Mysql then close xampp Control Panel.
PHP Tags

This is the correct open and close syntax of php.
<?php
//coding here..
 ?>
This is the sort tag of php
<?
//Coding here
?>
How to write PHP Code

In php you can use any editor software like notepad,wordpad,etc..
Much better to use Dreamweaver for beginners to view the color coding and easy to find the errors.

How to use Dreamweaver?
 1.Download Dreamweaver any version or go to adobe.com
 2Open the software and Double Click PHP.

Now you can use this Code
Simple Script:

<?php
echo "Hello World";
?>
=====================
Output:
Hello world
=====================

after encoding this code save it to htdocs to find htdocs follow this path C://xampp/htdocs
and name it anyfilename.php
Easy way to find htdocs:
Go to my computer and go to drive C: and find xampp folder click this and find htdocs

To run this code:

go to browser google chrome or any browser like firefox etc.
next follow this path localhost/anyfilename.php
Now you can see the output "Hello world"
Read More

Patients With Doctors



Title: Patients With Doctors Recorder
Language : PHP,ajax,javascript
Tags:
     PHP
     Ajax
     Javascript
     Jquery

Features:
  Management
    -Add patient and Doctor
    -Update patient and Doctor
    -Delete patient and Doctor
    -Search patient and Doctor

System Requirements:
  -Xampp 1.7.3 or below
  -Chrome,Firefox,Opera. etc.

How to Install?
 -Download the following files by clicking download below
 -Extract the zip files to htdocs(this is the path: C://xampp/htdocs)

Create Database
 -Go to your browser ans type localhost/phpmyadmin
 -Click Tab Import and Browse the SQL file from lib folder this is the file name (dbdoctor.sql)
Read More

Get updates

Twitter Facebook Google Plus LinkedIn RSS Feed Email

Get Free Updates in Your Email


Enter your email address:

Popular Posts