Showing posts with label PHP-MySQL. Show all posts
Showing posts with label PHP-MySQL. Show all posts

Saturday, July 8, 2017

PHP Control Instruction

PHP Control Instruction

Content

* Conditional Statement
* Loop

Conditional Statements

* Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this
* we have the following conditional statements:

 - if statement - executes some code only if a specified condition is true
 - if...else statement - executes some code if a condition is true and another code if the condition is false
 - if...elseif....else statement - selects one of several blocks of code to be executed
 - switch statement - selects one of many blocks of code to be executed

if statement

if(some condition)
{
 /* code to be executed if condition is true */
}

Example

<html>
<body>
<?php
$t=date("H");
if ($t<"12"&&$t>"5") {
   echo "Good Morning!";
}
?>
</body>
</html>

if-else statement

if(some condition)
{
/* code to be executed if condition is true */
}
else
{
/* code to be executed if condition is false */
}

Example

<html>
<body>
<?php
$x=25;
if ($x%2==0) {
   echo "Even Number";
}
else{
   echo "Odd Number";
}
?>
</body>
</html>

if-else-if….else statement

if(some condition){
/* code to be executed if condition is true */
} else if (some condition){
/* code to be executed if condition is true */
}else if(some condition){
/* code to be executed if condition is true */
}
else {
/* code to be executed if condition is false */
}

Example

<html>
<body>
<?php
$t=date("H");
if ($t<"12") {
   echo "Good Morning";
}else if($t<"17"){
   echo "Good After Noon";
} else if($t<"10"){
   echo "Good After Noon";
}else{
   echo "Good Night";
}
?> 
</body>
</html>

switch statement

switch(n)
{
 case label1:
/* code to be executed if n==label1 */
case label2:
/* code to be executed if n==label2 */
case label3:
/* code to be executed if n==label3 */
case label4:
/* code to be executed if n==label4 */
....
default:
/* code to be executed if n is different from all labels */
}

Example

<html>
<body>
<?php
$country="INDIA";
switch($country){
case "INDIA":
echo "Rupee";
break;
case "USA":
echo "Dollar";
break;
case "JAPAN":
echo "Yen";
break;
case "ENGLAND":
echo "Pound";
break;
default:
 echo "No Currency";
}
?>
</body>
</html>

Loop

* Sometimes, you want the same block of code to run over and over again, we can use loops to perform a task like this
* In PHP, we have the following looping statements:
 - while
 - do while
 - for
 - foreach

while statement

* The while loop executes a block of code as long as the specified condition is true

while(condition)
{
/*Code to be executed*/
}

Example

<html>
<body>
<?php
$x=1;
while($x<=10){
echo $x;
$x++;
}
?>
</body>
</html>

do while statement

* The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true

do
{
/*Code to be executed*/
} while(condition);

Example

<html>
<body>
<?php
$x=1;
do
{
echo $x;
$x++;
} while($x<=10);
?>
</body>
</html>

* Notice that in a do while loop the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition fails the first time.
for  statement
* The for loop is used when you know in advance how many times the script should run.

for(initialization; condition; flow)
{
/*Code to be executed*/
}

* Initialization is assigning initial value to the counter variable. Condition is any expression which will be evaluated as true of false. Flow is any change in counter variable so that loop reaches to its termination.

Example

<html>
<body>
<?php
for($x=1;$x<=10;$x++)
{
echo $x;
}
?>
</body>
</html>



Note: foreach loop will be covered with Arrays.

<<Prev     Home     Next>>

Friday, July 7, 2017

PHP Basics

PHP-MySQL

PHP Basics

Content

* Introduction to PHP 
* Installation
* PHP Syntax
* Saving and Executing PHP page
* Comments
* Variables
* Operators

Introduction to PHP

* PHP is server side scripting language
* Earlier it was Personal Home Page.
* Now a days it is PHP: Hypertext Preprocessor
* PHP is a powerful tool for making dynamic and interactive web pages.
* It was created in 1994 by Rasmus Lerdorf 
* PHP is a language for creating interactive web sites and is used on over 3.3 million web sites around the world. 
* PHP files can contain text, HTML tags and scripts 
* PHP files are returned to the browser as plain HTML. 
* PHP files have a file extension of ".php" 
* PHP can run on different platforms (windows, Linux, Unix, etc)
* PHP is compatible with almost all servers used today (Apache, IIS, etc.)
 
Installation

* You have to install three things
 - PHP
 - MySQL 
 - Apache Server
* Another Option is to install WAMP( W: Windows A:  Apache M: MySQL P: PHP )
* Download WAMP from www.wampserver.com
* WAMP is Windows Apache MySQL PHP

PHP Syntax

* A PHP scripting block always starts 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.
* The PHP script is executed on the server, and the plain HTML result is sent back to the browser.
* Each code line in PHP must end with a semicolon.
* There are two basic statements to output text with PHP: echo and print.
* The file must have a .php extension. 
* If the file has a .html extension, the PHP code will not be executed. 
* Whenever we create a new PHP page, we need to save it in our web servers root directory.
* For WAMP it is a folder called  www folder and is usually at this location on our hard drive:
c:\wamp\www

Saving and Executing PHP page

<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Welcome To PHP";
?>
</body>
</html>

1.Save the previous page by the name "demo.php" in the www folder of wamp.
2.Open the browser.
3.Type the url : " http://localhost/demo.php"
Comments
* Comments:  Comments are place in the same fashion as we do in C or C++. You can use single line comment by using //. And multi-line comment using /*...*/
Variables
* Variables are used to store constants 
* These constants could be strings, numbers or arrays. 
* When a variable is declared, it can be used over and over again in your script. 
* All variables in PHP start with a $ sign symbol


* In PHP, the variable is declared automatically when you use it.
* A variable name must start with an alphabet 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. 
* In PHP, 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

Operators

Concatenation Operator
* There is only one string operator in PHP, it is the concatenation operator (.)  is used to put two string values together.


Arithmetic Operators

Operator  ==> Name  ==> Example  ==> Result
* +  ==> Addition  ==> $x + $y  ==> Sum of $x and $y
* -  ==> Subtraction  ==> $x - $y  ==> Difference of $x and $y
* *  ==> Multiplication  ==> $x * $y  ==> Product of $x and $y
* /  ==> Division  ==> $x / $y  ==> Quotient of $x and $y
* %  ==> Modulus  ==> $x % $y  ==> Remainder of $x divided by $y
* Operator  ==> Name  ==> Description
* ++$x  ==> Pre-increment  ==> Increments $x by one, then returns $x
* $x++  ==> Post-increment  ==> Returns $x, then increments $x by one
* --$x  ==> Pre-decrement  ==> Decrements $x by one, then returns $x
* $x--  ==> Post-decrement  ==> Returns $x, then decrements $x by one


Assignment Operators

Assignment  ==> Same as...  ==> Description
* x = y  ==> x = y  ==> The left operand gets set to the value of the expression on the right
* x += y  ==> x = x + y  ==> Addition
* x -= y  ==> x = x – y  ==> Subtraction
* x *= y  ==> x = x * y  ==> Multiplication
* x /= y  ==> x = x / y  ==> Division
* x %= y  ==> x = x % y  ==> Modulus

Relational Operators

Operator  ==> Name  ==> Example  ==> Result
* ==  ==> Equal  ==> $x == $y  ==> True if $x is equal to $y
* ===  ==> Identical  ==> $x === $y  ==> True if $x is equal to $y, and they are of the same type
* !=  ==> Not equal  ==> $x != $y  ==> True if $x is not equal to $y
* <>  ==> Not equal  ==> $x <> $y  ==> True if $x is not equal to $y
* !==  ==> Not identical  ==> $x !== $y  ==> True if $x is not equal to $y, or they are not of the same type
* >  ==> Greater than  ==> $x > $y  ==> True if $x is greater than $y
* <  ==> Less than  ==> $x < $y  ==> True if $x is less than $y
* >=  ==> Greater than or equal to  ==> $x >= $y  ==> True if $x is greater than or equal to $y
* <=  ==> Less than or equal to  ==> $x <= $y  ==> True if $x is less than or equal to $y

Logical Operators

Operator  ==> Name  ==> Example  ==> Result
* and  ==> And  ==> $x and $y  ==> True if both $x and $y are true
* or  ==> Or  ==> $x or $y  ==> True if either $x or $y is true
* xor  ==> Xor  ==> $x xor $y  ==> True if either $x or $y is true, but not both
* &&  ==> And  ==> $x && $y  ==> True if both $x and $y are true
* ||  ==> Or  ==> $x || $y  ==> True if either $x or $y is true
* !  ==> Not  ==> !$x  ==> True if $x is not true


<<Prev     Home     Next>>

Thursday, July 6, 2017

Menu Designing

Menu Designing

Content
* Menu
* Sample Menu Designing Example

Menu

* Menu in any web application facilitate easy navigation to the user
* Content of your web application is categorized and displayed in different pages. To access these pages you design menu as links to web pages.
* Menu can be designed in many ways, in fact it is a matter of designing and innovation.
* Fancy menu may use HTML, CSS and javascript.
* Go through the following examples and understand the basics. 
 
Sample Menu Designing Example

Example 1

menu.html

<html>
 <head>
  <link rel="stylesheet" href="menu.css"/>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
   $(document).ready(function(){
    $("ul li").hover(
     function(){
         $("ul",this).css("display","block");
         $("a",this).css("color","red");
   $("a",this).css("background-color","lightblue");
  $("ul li a",this).css("background-color","white");
         $("ul li a",this).css("color","#777");
              },
     function(){
       $("ul",this).css("display","none");
       $("a",this).css("color","#777");
       $("a",this).css("background-color","white");
    });
   });
  </script>
 </head>
 <body>
  <ul id="ul1"> 
    <li class="li1"><a class="a1" href="#">Home</a></li> 
    <li class="li1"><a class="a1" href="#">Courses</a> 
      <ul> 
        <li><a class="a2" href="#">C/C++</a></li> 
        <li><a class="a2" href="#">PHP</a></li> 
        <li><a class="a2" href="#">Java</a></li> 
      </ul> 
    </li> 
    <li class="li1"><a class="a1" href="#">Services</a> 
      <ul> 
        <li><a class="a2" href="#">Web Design</a></li> 
        <li><a class="a2" href="#">Internet Marketing</a></li> 
        <li><a class="a2" href="#">Hosting</a></li> 
        <li><a class="a2" href="#">Domain Names</a></li> 
        <li><a class="a2" href="#">Broadband</a></li> 
      </ul> 
    </li>
    <li class="li1"><a class="a1" href="#">Contact Us</a> 
      <ul> 
        <li><a class="a2" href="#">Bhopal</a></li> 
        <li><a class="a2" href="#">Indore</a></li> 
        <li><a class="a2" href="#">Bangalore</a></li> 
        <li><a class="a2" href="#">Hydrabad</a></li> 
      </ul> 
    </li> 
  </ul>
 </body>
</html>

menu.css

body
{
        background-color:lightgray;
}
#ul1,ul 
{
  position:relative;
 list-style: none;
}
#ul1 .li1
{
        display:table-cell;
        width:150px;
}
li ul li
{
 position:relative;
 left:-40;
}
ul li ul
{
 position:absolute; 
 display:none;
 border:2px solid black;
 background-color:white;
 width:110px;
}
.a1
{
 background-color: #fff;
 border: 1px solid #ccc;
}
.a2
{
 width:140;
}
ul li a 
{
 display:block;
 text-decoration: none;
 color: #777;
 padding: 5px;
 border-bottom: 0;
        margin:0;
}

Example 2

menu.html

<html>
 <head>
  <link rel="stylesheet" href="menu.css"/>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
   $(document).ready(function(){
    $("#ul1 li").hover(
     function(){
         $("ul",this).css("display","block");
         $("a",this).css("color","red");
   $("a",this).css("background-color","lightblue");
  $("ul li a",this).css("background-color","white");
         $("ul li a",this).css("color","#777");
              },
     function(){
       $("ul",this).css("display","none");
       $("a",this).css("color","#777");
       $("a",this).css("background-color","white");
    });
   });
  </script>
 </head>
 <body>
  <ul id="ul1"> 
    <li><a href="#">Home</a></li> 
    <li><a href="#">Courses</a> 
      <ul> 
        <li><a href="#">C/C++</a></li> 
        <li><a href="#">PHP</a></li> 
        <li><a href="#">Java</a></li> 
      </ul> 
    </li> 
    <li><a href="#">Services</a> 
      <ul> 
        <li><a href="#">Web Design</a></li> 
        <li><a href="#">Internet 
            Marketing</a></li> 
        <li><a href="#">Hosting</a></li> 
        <li><a href="#">Domain Names</a></li> 
        <li><a href="#">Broadband</a></li> 
      </ul> 
    </li>
    <li><a href="#">Contact Us</a> 
      <ul> 
        <li><a href="#">Bhopal</a></li> 
        <li><a href="#">Indore</a></li> 
        <li><a href="#">Bangalore</a></li> 
        <li><a href="#">Hydrabad</a></li> 
      </ul> 
    </li> 
  </ul>
 </body>
</html>

menu.css

body
{
        background-color:lightgray;
}
ul 
{
 margin: 0;
 padding: 0;
 list-style: none;
 width: 150px;
}
ul li 
{
 position: relative;
}
li ul 
{
 position: absolute;
 left: 149px;
 top: 0;
 display: none;
}
ul li a 
{
 display: block;
 text-decoration: none;
 color: #777;
 background: #fff;
 padding: 5px;
 border: 1px solid #ccc;
 border-bottom: 0;
}





<<Prev     Home     Next>>

Wednesday, July 5, 2017

JQuery

JQuery

Content
* Introduction to Jquery
* jquery Syntax
* Document ready event
* Jquery Events
* Jquery Effects
* Handling css through jquery


Introduction to jquery

* JQuery is a JavaScript library. 
* It is a library of JavaScript functions. 
* jQuery is a fast, small, and feature-rich JavaScript library
* It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
* The jQuery library is stored as a single JavaScript file, containing all the jQuery methods. 
* You need to download jquery.js first.

<head>
 <script type="text/javascript" src="jquery.js">   </script>
</head> 

Jquery Syntax

* The jQuery syntax is tailor made for selecting HTML elements and performing some action on the element(s).

$(selector).action()

* $ sign defines JQuery. 
* Selector is HTML element. 
* Action is a function to be performed.
* jQuery selectors allow you to select HTML elements (or groups of elements) by tag name, class name or by Id.

Example

$(this).hide()      //Hides the current element
$("img").hide() //Hide all images
$(".i").hide()      //Hide all html elements with class attribute value is 'i'.
$("#i1").hide() //Hide an html element whose id attribute is i1.

Document ready event

* All JQuery code written inside ready() method, this is to prevent any JQuery code from running before the document is finished loading. 

$(document).ready(function(){
//JQuery code here
}); 

Jquery Events

* All the different visitor actions that a web page can respond to are called events.
* An event represents the precise moment when something happens
* For example - moving a mouse over an element, selecting a radio button, selecting an element, clicking a button, etc
* Some of the jquery methods representing events are:
 - click  
 - dblclick
 - mouseenter
 - mouseleave
 - hover
 - mousemove
 - keypress
 - keydown
 - keyup
 - submit
 - change
 - focus
 - blur
 - load
 - resize
 - scroll
 - unload
* To assign a click event to a particular button on a page, you can do this
 - $("#button1").click();
* The next step is to define what should happen when the event fires
 - $("#button1").click(function(){
//your action code here
});


Jquery Effects

* There are several jquery methods that can be used to represent effects or actions.
 - animate
 - delay
 - fadeIn
 - fadeout
 - fadeTo
 - hide
 - show
 - slideDown
 - slideUp
 - toggle

Example

<html>
<head>
<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
  $("#hide").click(function(){
    $("p").hide();
  });
  $("#show").click(function(){
    $("p").show();
  });
});
</script>
</head>
<body>
<p>If you click on the "Hide" button, I will disappear.</p>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>

Example

<html>
<head>
 <script type="text/javascript" src="jquery.js">
 </script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("p").toggle(1000);
    if($(this).html()=="Hide")
        $(this).html("Show");
    else
        $(this).html("Hide");  });
});
</script>
</head>
<body>
<button>Hide</button>
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</body>
</html>

Handling CSS through Jquery

* Just like javascript, we can manipulate css through jquery. 
* We use css() method, with two string arguments. First argument is property name and second argument is property value. 

Example: Change background when mouse move over a paragraph

<html>
<head>
 <script type="text/javascript" src="jquery.js">
 </script>
<script type="text/javascript">
$(document).ready(function(){
  $("p").hover(function(){
     $("this").css("background-color", "lightblue");
    }, function(){
     $("this").css("background-color", "white");
});
</script>
</head>
<body>
<p>This is first paragraph.</p>
<p>This is second paragraph.</p>
</body>
</html>

<<Prev     Home     Next>>