web learning

HTML

CSS

PHP

JAVA SCRIPT

AJAX

EBOOKS

PROJECT

FORUM

Read More …


CSS Rollover Lists (Horizontal Menu)
1.
Make a basic list using a href  property

Example

<div id="navigation">
    <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Countries</a></li>
            <li><a href="#">People</a></li>
            <li><a href="#">About Us</a></li>
            <li><a href="#">Contact</a></li>
     </ul>
</div>


Output

·         Home
·         Countries
·         People
·         About Us
·         Contact

2.
To remove the HTML bullets set the “ list-style-type” to “none

CSS Code
#navigation ul {
    list-style-type: none;
}

Output

Home
Countries
People
About Us
Contact
3.
Remove paddings and margins

CSS Code
#navigation ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
}

Output

Home
Countries
People
About Us
Contact
4.
In order to get the list items in one line we use  “inline” property for “a” element

CSS Code
#navigation ul {
    list-style-type: none;
    padding: 0;
    margin: 0;
}

#navigation ul li {
    display: inline;
}



Output

Description: HM-4.png
5.
Remove the text underline. Navigations do not have underlines. For this set the “text-decoration” property of the “a” element as none.

CSS Code
#navigation ul li {
    display: inline;
}

#navigation ul li a {
    text-decoration: none;
}

Output
Description: HM-5.png

6.
Add padding around list items so that it looks like they are inside a box.

CSS Code
#navigation ul li {
    display: inline;
}

#navigation ul li a {
    text-decoration: none;
    padding:  .2em 1em;
}

Output
Description: HM-6.png

7.
Add background color  to the list items. Also change the text color to make it readable.

CSS Code
#navigation ul li a {
    text-decoration: none;
    padding:  .2em 1em;
    color: #fff;
    background-color: #036;
}

Output
Description: HM-7.png

8.
Add rollover color to the lists. Use “a:hover” to set a second background color, as a rollover state.


CSS Code
#navigation ul li a {
    text-decoration: none;
    padding:  .2em 1em;
    color: #fff;
    background-color: #036;
}

#navigation ul li a:hover {
      color: #fff;
      background-color: #369;
}

Output
Description: HM-8.png


Read More …


<code><ul id="nav">
    <li>
        <a href="#">Home</a>
    </li>

    <li>
        <a href="#">About</a>
        <ul>
            <li><a href="#">The product</a></li>

            <li><a href="#">Meet the team</a></li>
        </ul>
    </li>
    <li>
        <a href="#">Services</a>

        <ul>
            <li><a href="#">Sevice one</a></li>
            <li><a href="#">Sevice two</a></li>

            <li><a href="#">Sevice three</a></li>
            <li><a href="#">Sevice four</a></li>
        </ul>

    </li>
    <li>
        <a href="#">Product</a>
        <ul>
            <li><a href="#">Small product (one)</a></li>

            <li><a href="#">Small product (two)</a></li>
            <li><a href="#">Small product (three)</a></li>
            <li><a href="#">Small product (four)</a></li>

            <li><a href="#">Big product (five)</a></li>
            <li><a href="#">Big product (six)</a></li>
            <li><a href="#">Big product (seven)</a></li>

            <li><a href="#">Big product (eight)</a></li>
            <li><a href="#">Enourmous product (nine)</a></li>
            <li><a href="#">Enourmous product (ten)</a></li>

            <li><a href="#">Enourmous product (eleven)</a></li>
        </ul>
    </li>
    <li>
        <a href="#">Contact</a>

        <ul>
            <li><a href="#">Out-of-hours</a></li>
            <li><a href="#">Directions</a></li>

        </ul>
    </li>
</ul></code>

The CSS

This is where the magic happens--we use CSS to transform a series of nested <ul>s into a smooth, easy to use, neat and self-contained dropdown menu.
<code><span class="code-comment">/* BE SURE TO INCLUDE THE CSS RESET FOUND IN THE DEMO PAGE'S CSS */</span>
<span class="code-comment">/*------------------------------------*\
    NAV
\*------------------------------------*/</span>
#nav{
    list-style:none;
    font-weight:bold;
    margin-bottom:10px;
    <span class="code-comment">/* Clear floats */</span>
    float:left;
    width:100%;
    <span class="code-comment">/* Bring the nav above everything else--uncomment if needed.
    position:relative;
    z-index:5;
    */</span>
}
#nav li{
    float:left;
    margin-right:10px;
    position:relative;
}
#nav a{
    display:block;
    padding:5px;
    color:#fff;
    background:#333;
    text-decoration:none;
}
#nav a:hover{
    color:#fff;
    background:#6b0c36;
    text-decoration:underline;
}

<span class="code-comment">/*--- DROPDOWN ---*/</span>
#nav ul{
    background:#fff; <span class="code-comment">/* Adding a background makes the dropdown work properly in IE7+. Make this as close to your page's background as possible (i.e. white page == white background). */</span>
    background:rgba(255,255,255,0); <span class="code-comment">/* But! Let's make the background fully transparent where we can, we don't actually want to see it if we can help it... */</span>
    list-style:none;
    position:absolute;
    left:-9999px; <span class="code-comment">/* Hide off-screen when not needed (this is more accessible than display:none;) */</span>
}
#nav ul li{
    padding-top:1px; <span class="code-comment">/* Introducing a padding between the li and the a give the illusion spaced items */</span>
    float:none;
}
#nav ul a{
    white-space:nowrap; <span class="code-comment">/* Stop text wrapping and creating multi-line dropdown items */</span>
}
#nav li:hover ul{ <span class="code-comment">/* Display the dropdown on hover */</span>
    left:0; <span class="code-comment">/* Bring back on-screen when needed */</span>
}
#nav li:hover a{ <span class="code-comment">/* These create persistent hover states, meaning the top-most link stays 'hovered' even when your cursor has moved down the list. */</span>
    background:#6b0c36;
    text-decoration:underline;
}
#nav li:hover ul a{ <span class="code-comment">/* The persistent hover state does however create a global style for links even before they're hovered. Here we undo these effects. */</span>
    text-decoration:none;
}
#nav li:hover ul li a:hover{ <span class="code-comment">/* Here we define the most explicit hover states--what happens when you hover each individual link. */</span>
    background:#333;
}</code>
Read More …

Read More …


filetype: comments.html


<html>
<head>
<title>comment box</title>
</head>

<body>
<form action="sabincomment.php" method="post">
name:<input type="text" id="name" name="name"><br>
comment:<textarea id="comment"name="comment" rows=4 cols=60>your text here</textarea>
<input type="submit" id="submit" name="submit" value="comment">
</form>
http://webstufflearning.blogspot.com/
</body>
</html>



filetype:sabincomment.php


<?
$name=$_POST['name'];
$comment=$_POST['comment'];
$con=mysql_connect("localhost","root","");
mysql_select_db("share",$con);
$sql="INSERT INTO comment(name , comment) VALUES ('".$name."','".$comment."')";
$result=mysql_query($sql);
mysql_close();
if($result){
header('location:http://localhost/comments/display.php');
}

?>

http://webstufflearning.blogspot.com/






filetype:display.php


<html>
<head>
<title>comment box</title>
</head>
<body>
<?
$con=mysql_connect("localhost","root","");
mysql_select_db("share",$con);
$sql="SELECT * from comment";
$result=mysql_query($sql);
mysql_close();
while($row=mysql_fetch_array($result)){
$name=$row['name'];
$comment=$row['comment'];
echo "$name:<br>$comment:";}
?>
http://webstufflearning.blogspot.com/
</body>
</html>





Read More …


<?php
$text = $_GET['Comments']; 



mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
mysql_select_db("databasename") or die ('Data error:' . mysql_error());

$query="INSERT INTO KeepData (player_data)VALUES ('$text')";

mysql_query($query) or die ('Error updating database' . mysql_error());




?> 



 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <textarea id="Comments" name="Comments">
 example text
 </textarea>
<input type="submit" name="mysubmit" value="Save Post" />
 </form>
Read More …

Web Database Applications with PHP \& MySQL

Chapter 2. PHP

In this chapter, we introduce the PHP scripting language. PHP is similar to high-level languages such as C, Perl, Pascal, FORTRAN, and Java, and programmers who have experience with any of these languages should have little trouble learning PHP. This chapter serves as an introduction to PHP; it's not a programming guide. We assume you are already familiar with programming in a high-level language.
The topics covered in this chapter include:
Programmers new to PHP should read Section 2.1, which describes the basic structure of a PHP script and its relationship to HTML, and includes discussion of how PHP handles variables and types. The two sections that follow, Section 2.2 and Section 2.3, deal with conditional statements and looping structures and should be familiar material. We then present a short example that puts many of the basic PHP concepts together.
The remainder of the chapter expands on the more advanced features of PHP, presents a reference to selected library functions, and discusses some of the common mistakes that programmers make when learning PHP. This material can be examined briefly, and used later as a reference while reading Chapter 4 to 13 and while programming in PHP. However, programmers new to PHP should consider reading the beginning of the Section 2.5 and Section 2.6 sections to understand the way PHP supports these concepts, as there are important differences from other languages.
We don't attempt to cover every function and every library that are supported by PHP. However, we provide brief descriptions of the supported libraries in Appendix E. In later chapters, we discuss more specialized library functions that support the topics and techniques presented here.

2.1. Introducing PHP

The current version of PHP is PHP4, which we call PHP throughout this book. The current release at the time of writing is 4.0.6.
PHP is a recursive acronym that stands for PHP: Hypertext Preprocessor; this is in the naming style of GNU, which stands for GNU's Not Unix and which began this odd trend. The name isn't a particularly good description of what PHP is and what it's commonly used for. PHP is a scripting language that's usually embedded or combined with HTML and has many excellent libraries that provide fast, customized access to DBMSs. It's an ideal tool for developing application logic in the middle tier of a three-tier application.

2.1.1. PHP Basics

Example 2-1 shows the first PHP script in this book, the ubiquitous "Hello, world." When requested by a web browser, the script is run on the web server and the resulting HTML document sent back to the browser and rendered as shown in Figure 2-1.
Figure 2-1

Figure 2-1. The rendered output of Example 2-1 shown in the Netscape browser

Example 2-1. The ubiquitous Hello, world in PHP

<!DOCTYPE HTML PUBLIC
   "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
  <title>Hello, world</title>
</head>
<body bgcolor="#ffffff">
  <h1>
  <?php
    echo "Hello, world";
  ?>
  </h1>
</body>
</html>
Example 2-1 illustrates the basic features of a PHP script. It's a mixture of HTML—in this case it's mostly HTML—and a PHP script. The script in this example:
<?php
  echo "Hello, world";
?>
simply prints the greeting, "Hello, world."
The PHP script shown in Example 2-1 is rather pointless: we could simply have authored the HTML to include the greeting directly. Because PHP integrates so well with HTML, using PHP to produce static strings is far less complicated and less interesting than using other high-level languages. However, the example does illustrate several features of PHP:
  • The begin and end script tags are <?php and ?> or, more simply, just <? and ?>. The longer begin tag style <?php avoids conflicts with other processing instructions that can be used in HTML. We use both styles in this book.
    Other begin and end tag styles can also be configured, such as the HTML style that is used with JavaScript or other embedded scripts: <script language="PHP"> and </script>.
  • Whitespace has no effect, except to aid readability for the developer. For example, the script could have been written succinctly as <?php echo "Hello, world";?> with the same effect. Any mix of spaces, tabs, carriage returns, and so on in separating statements is allowed.
  • A PHP script is a series of statements, each terminated with a semicolon. Our simple example has only one statement: echo "Hello, world";.
  • A PHP script can be anywhere in a file and interleaved with any HTML fragment. While Example 2-1 contains only one script, there can be any number of PHP scripts in a file.
  • When a PHP script is run, the entire script including the start and end script tags <?php and ?> is replaced with the output of the script.
TIP: When we present a few lines of code that are sections of larger scripts, we usually omit the start and end tags.
The freedom to interleave any number of scripts with HTML is one of the most powerful features of PHP. A short example is shown in Example 2-2; a variable, $outputString="Hello, world", is initialized before the start of the HTML document, and later this string variable is output twice, as part of the <title> and <body> elements. We discuss more about variables and how to use them later in this chapter.

Example 2-2. Embedding three scripts in a single document

<?php $outputString = "Hello, world"; ?>
<!DOCTYPE HTML PUBLIC 
   "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
  <title><?php echo $outputString; ?></title>
</head>
<body bgcolor="#ffffff">
  <h1><?php echo $outputString; ?></h1>
</body>
</html>
The flexibility to add multiple scripts to HTML can also lead to unwieldy, hard-to-maintain code. Care should be taken in modularizing code and HTML; we discuss how to separate code and HTML using templates in Chapter 13.

2.1.1.1. Creating PHP scripts

PHP script can be written using plain text[4] and can be created with any text editor, such as joevineditemacs, or pico.
[4]While printable characters with the most significant bit are allowed, PHP scripts are usually written using characters from the 7-bit ASCII character set.
If you save a PHP script in a file with a .php extension under the directory configured as Apache's document root, Apache executes the script when a request is made for the resource. Following the installation instructions given in Appendix A, the document root is:
/usr/local/apache/htdocs/
Consider what happens when the script shown in Example 2-1 is saved in the file:
/usr/local/apache/htdocs/example.2-1.php
Apache—when configured with the PHP module—executes the script when requests to the URL http://localhost/example.2-1.php are made, assuming the web browser is running on the same machine as the web server.
If directory permissions don't permit creation of files in the document root, it's also possible to work in the user home directories. If the installation instructions in Appendix A have been followed, a directory can be created by a user beneath her home directory and the permissions set so that the directory is readable by the web server:
mkdir ~/public_html
chmod a+rx ~/public_html
The example file can then be created with the filename:
~/public_html/example.2-1.php
The file can then be retrieved with the URL http://localhost/~user/example.2-1.php, where user is the user login name.

2.1.3. Types

PHP has four scalar types—boolean, float, integer, and string—and two compound types, array and object.
TIP: In this book, and particularly in this chapter, we present function prototypes that specify the types of arguments and return values. There are many functions that allow arguments or return values to be of different types, which we describe as mixed.
Variables of a scalar type can contain a single value at any given time. Variables of a compound type—array or object—are made up of multiple scalar values or other compound values. Arrays and objects have their own sections later in this chapter. Other aspects of variables—including global variables and scope—are discussed later, with user-defined functions.Boolean variables are as simple as they get: they can be assigned either true or false. Here are two example assignments of a Boolean variable:
$variable = false;
$test = true;
An integer is a whole number, while a float is a number that has an exponent and a fractional part. The number 123.01 is a float, and so is 123.0. The number 123 is an integer. Consider the following two examples:
// This is an integer
$var1 = 6;

// This is a float
$var2 = 6.0;
A float can also be represented using an exponential notation:
// This is a float that equals 1120
$var3 = 1.12e3;

// This is also a float that equals 0.02
$var4 = 2e-2
You've already seen examples of strings earlier, when echo( ) and print( ) were introduced, and string literals are covered further in Section 2.6. Consider two example string variables:
$variable = "This is a string";
$test = 'This is also a string';

2.1.5. Expressions, Operators, and Variable Assignment

We've already described simple examples of assignment, in which a variable is assigned the value of an expression using an equals sign. Most numeric assignments and expressions that work in other high-level languages also work in PHP. Here are some examples:
// Assign a value to a variable
$var = 1;

// Sum integers to produce an integer
$var = 4 + 7;

// Subtraction, multiplication, and division
// that might have a result that is a float or 
// an integer, depending on the initial value of $var
$var = (($var - 5) * 2) / 3;

// These all add 1 to $var
$var = $var + 1;
$var += 1;
$var++;

// And these all subtract 1 from $var
$var = $var - 1;
$var -= 1;
$var--;

// Double a value
$var = $var * 2;
$var *= 2;

// Halve a value
$var = $var / 2;
$var /= 2;

// These work with float types too
$var = 123.45 * 28.2;
There are many mathematical functions available in the math library of PHP for more complex tasks. We introduce some of these in Section 2.9.
String assignments and expressions are similar:
// Assign a string value to a variable
$var = "test string";

// Concatenate two strings together
// to produce "test string"
$var = "test" . " string";

// Add a string to the end of another
// to produce "test string"
$var = "test";
$var = $var . " string";

// Here is a shortcut to add a string to
// the end of another
$var .= " test";

2.1.5.1. Expressions

Expressions in PHP are formulated in much the same way as other languages. An expression is formed from literal values (integers, strings, floats, Booleans, arrays, and objects), operators, and function calls that return values. An expression has a value and a type; for example, the expression 4 + 7 has the value 11 and the type integer, and the expression "Kelpie" has the value Kelpie and the type string. PHP automatically converts types when combining values in an expression. For example, the expression 4 + 7.0 contains an integer and a float; in this case, PHP considers the integer as a floating-point number, and the result is a float. The type conversions are largely straightforward; however, there are some traps, which are discussed later in this section.

2.1.6. Type Conversion

PHP provides several mechanisms to allow variables of one type to be considered as another type. Variables can be explicitly converted to another type with the following functions:
string strval(mixed variable)
integer intval(mixed variable)
float floatval(mixed variable)
The function settype(mixed variable, string type) can explicitly set the type of variable to type, where type is again one of array, boolean, float, integer, object, or string.
PHP supports type-casting in much the same way as C, to allow the type of an expression to be changed. By placing the type name in parentheses in front of a variable, PHP converts the value to the desired type:
(int) $var
 or (integer) $var
Cast to integer
(bool) $var
 or (boolean) $var
Cast to Boolean
(float) $var(double) $var
 or (real) $var
Cast to float
(string) $varCast to string
(array) $varCast to array
(object) $varCast to object
The rules for converting types are mostly common sense, but some conversions may not appear so straightforward. Table 2-1 shows how various values of $var are converted using the (int)(bool),(string), and (float) casting operators.

Table 2-1. Examples of type conversion in PHP using casting operators

Value of $var(int) $var(bool) $var(string) $var(float) $var
null
0
false
""
0
true
1
true
"1"
1
false
0
false
""
0
0
0
false
"0"
0
3.8
3
true
"3.8"
3.8
"0"
0
false
"0"
0
"10"
10
true
"10"
10
"6 feet"
6
true
"6 feet"
6
"foo"
0
true
"foo"
0

2.1.6.1. Automatic type conversion

Automatic type conversion occurs when two differently typed variables are combined in an expression or when a variable is passed as an argument to a library function that expects a different type. When a variable of one type is used as if it were another type, PHP automatically converts the variable to a value of the required type. The same rules are used for automatic type conversion as are demonstrated inTable 2-1.
Some simple examples show what happens when strings are added to integers and floats and when strings and integers are concatenated:
// $var is set as an integer = 115
$var = "100" + 15;

// $var is set as a float = 115.0
$var = "100" + 15.0;

// $var is set as a string = "39 Steps"
$var = 39 . " Steps";
Not all type conversions are so obvious and can be the cause of hard-to-find bugs:
// $var is set as an integer = 39
$var = 39 + " Steps";

// $var is an integer = 42
$var = 40 + "2 blind mice";

// $var is a float, but what does it mean
$var = "test" * 4 + 3.14159;
Automatic type conversion can change the type of a variable. Consider the following example:
$var = "1"; // $var is a string == "1"
$var += 2;  // $var is now an integer == 3
$var /= 2;  // $var is now a float == 1.5
$var *= 2;  // $var is still a float == 3
WARNING: Care must be taken when interpreting non-Boolean values as Boolean. Many library functions in PHP return values of different types: false if a valid result could not be determined, or a valid result. A valid return value of 00.0"0", an empty string, null, or an empty array is interpreted false when used as a Boolean value.The solution is to test the type of the variable using the functions described in the next section.

2.1.7. Examining Variable Type and Content

Because PHP is flexible with types, it provides the following functions that can check a variable's type:
boolean is_int(mixed variable)
boolean is_float(mixed variable)
boolean is_bool(mixed variable)
boolean is_string(mixed variable)
boolean is_array(mixed variable)
boolean is_object(mixed variable)
All the functions return a Boolean value of true or false for the variable variable, depending on whether it matches the variable type that forms the name of the function. For example, the following prints 1, that is, true:
$test = 13.0;
echo is_float($test); // prints 1 for true

2.1.7.2. Testing, setting, and unsetting variables

During the running of a PHP script, a variable may be in an unset state or may not yet be defined. PHP provides the isset( ) function and the empty( ) language construct to test the state of variables:
boolean isset(mixed var)
boolean empty(mixed var)
isset( ) tests if a variable has been set with a non-null value, while empty( ) tests if a variable has a value. The two are different, as shown by the following code:
$var = "test";

// prints: "Variable is Set"
if (isset($var)) echo "Variable is Set";

// does not print
if (empty($var)) echo "Variable is Empty";
A variable can be explicitly destroyed using unset( ):
unset(mixed var [, mixed var [, ...]])
After the call to unset in the following example, $var is no longer defined:
$var = "foo";

// Later in the script
unset($var);

// Does not print
if (isset($var)) echo "Variable is Set";
Another way to test that a variable is empty is to force it to the Boolean type using the (bool) cast operator discussed earlier. The example interprets the $var variable as type Boolean, which is equivalent to testing for !empty($var):
$var = "foo";

// Both lines are printed
if ((bool)$var)    echo "Variable is not Empty";
if (!empty($var))  echo "Variable is not Empty";
Table 2-2 show the return values for isset($var)empty($var), and (bool)$var when the variable $var is tested. Some of the results may be unexpected: when $var is set to "0"empty( ) returnstrue.

Table 2-2. Expression values

State of the variable $varisset($var)empty($var)(bool)$var
$var = null;  
false
true
false
$var = 0; 
true
true
false
$var = true
true
false
true
$var = false
true
true
false
$var = "0";
true
true
false
$var = "";  
true
true
false
$var = "foo";
true
false
true
$var = array( );
true
true
false
unset $var;
false
true
false


Library Navigation Links
Copyright © 2003 O'Reilly & Associates. All rights reserved.
Read More …