Thursday, January 17, 2013

JavaScript Auto-Refreshing Script


Take an example of clock. Here we will create a clock that refreshes in every 1 second to show the correct time.
Start with the basic HTML tags
<html>
     <head>
Now, in head section start the javascript section
           <script type = "text/javascript">
Create a function time() function that creates a variable today and assign it the value of current time.
Now, the value of variable is written in the element having id “timeDiv”
                function time(){
                var today = new Date();
                document.getElementById("timeDiv").innerHTML = today;
                }
Create another function update() that runs the time() function in every 1 second.
                function update(){
                time();
                setInterval("time()",1000);
                }
Close the javascript section.
           </script>
Give some title to the document.
           <title>Time Auto Refreshing</title>
Close the head section.
     </head>
Start the body section. It automatically runs the update() function when the page is loaded.
     <body onload="update();">
Create a div element with id “timeDiv” in which the variable “today” defined above will be stored.
           <div id="timeDiv" style="font-size:32px;"></div>
Close the body and html section.
     </body>
</html>
Save the file with .html extension (eg. test.html)

Tuesday, January 15, 2013

PHP Coding Tips


  • Use mysql_real_escape_string() function in MySQL Queries.
  • Remember to close the mysql connection at the end of the page using mysql_close().
  • Use comments for better understanding of large programs.
  • Prefer writing column names instead of * in select statement. Prefer writing column names instead of * in select statement.

    Select `fname`,`lname` from `users`
    Instead of
    Select * from `users`


  • Prefer writing comments in PHP rather than HTML because PHP comments are invisible from the browser source code but HTML comments are visible.
  • Avoid using multi-variable assignments unless not needed like:

  •             $var=$_POST[‘user’];
    $user=$_POST[‘user’];

  • Shorten the coding as much as possible. This will optimize the program and make the site faster.
  • Indent properly for the clarity of the program like

  • If($var==1) {
                            do something;
    }
    else { 
                            do something else;
    }

Saturday, January 12, 2013

PHP Installation in Apache


Following are steps for installing PHP enviornment in your Apache Server
Step 1: Download PHP from http://php.net/downloads.php

Step 2: Extract the files at some location say "C:\php".

Step 3: Define extention directory in php.ini
extension_dir = "C:\php\ext"

Step 4: Go to System Environment Variables
 -> In "System Variables" Block Select variable "Path"
 -> Click Edit
 -> Then at the end of value add the php folder i.e. "C:\php" path by separating from others with semi-colon(;)

In httpd.conf (in apache/conf)
Edit <IfModule dir_module> DirectoryIndex if (present) as below, otherwise add the following line.
DirectoryIndex index.php index.html

At the end, add the following lines
# PHP5 module
LoadModule php5_module "c:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"

In above, replace "C:/php" with directory specified by you.

Save and test in command line
cd \Apache2\bin
httpd -t