Tuesday, February 28, 2012

PHP Tutorial: Getting user input in PHP using HTML form

There are basically three ways in PHP to get user input. They are
  • Using HTML Form
  • Using URL values links
  • Using COOKIES
In this tutorial, I shall talk how to use HTML Form to get user input and retrieve that values from a web server. Form is simple HTML form start with <form> tag and end with </form>. The form is posted to the server using POST method and each data user has entered is stored in a PHP super global variable $\$\_POST$.  $\$\_POST$ is an associative array that stores variables in key => value format. Let us take an example of simple HTML form with two fields: username and password.

Monday, February 27, 2012

Fastest way of calculating Prime Number with less system overhead with C code.

A number is said to be a prime number if it is divisible only by 1 and  the number itself. That is prime number is divisible by exactly two numbers. For example, 11 is prime number because it is divisible by 1 and 11 itself  but 4 is not prime number because it is divisible by 1, 2 and 4.
Test of prime number is famous question in programming. You are supplied any arbitrary number and you have to find out whether it is prime or not. There are various methods and algorithms for testing the prime number condition. Some methods take less iteration and in turn less overhead and some  methods take large iteration and in turn large overhead. I will present you two methods; one uses large number of iterations and another uses drastically reduced number of iterations.
First method (High Iterations, Slow process)

Friday, February 24, 2012

Mini project Digital Volt Meter (DVM) using PIC16F877A with C code

DVM Project Overview
Digital Volt Meter is a Mini Project that I made as my project of the subject ‘Microprocessor Based Instrumentation System’. In this project I have used three major electronic components. They are PIC16F877A, LCD Display (16 X 2) and Low power Quad op-amp (LM324). PIC16F877A is used as an ADC module which converts the analog input into corresponding 10 bit digital output. It also provides enable and RS signals to the LCD display. LCD Display is used to display the converted digital binary data to decimal format so that user can easily understand the voltage. And finally Low power quad op-amp is used as buffer and as inverter. The DVM measures the voltages ranging from –50 V to +50 V

Wednesday, February 22, 2012

Configuring MonoDroid in Visual Studio 2010

In my previous article, I walked through the introduction of MonoDroid and the way to download the free version of MonoDroid installer. If you have downloaded the installer, then now I will show you how to download different components and install it and finally how to configure Mono for Android in Visual Studio 2010.
In first step double click the installer you have downloaded. A dialog box will appears that is all about welcoming you to installation process. Click Next button.

Saturday, February 11, 2012

Android App Development with MonoDroid using .NET framework in Visual Studio

The craze of smart phones is increasing day by day. Many people in the world use Smart Phones as they are portable, good looking, have high RAM and memory, high processor speed, good support for wireless and wifi and many more. iPhone, Android , Windows Phone 7 are some examples of smart phone. Among them android is most popular. It is because, it is open source, availability of large numbers of applications, large number of mobile company deploy it, large developer community and many more. So large number of mobile application developers are interested in Android Application Development. In this article, I focus on the Android App Development for C# .NET programmers using MonoDroid.
Android APP Development is mostly based on Java VM “Dalvik” with Eclipse IDE.

Friday, February 10, 2012

Different Data Types in PHP

PHP supports wide range of Data Types. It supports eight different primitive data types: four scalar types (boolean, integer, float, string), two command types: (array and object) and finally two special types (resource, NULL). All these data types are discussed below:
Boolean:
Boolean is data type whose value is either true or false. True and false in PHP are case insensitive. To specify a boolean literal, use the keywords TRUE or FALSE. For example:

PHP Tutorial: The Operational Trail

In this section, I discuss about the operation of server to process PHP code. TheOperationalTrail

When you request some file like http://yourdomain/test.php on a web browser, as shown in above figure, then following different operations are performed on server.

  • First web browser will make a request to the the server to find a page
  • The file is located on the hard drive of the server.
  • Because it has .php extension, it needs to process the PHP code which is done by the server.
  • If the file contains operations on database, then it sends/receives the data to/from database
  • At last server returns pure HTML code to the browser which browser can easily understand.

Thursday, February 9, 2012

Subtraction of two binary numbers using C

Basic Theory
Subtraction can be regarded as addition. For example A - B is same as A + (-B). In binary most computer uses 2’s complement form to represent negative binary number. The subtrahend is first converted to 2’s complement form to make it negative and then is added to minued. This is same as above example.
Code Assumption
This code is capable to perform subtraction between two decimal numbers from 0 to 127. The subtrahend is first converted into 2’s complement form and then addition is perform. After addition the result is obtained. If the result has 1 in its MSB then it is treated as negative result and is converted to decimal format otherwise it is treated as positive number and converted to positive decimal.
Source Code:

Addition of two binary numbers using C

Basic Theory
Addition of binary numbers is far simpler than that of decimal number. This is because, binary addition includes addition between 1 and 0 only. The addition process results into two unit: sum and carry
sum = a xor b xor c
carry = ab + bc+ ac
Initially the carry bit is set to be 0. This process is continue until all the bits in a binary number finish.

Code Assumption

The following code uses 8 – bit binary unsigned integer. This means each integer can hold from 0 to 255 in decimal. So user can add up to 255. If the result is greater than 255, then condition of overflow occurs. The code contains following segments
  • Decimal to Binary Conversion
  • Addition of two binary numbers
  • Binary to Decimal conversion

Wednesday, February 8, 2012

Getting Started with PHP : Writing Hello World Program

Lets start this section with small example, consider a small portion of PHP code

<?php
	$var = 5;
	echo "the value of variable is ". $var;
?>

This program simply outputs a string “the value of variable is 5” on a browser. Now lets see the mechanism how it works.


Every PHP code segment must start with <?php tag and end with ?> tag. Note that there is no space between < , ? and php. If web server finds <?php ?> tag on a web page that browser is requesting, then it converts all the PHP statement into corresponding HTML statement which browser can understand. If you run the above code on web server and view the source, then you see that there are no any php statements.


Every PHP variable declaration start with $ sign. PHP is not so strict like programming languages therefore all types of variable (int, float, string, long etc) can be declared by just writing $var_name. For example:

<?php
    $intvar = 5;
    $floatvar = 5.5;
    $stringvar = "nepal";
?>

To print a statement to a browser window, PHP command echo is used. The statement to be printed is kept inside single or double quotes. The . (dot) operator concatenates two string.


Embedding PHP code with HTML


PHP is designed to use with HTML. PHP alone cannot do anything. Any website needs different HTML tags. But PHP with HTML helps to create dynamic pages. So PHP code can be embedded inside HTML code as below.

<HTML>
<HEAD>
<TITLE>My First Program</TITLE>
<HEAD>
<BODY>
<?php
    $var = 5;
    echo "the value of variable is " . $var;
?>
</BODY>
</HTML>

Tuesday, February 7, 2012

Installing PHP using WAMP on windows

I gave you brief introduction of Wamp Server in my previous article and link to download it. Now I am going to write how Wamp server can be used to run the PHP code on windows. Wamp basically contains three components : Apache, PHP and MySQL. Besides  it contains quick links for Localhost and PhpMyAdmin.  You can edit php.ini file as well as other many files using wamp server. It also has MySQL Console window (command prompt). WAMP server configures all the components for you so that you don’t need to configure each component manually.  Now lets install the wamp server first

Installation:

http://www.wampserver.com is official site for wamp server. Browse the site and download the wamp server. After downloading , just double click the executable file WampServer2.0i.exe (or any other version) and install it.

Wamp Server

At the end of the installation , tick the check box start wampserver now . After you start wampserverm you will see a small icon  on the lower – right corner of desktop window. Click on the icon, a list of different files and folders like localhost, phpmyadmin, Apache, MySQLand PHP  pops up. Through this icon, you can put server online or offline, restart the services, edit the settings for PHP, see the versions of PHP , MySQL and Apache, jump to different directories and many more.

Testing the wampserver by running a sample php code.

Now wampserver is ready to use. Lets check  whether it is actually working or not. Open any web browser and type http://localhost or http://127.0.0.1 in URL Address bar. If wampserver is running, then a webpage is displayed (as shown in below) for you otherwise browser cannot find localhost.

Localhost

Open C drive then you will see a folder named wamp. Inside wamp there is a folder called www. This is the folder where each PHP file must be kept in order to run them. If you place PHP files other than inside www, then the browser cannot find your files. Make a text file and rename the name of the file as test.php. Inside test.php write following code. 

<HTML>
<HEAD>
<TITLE> Testing Wampserver </TITLE>
</HEAD>
<BODY>
<?php
    echo "Hello world";
?>
</BODY>
</HTML>

Save this file and type http://localhost/test.php one URL of the browser. Then you will see a webpage with Hello world written. This confirms that the server is working well on your computer  and you are ready to write a php code and run it.

Monday, February 6, 2012

Installation of PHP on Windows Platform

To run PHP code and see the output on Browser, you need following things

  1. A Web Server [e.g. Apache]
  2. PHP
  3. Database [e.g. MySQL]
  4. Text Editor [e.g. Notepad++, KOMODO, Text Mate]
  5. Web Browser [e.g. IE, Firefox, Google Chrome]

You can use any combination of them. Most people uses Apache as Web Server, PHP and MySQL as Database regardless of their platform.

PHP need 32 or 64 bit platform to work properly. PHP will not work on 16 bit platforms such as Windows 3.1.  Windows 98/Me/NT4 is no longer supported as of PHP 5.3.0. Windows 95 is no longer supported as of PHP 4.3.0.

There are two ways to install PHP on windows. One way of installing is to download each component (Apache, PHP and MySQL) separately and configure manually. Configuring PHP manually, which I think, is very cumbersome. You should follow a lot of steps very carefully. If you prefer to configure each component manually then here is the links to download

  1. Apache: httpd.apache.org

The other way of installing is to download a single pack which contains all these things (Apache, PHP and MySQL) . For windows, there are some single pack installer available on the internet.

  1. WAMP (Windows Apache MySQL PHP) : http://www.wampserver.com/en/index.php

Both are very handy and easy to use. But I will be using WAMP throughout my tutorial series. In next tutorial I will show you how to use WAMP on windows.

Sunday, February 5, 2012

Why should we use PHP?

PHP is important tool for web development because of the following facts

  • PHP is open source and free software – You can use PHP without any cost.
  • PHP is cross platform to develop, to deploy and to use.
  • PHP is Powerful, Robust and Scalable.
  • PHP is web development specific.
  • PHP can be object oriented , especially version 5 or more.
  • PHP has Great Documentation in many languages.
  • PHP has large active developer community (more than 25 million websites)

What is PHP?

PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source general purpose scripting language that is especially suited for Web development and can be embedded into HTML. Here are some points that define PHP.

  • PHP is a scripting language but not programming language.
  • PHP is server – side scripting language. PHP code is only recognized by web servers. Web Browsers do not recognize PHP and can’t response it.
  • PHP need a web server in order to run its code. It does not need to be compiled like C, C++ or JAVA.
  • PHP is especially designed to use  with HTML. HTML alone can not provides flexibility to a website. PHP helps to create dynamic pages.
  • PHP syntax is very similar to that of C and Perl. Object oriented PHP syntax is similar to JAVA and C++.
  • The extension of PHP file must be .php. If you place PHP code in a file having extension .htm or .html then PHP code can not do anything.
  • PHP can be used on all major operating system like Microsoft Windows, Linux, Mac OS, RICS OS and others. So, with PHP, we are free to choose any kind of operating system you are familiar with.
  • PHP has support for must of the web server like Apache, IIS and many more.
  • With PHP you are not limited to output HTML. PHP's abilities includes outputting images, PDF files and even Flash movies (using libswf and Ming) generated on the fly. You can also output easily any text, such as XHTML and any other XML file. PHP can auto generated these files, and save them in the file system, instead of printing it out, forming a server-side cache for your dynamic content.
  • PHP has support for wide range of Databases.
  • PHP also has support for talking to other services using protocols such as LDAP, IMAP, SNMP, NNTP, POP3, HTTP, COM (on Windows) and countless others.
     
     
 

 

 

     

Saturday, February 4, 2012

Lightning in OpenGL with spot Light

So far we have seen lighting effects which have the following properties:

  • The light is being projected from an infinite distance.
  • The rate of fading of the brightness at the circumference is very low.

But, we might want to produce lights, which have a position in the scene and are not projected from an infinite source unlike the normal lighting which we have seen so far. Like a study lamp or a spot light on a pool table, etc.

Source Code:

#include <windows.h>
#include <GL/glut.h>
static double yVal=1.0;
int spot(double a, double b, double c, double d, double e, double f){
    /*
    a, b and c -- x, y and z co-ordinates for light position
    d, e and f -- x, y and z co-ordinates for spot light position
    */
    GLfloat mat_specular[] = { 0.3, 1.0, 0.3, 1.0 };
    GLfloat mat_shininess[] = { 50.0 };
    GLfloat light_position[] = { a,b,c, 1.0 };
    GLfloat spotDir[] = { d,e,f };
    glClearColor (0.5, 0.5, 0.5, 0.0);
    glShadeModel (GL_SMOOTH);
    glLightfv(GL_LIGHT0,GL_SPECULAR,mat_specular);
    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    // Definig spotlight attributes
    glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,95.0);
    glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,2.0);
    glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_DEPTH_TEST);
    return 0;
}
void drawSphere(){
    GLUquadricObj* cyl;
    glClearColor (0.5, 0.5, 0.5, 0.0);
    GLfloat light_position[] = { 50.0, 50.0 , 0.0, 1.0 };
    GLfloat mat_specular[] = { 0.3, 0.3, 1.0, 1.0 };
    GLfloat mat_shininess[] = { 100.0 };
    GLfloat spotDir[] = { 50.0, 30.0, 0.0 };
    glShadeModel (GL_SMOOTH);
    spot( yVal,5.0,1.5,10.0,1.0,10.0);
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(35.0, 1.0, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (30.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
    cyl = gluNewQuadric();
    gluQuadricDrawStyle(cyl, GLU_FILL);
    glPushMatrix();
    gluSphere(cyl, 4.0, 1000, 1000);
    glPopMatrix();
    glFlush();
}
void display(void){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    drawSphere();
    glFlush ();
}
void init (void){
    glClearColor (0.5, 0.5, 0.5, 0.0);
    glEnable(GL_DEPTH_TEST); //enabling z-buffer
    /* initialize viewing values */
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(35.0, 1.0, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (30.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
}
void keyboard(unsigned char key, int x, int y){
    switch (key) {
        case 27: // “esc” on keyboard
        exit(0);
        break;
        case 97: // “a” on keyboard
        yVal++;
        glutPostRedisplay();
        break;
    }
}
int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (600, 600);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Spot Light - Programming Techniques");
    init ();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

Output


Spot Light in OpenGL

Lightning in OpenGL with Material Effect

This is second tutorial on Lightning in OpenGL. In this tutorial, I will discuss about producing Material Effect in OpenGL. Material parameters specify how a surface reflects light. Applications change OpenGL material parameters to emulate different colored materials, shiny and flat materials, high gloss materials such as pool ball, etc.
The routine glColor3f(), sets the primary color and is a good idea to call this routine as long as we are not using lighting effects. The OpenGL lighting doesn’t use the primary color, instead uses the material colors. Hence it is important to introduce materials while talking about lighting.

So far we have seen a simple lighting example which has no material effects. But, in order to make the scene more attractive, we have to make use of materials. In the following code, we have used a green material. We make use of GL_SPECULAR, GL_AMBIENT, GL_SHININESS depending on the
scene requirements. The same parameters are also applicable to lighting methods.

Source Code:

#include <windows.h>
#include <GL/glut.h>
static double yVal=50.0;
void drawSphere(){
    GLUquadricObj* cyl;
    GLfloat light_position[] = { 0.0, 40.0, yVal, 0.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    GLfloat mat_specular[] = { 0.3f, 1.0f, 0.3f, 1.0f }; // Green color material
    GLfloat mat_shininess[] = { 70.0 }; // Defines shininess
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // Using materials
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(35.0, 1.0, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (30.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
    cyl = gluNewQuadric();
    gluQuadricDrawStyle(cyl, GLU_FILL);
    gluSphere(cyl, 2.0, 50, 50);
    glFlush();
}
void display(void){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    drawSphere();
    glFlush ();
}
void init (void){
    glClearColor (0.5, 0.5, 0.5, 0.0);
    glEnable(GL_DEPTH_TEST); //enabling z-buffer
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(35.0, 1.0, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (30.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
}
void keyboard(unsigned char key, int x, int y){
    switch (key) {
        case 27: // “esc” on keyboard
        exit(0);
        break;
        case 97: // “a” on keyboard
        yVal = yVal-5.0;
        glutPostRedisplay();
        break;
    }
}
int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (600, 600);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Light Material effect - Programming Techniques");
    init ();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}


Output


Lightning in OpenGL with Material

Wednesday, February 1, 2012

Lightning in OpenGL without material effect .

This is the first tutorial in Lightning in OpenGL. In this tutorial, I am going to show you how to produce a simple lightning effect in OpenGL without material effect. In later tutorial, I will discuss about material effect and also discuss about spot light. OpenGL implementations are required to have at least 8 primary lights ranging from GL_LIGHT0 to GL_LIGHT7. In order to use lighting in the code, we have to first enable lighting by calling the routine glEnable(GL_LIGHTING). A glLookAt() routine sets the camera position and we have can set the position of the lighting using glLightfv(). This routine actually sets the direction of the light, but not the actual position of the light. Though we are able to set a position for the light source, the light source behaves as if it is at an infinite distance. The following example creates a sphere and produces a simple light effect.
 Source Code