|
Removing undeline from links
June 24, 2008 – 6:38 pmWhenever we create a href then an underline comes by default for the link. We can remove this underline from the anchored text link by adding inline style to the a tag. Example is given below
<a href="contact.php">Contact us</a> <!-- this produce the link with underline -->
<a href="contact.php" style="text-decoration:none;">Contact us</a> <!-- this produce the link without underline -->
we can also define a class for removing underlines from all the anchored occurrences
<style type="text/css">
a.nounderline {text-decoration:none;}
</style>
<a href="contact.php" class="nounderline">Contact us</a>
Different types of CSS styles
June 24, 2008 – 6:30 pmCSS gives flexibility in using different style property locally by overriding the global declared styles in external style sheet. Following are the 3 different types of styles.
- Inline or embedded style sheet
- Internal style sheet
- External style sheet
Inline or embedded style sheet
These styles can be added directly within HTML tags. This gets highest priority than Internal and external defined styles. It means it overrides the existing style and applies the inline style.
<style type="text/css">
h2 {background-color:#ffff00;} /*Internal style sheet*/
</style>
<h2 style="background-color: #cccccc;">Heading tag</h2> <!-- inline style sheet -->
The above code outputs the heading tag with background color #cccccc by overriding the color #ffff00.
Internal Style Sheets
This type of style is defined inside the body tag of the page. The style defined here get overrides the external styles. Here is an example of Internal styles.
<head>
<title>Page title</title>
<style type="text/css">
p {
font-family: verdana, arial, sans-serif;
background-color: #cccccc;
}
</style>
</head>
<body >
<p> This paragraph is having font family verdana with background color #cccccc</p>
</body>
External Style sheets
These styles are created in some another file and are linked from the required page.
<link rel="stylesheet" href="css/style.css" type="text/css">
The above tag is to be placed within the head tags of the page. here style.css will have all the styles for different tags and classes.
Script to access MySQL database using PHP
June 22, 2008 – 8:51 amPHP and MySQL are most popular combination in creating web application. PHP and MySQL gets integrated easily in any platforms. Scripts developed in PHP using Window platform works perfectly in when used in Linux environment. Below example shows how to connect between PHP and MySQL database.
<?php
// Connecting to database
$link = mysql_connect('mysqlHost', 'mysqlUser', 'mysqlPassword')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
// Selecting to database
mysql_select_db('myDatabase') or die('Could not select database');
// Performing SQL query
$query = 'SELECT * FROM myTable';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table width='400'>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
echo "\t\t<td>$line['colname1']</td><td>$line['colname1']</td>\n";
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
The above example shows to display the contents of the MySQL table. Here mysql_connect function is used to connect to the mysql using servername, username and userpassword parameters and returns the link identifier to that MySQL server. mysql_select_db function selects the database to run the queries and finally mysql_query runs the query and stores the result set in the $result variable.
Now using mysql_fetch_array or other functions we can make the operations on the resultant datas.
What is PHP?
June 22, 2008 – 8:25 amPHP is a server side scripting language, widely used in web programming to build web applications. PHP can be used to develop dynamic content pages. Using PHP we can send request and get response or interact with database servers and manage the page content accordingly.
How PHP works
To run the PHP we need a web server like apache or IIS. Whenever a request comes, then web server runs the PHP engine and the output of the executed script is sent back to the user. Depending on the various input conditions the page content can be changed. PHP can easily connect to MySQL database and generate the content accordingly.
Different methods for passing values from one page to another page
June 21, 2008 – 12:44 pmWe often have to pass values of variables between pages in our web site. These are required in many different types. Some time we gather few values from database and want to retain the value throughout the site for some user as the user moves between pages. There are different methods for passing such values of the variables. It can be passed within a site pages or even outside the site. following are few examples.
Passing values using session between pages
This is one of the secured ways to pass the values between pages. The best example of such a system is when we see our user details inside the member section after we logged in. In the member login system a new session gets created if the given details are correct. These value is stored at server end. Whenever a new page is requested by the same user, the server checks the for existing session value and gives the access to that page. This system is more secure and the member doesn’t get any chance to change the values.
Passing values using cookies between pages
Cookies are stored at the user/ client side and values can be passed between pages. Here the client browser can have the option of reject or accept the cookies by changing the security settings of the browser. Sometime this system can fail to pass values between pages if user or client end settings are changed.
Passing values using URL between pages
We can pass values between pages using the URL. These values are visible to the user and others as they appear in the URL. This is not a secure way to transfer important data like password, etc.
Passing values using web forms
Forms are used mainly to collect data and transfer these data to a processing page. Here the processing page uses one of the above methods to pass values to different pages as per the requirements. Forms can be used to pass values to different sites. we can access the data using $_GET or $_POST methods depending the form method
Disable browser back button using javascript
June 8, 2008 – 11:03 amSome times after filling the form, user oftenly clicks on back button to go back. We can disable this back button using javascript in the following way
<script language=”JavaScript”>
<!–
history.go(+1);
// –>
</script>
<script>
javascript:window.history.forward(1);
</script>
This script sends the user to the same page where he/she is clicking the back button. In another way its just disables the back button
Display total rows count with LIMIT clause in MySQL
June 8, 2008 – 10:46 amAssume there are lots of records in result set and want to display only few records. This concept is known as the pagination.
The sql query to achieve above is, as follows,
select * from books where bookcost > 3oo order by bookcost limit 0, 25;
The above query will check the condition and returns only 25 records out of hundreds of records. Now if we want to display the pagination which also tells the how many records are there.
Ex: showing record 1 to 25 of 1500
For this we need to use the count(*) option initially and run the query, then we will get the total results. There is one option which also provides the total number of records.
Use SQL_CALC_FOUND_ROWS and FOUND_ROWS() as a second query to find the total rows.
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 50 LIMIT 25;
mysql> SELECT FOUND_ROWS();
Here first query outputs the first 25 rows and also calculates the total number of records using the SQL_CALC_FOUND_ROWS option. Then second query actually outputs the total number of rows.
Repair the corrupted table in MySQL
April 6, 2008 – 4:42 pmSometimes, high load on the server, multiple INSERTs and UPDATEs, many SELECT query execution, or hardware failure, your database server may corrupt a table.
The corrupted table can be repaired back using the following statement:
REPAIR TABLE tablename;
Create or copy an existing MySQL table to a new table
April 6, 2008 – 4:28 pmYou can create a new table that looks like another table. That is newly created table will have same structure and definition as of an existing table. The definitions that are copied are: Column names, Data type, precision, length, and scale, Column text.
CREATE TABLE Employee1 LIKE Employee;
Above statement creates the new table Employee1 same as Employee table with the same columns and definition in it.
INSERT Employee1 SELECT * FROM Employee;
This statement copies the data from Employee to new table Employee1. You can also specify the specific database names if you want to copy the database1.table1 in database2 as table1.
Pagination using LIMIT clause in MySQL
April 6, 2008 – 4:14 pmMySQL supports a really cool feature for pagination is LIMIT clause.
The LIMIT clause is used to limit the number of results or rows returned in a SQL statement. So if you have 500 rows in a table, and only want to return the first 20, you would do something like this:
SELECT column1, column2, column3 FROM tablename LIMIT 20;
LIMIT clause always goes at the end of the query on MySQL. Now suppose if you wanted to show results 21-30.
SELECT * FROM tablename LIMIT 20,10;
This feature can be used in the pagination for multiple records. Pagination can achieved by passing the page number and number of results or rows per page in the coding.
SELECT * FROM tablename LIMIT $page_number*$rows, $rows; #php coding
For Example:
$rows = 10 (per page)
case page = 0: SELECT * FROM tablename LIMIT 0,10; #first 10 records
case page = 1: SELECT * FROM tablename LIMIT 10,10; #first 11-20 records
case page = 2: SELECT * FROM tablename LIMIT 20,10; #first 21-30 records
……
……
