Learning Computer ProgrammingLearning Computer ProgrammingI post articles mostly on Learning Computer Programming in general and specifically using C plus plus. This blogsite is created as a part of my hobby of computer programming, I also share my experiences in a teaching manner. Articles
Creating a Simple HTML Form Validation System Using JavaScript
2008-06-09 07:43:00 OK, so today we are going to create a simple Form Validator Script using Java Script that would not let a HTML form be submitted unless all required field are filled in. JavaScript is widely used for this purpose as it does not need server processing hence no form sending and pageload is required. When you are using data submission via form on your website there are a few other methods that you can employ other than JavaScript; first, not to use validation at all (NOT RECOMMENDED), second, to use validation in the script and make it show appropriate message (need pageload). So definitely we’ll be using JavaScript and you should in most cases, too. Let’s kick off guys! Now before we begin I want to tell you one thing, we will only be checking (validating) if all the required fields are filled or not and NOT whether what is filled in is acceptable. Of course we can use JavaScript to validate whether, let’s say the filled email address is ... More About: System , Creating , Javascript
Deigning a Simple HTML Menu-Bar Using CSS
2008-06-08 08:11:00 In this post I’m going to show how you can create a menu-bar (list of hyperlinks) using CSS styling. This requires that you at least have some basic knowledge of HTML and preferably CSS too. If you can’t figure out what a menu-bar looks like, scroll to the bottom. Seen! Though pretty much the same can be implemented using tables but that is not web developers prefer. In HTML there is not standard tag for creating a menu like item therefore we would be customized or style a standard tag. We’ll be using the ‘unordered list’ <ul></li> tag to do so, in this case every item i.e. <li></li> will be items in the menu. Let us first start by creating an unordered list as follows: <ul> <li>Item&nb sp;1</li> <li>Item&nb sp;2</li> </ul> It shows up something like below: Item 1 Item 2 Obviously this is not what we want; items h... More About: Simple , Menu , Html
Blogger: Method to Add 'Link to This Post' Widget on Your Posts
2008-06-07 08:20:00 [Today I’m going to provide you with a code that you can put on your Blogger Blogs. It’ll give your visitors a very easy way to link to your posts if they like it. Scroll to the bottom to see it in action! You might also want to read New Working Method of Making Blogger Title Tags SEO Friendly] When it comes to Search Engine rankings, link popularity is no doubt one of the most important factors especially organic link popularity. Nowadays Search Engines’ algorithms have become so intelligent and sophisticated that they could differentiate between different types of links; paid, reciprocal, organic etc. to name some, automatically and not let discouraged factors manipulate rankings much (in some cases by even penalizing those site). Coming to Organic Link Building, there is no better way to do (or at least increase) Link Building than to ask or encourage your visitors. You see, I’ve it at the bottom of this post. This way you ... More About: Widget , Blogging , Post
One More Use of Web Cookies
2008-06-04 08:20:00 Ok, so we were discussing about Cookies in the last post, continuing the discussion today we’ll see one more example of where cookie is almost irreplaceable by any other method. If you remember the post Creating a Simple Visitor Counter in PHP, although we named it ‘Visitor Counter’, what it actually counted was the number of times a page was requested. So, for example one person can request a page ten times and counter would show ten but actually the number of visitor id ONE. Ten is the number of page views. Now you may have got what I’m up to today! Yeah we’ll employ cookies to track whether consecutive page requests came form the same visitor or not. If it is then another counter for number of page views will be incremented instead. We cannot use Session Variables as they by default last only until the session is over. We’ll make the cookie expire after twenty four hour or one day period after that again t...
Enhancing the Poll System Using 'Cookies'
2008-06-03 08:14:00 Umm, actually not the usual ?Cookie? that we eat! Those of you who have created the Poll ing System that we discussed in Creating a Simple Polling System in PHP, know that the system was simple enough to let the same user vote over and over gain multiple times. It is an open invitation to spammers and other peoples to manipulate the poll. The real-world polling systems do not let people do that. So, unless we implement some method to fight that, out Polling System can not be used on websites. So, you?ve got to read along if you are interested in creating a ?real? polling system. One more thing, it?s not just polling system that requires knowing a unique visitor, there are plenty more applications of it. We have previously discussed in What is Session Control/Variables? that Session Control helps us know whether consecutive requests are made by the same user or not thus it can be used in polling system to not let the ?same? user vote again. But, as we know sessions (by default) last... More About: Cookies
Enhancing Search (Input) Boxes Using JavaScript
2008-06-02 08:13:00 Time for some JavaScript action. JavaScript as you know is a client-side web scripting language and is the best choice for any client-side scripting application for the web. Today we’re going to employ some basic and simple JavaScript methods to enhance a Search (Input ) Box as in the image above. You’ve probably come across this type of Search Box before. In the text box initially is written ‘Search’ or something of that sort but as soon as you click or focus on it, the field becomes empty for you to type in, again when it is left blank and focus is taken off it, field changes back to have the initial text. I don’t actually see a big enough reason why people employ this technique but one common reason might be when there is not enough space to label the search box, this technique can use the Text Box to label itself. OK, enough discussion, now let’s have a look at the code: <form action="SCRIPT-NAME" m ethod... More About: Blogging , Javascript
Permanent (301) and Temporary (302) Redirection Using PHP
2008-06-01 08:14:00 Redirection is the method of forwarding users to a new domain or URL when they try to reach the old one. Suppose my friend Ralph had once created a website at: ralphhaynes.com and after sometime created yet another website on the sub domain: blog.ralphhaynes.com Now he wants to have his blog (second website) on a new separate domain name. Since he already has lots of readers who visit the old URL, he has to find some way to let them know the new URL. The easiest way yet the most effective would be to use Redirection. Actually, there are two types of redirection, permanent and temporary. In the above case when Ralph wanted to permanently move to a new domain he should use Permanent Redirection. In some other cases when webmasters have to move from, say a domain to a sub domain due to some temporary problems they could use Temporary Redirection. 301 Permanent Redirection ‘301’ here is the HTTP response code sent by the server to the client. Serve... More About: Methods
New Working Method of Making Blogger Title Tags SEO Friendly
2008-05-27 11:58:00 [Blogger made some recent changes on 9th May 2008 which enables us to use a simple method to make SEO Friendly Title Tags for Post Pages (Permalink Pages), I guess I am a bit too late to notice but as we’re discussing about Web Programming lately, I’ll share the method anyway.] One of the biggest problems I face with Blogger blogs is the inability to create custom SEO and human friendly title tags. Blogger does not give us any option to change the way title tags are in post pages. Titles of post pages in Blogger are always in the following form: [Blog Title] : [Post Title] In this case when you have a bit long Blog Name, all your pages in the Search Engine index would start with the blog name taking most of the previous space. That way it wouldn’t be relevant to the content of the post in most cases The following form of title would be considered good: [Post Title] : [Blog Title] Or if your blog’s name is too long you can use just th... More About: Blogging
Creating a Simple Poll Script in PHP
2008-05-26 07:44:00 OK, so again a useful script that we are going to create here. A polling system/script needs no introduction as it has been on the Web since long and is a good method to increase visitor activity on a website, apart from knowing their responses. OK let’s start! A polling system requires vote data to be stored, for that database (MySQL) would be the best choice. Possible table structure for the database for a poll having three options would be Logically thinking we know that only one row is required for a poll. Each time someone votes the value of the respective fields should be updated (incremented). Initially the table would be like below: After five votes to the first option, four to the second option and one to the third; the table would look like: It should now be clear why we only need one row for a poll. Designing the Poll OK, now we’ll have to design the poll using an HTML table, some radio buttons and a submit button. You may write you... More About: Creating , Script , Simple
Constructors & Destructors of a Class in PHP
2008-05-24 11:36:00 [Please read Introduction to Class es in C++, Introduction to Constructor and Destructor Functions in C++ and Destructor Functions in (C++) in Detail to know more about Constructors and Destructors, even though from C++’s perspective, they are useful nonetheless.] Constructors A constructor is a special member function of a class which is called automatically when an object of the class is created. We can use it to perform useful initialization such as giving initial values to member variables etc. Unlike C++, PHP’s constructors always have the name ‘__construct’ and they can be defined as: function __c onstruct() {   ; ... } Prior to PHP Version 5, constructors used to have the same name as of the class (like in C++). Though PHP5 still understands constructors defined this way to be backward compatible...
Object Oriented PHP...Creating and Using Classes
2008-05-23 11:42:00 [Please Read Introduction to Classes in C++, Unit Conversion Program using Classes, A Multi-Purpose String Class in C++ etc. for more information. Despite being from C++'s perspective they're useful.] In PHP a class is defined as following: class myclass { ... ... } For C/C++ programmers, do note that there’s no ‘;’ after the closing curly brace. A variable can be defined inside a class (AKA member variable) as: class myclass { //notice the use of 'var' var $var; ... ... } NOTE: We have to use ‘var’ keyword to define variables. A function can also be defined inside a class as following. These functions are known as Member Functions. class myclass { function myf unc() ... More About: Creating , Object
A Complete Note Keeping Application
2008-05-17 08:34:00 In this post we are going to expand the Note Keeping Application , using MySQL to create a complete multi-user Application which would be able to register new users as well. It’d be a multi-user system hence many users can register and work on their notes simultaneously. Each user’s data will be private to them. It’d also have the missing features such as editing and deleting of existing notes, hence a complete Note Keeping Application. In a multi-user application we need to separate each user’s data so that every user is shown only the data belonging to them. As we’ll be using MySQL database to store data, one option is to create a table for each user, it’d be good but when you have like thousands of users, having a table for each is not very efficient. There is one very efficient technique however, to separate data of different users using relational database structure. For this we’ll only need two table... More About: Complete
Using Template System for CSS Designed Web Page
2008-05-16 08:40:00 In the last post on Creating Simple Template Based Website we designed a simple Template System , if you remember its layout was designed using a Table. While tables can be used to design page layout, it’s not the standard way and has got many problems associated, instead Cascading Style Sheets or CSS should be used. If at all I’m going to give an introduction to CSS now, I’d say it is a way of styling pages and page elements. Even if you don’t know about CSS (and I’m not discussing much about it here) you’ll understand this post and how CSS can be used for designing page layout. So let’s begin! Below is the CSS coded version of the web page that we deigned in the previous post: <html> <head> <title>CSS Coded Web  ;Page </title> <style> #page{ width: 90%; height: 90%; border: 1px& nbsp;sol...
Using 'require' to Create a Simple Template System for Websites
2008-05-15 08:24:00 The image above shows what different parts of a general website are known as. It is certain that for most websites (even this one), there is a consistent look. E.g. the header, sidebar and footer remain the same for all the pages of a website such that you may create many pages from one page just by changing the body text and saving as a different file. But suppose if you have created, like 10, 20, 30, 50 or even more pages that way and now you want to change the copyright year on the footer or add a link on the sidebar, what would you do? There is only one way, to change all the pages which is very cumbersome. Well, PHP has got features to include consistent parts of pages on many pages such that editing only that part at one place would be enough to change the whole site. It can be done in the simplest way as following: <html> <head> <title>Sample Page</title&g t; </head> <body><?php require('... More About: System , Websites , Template , Simple , Create
Designing a User-Registration Script in PHP
2008-05-14 08:48:00 Having enough knowledge of Storing and Retrieving Data from MySQL Database we now can easily create a User Registration System to register new users for sites requiring user authorization. We’ve previously created many scripts and applications that require user authorization but none of them can register new users by themselves. We had to hard code the user data into the scripts. For any real application though, that won’t work and we require a “New User Registration’ system. That’s what we are going tot create in this post. Registering new user is just a matter of storing username, password and optionally other information about the user in a database. As we have discussed Storing and Retrieving Data from MySQL Database, so we straightaway look at the code listing: <html> <head> <title>My Web Site | User Registration</title> ; </head> <body> ... More About: Designing , Script
Creating a Simple Phone Book in PHP
2008-05-13 12:56:00 This is a short follow-up of the post Storing and Retrieving Data from MySQL Database. Here we’ll expand the Phone Book Script that we’ve created to include the following three features: Use an efficient table structure to store data with proper INDEX (primary key) defined. Display previously stored phone numbers in different orders. Include a Search feature to let user easily find the person’s phone number he/she is seeking. To create efficient table Read the MySQL Data Types and Properties post to know more. We’ll create a table having an ID field having serial numbers auto_incremented. We’ll set it to be the primary key. We can do this with the help of the following SQL query: CREATE TABLE phno( id int AUTO_INCREMENT PRIMARY KEY , name varchar( 50 ) , phnum varchar( 20 ) ) Retrieving data in other orders For our table we’d easily be able to retrieve data in the following f... More About: Creating , Simple
MySQL Data Types and Properties
2008-05-13 11:54:00 Have a look at the following line of code from the post Storing and Retrieving Data from MySQL Database. CREATE TABLE phno( name varchar( 50 ) , phnum varchar( 20 ) ) As I told you we have to declare the column data types while creating tables depending on the values we want to store. MySQL has wide variety of data types, out of which I’m stating some of the important ones below: CHAR: For storing character and string data, faster than VARCHAR. VARCHAR: For storing character and string data. Slower but efficient in storing variable length string as only the needed memory is used no matter how much we allocate. INT: For storing regular integers. Same as INTEGER, other related data types are TINYINT, SMALLINT, MEDIUMINT, BIGINT. FLOAT: For storing floating point values, it is 4 bytes long. There is also a DOUBLE which is 8 bytes long. DATE: For storing YYYY-MM-DD date values. ... More About: Mysql , Types , Properties
Storing and Retrieving Data from MySQL Database
2008-05-12 13:58:00 You may first want to read Installing, Configuring and Testing MySQL Data base Server on 'localhost', About MySQL, Database s and SQL Commands. Now that you have installed MySQL database server and have learnt some SQL commands to query MySQL, its time to use our knowledge to do what database servers are used for, storing and retrieving data. To make for a nice example, we’ll create a simple Phonebook Application which would store phone numbers and names of peoples. It’d be able to illustrate the storage and retrieval of data from database quite well. OK let’s first start with the datable design. We’d need to create a database first and table in that database. One table is enough because we only need to store name and phone number which should be stored in the same table. For the table we should have two fields (column), one for name and the other for phone number. Both can be of data type (MySQL) VARCHAR as we don’t nee... More About: Mysql
About MySQL, Databases and SQL Commands
2008-05-12 08:52:00 In the last post we ‘created a database’ from our script as an example to test the working of MySQL. Actually a database is a set of tables for storing data. The tables in a database may be logically related with each other. MySQL supports unlimited number of databases and for each application you may use a different one To understand relation between a database and tables consider an example of a database oriented Note Keeping application. Since it needed user authorization we can store user information such as username and password in one table, upgrading that system to multi-user capable is also possible because a table can store any number of rows (i.e. different set of username & password, in this case). We’ll also need a second table for storing note data. Thus we’re using a database having two tables to store data related to a single application. The following image illustrates this relation quite well: MySQL da... More About: Databases , Mysql , Commands
Installing, Configuring and Testing MySQL Database Server on ?localhost?
2008-05-12 08:39:00 Here we go with another installation and configuration post for out local server; this time for MySQL, the well known and widely popular free database server. [You may want to read Installing Web Server on your PC, Installing Apache and PHP on your PC and Configuring Apache Web Server and PHP] In this post, we’ll learn to set-up MySQL server on our own local server and see how we can connect to it from our scripts. Since the last few posts we have been having problems implementing data storage/retrieval from files, we thought it was the right time to start using database for that purpose. However before we can use database connectivity from our scripts, we need to install a database server, we’ve chosen My SQL. In spite of MySQL database server being free, it has proven itself time and again as the best database server around. Its free too so there is no point in us looking for anything else. OK, let’s start by first downloading ... More About: Testing , Database , Mysql , Localhost
Designing a Simple Online Note Keeping Web Site
2008-05-11 08:54:00 From the start of Web 2.0 everything from Bookmarks to Spreadsheets and Office packages to RSS Readers has become an online thing. In that spirit we are going to create an Online Note Keeping application in PHP which would give us the power to keep our notes online. That means all of our notes will be accessible from anywhere the Internet is available that too secured with user authentication. We are talking about storing notes so obviously we’ll need either a database or file to store the data. For now we’ll use a file instead. To store notes in a file we need to create and follow a format (the way each of the notes will be stored). For each note there will be a Title, Date and Time it was written and the actual Note. So I think the following format will be fine: <Title> <Date & Time> <Body> We’ll follow this for each note we store into the file. Talking about the interface, user will first be presented with a logi... More About: Designing , Site , Simple
Implementing User Authorization Using Session Control
2008-05-10 09:18:00 You may want to read An Example of User Authentication System in PHP, What is Session Control /Variables? before reading this post. In this post we are going to create a ‘secret’ site whose pages will only be accessible after logging in using the correct username and password. We will be using our knowledge of Session Control/Variables since we want authorization for the whole site and not a single page. The site that we are going to create will have three ‘secret’ pages plus one-one page for logging in and homepage. Let’s start by having a look at the login page code: <html> <head> <title>My Web Site | Login</title> </head> <body> <h1>My Web Site</h1> ; <h2>Login </h2> <?php define('USERNAME','happyjoe'); define('PASSWORD','123456'); //if submit button was&nbs p;pressed //that means form was  ;su...
What is Session Control/Variables?
2008-05-10 09:15:00 In the post An Example of User Authentication System in PHP we created a simple authorization system which could show a personalized page when the user enters correct username and passwords. But since HTTP is a stateless protocol (it can’t figure out if two subsequent requests come from the same user) we cannot preserve the state (logged in) on ant consecutive clicks. All it means is that after logging in into that script, if the user clicks on some link, there is no way we can preserve the logged in state (know that a logged in user is requesting a page). Therefore we cannot, that way, personalize the whole site for the logged in user. So, only logging in someone is not all, we’ve tpo preserve that state across the whole session. For this PHP gives us a easy-to-use method. We call it Session Control because it can help maintain a state throughout a session. We implement Session Control using Session Variables whose values are preserved ...
Some Useful Array Manipulation Functions in PHP
2008-05-09 08:49:00 Please read Array s and Array Processing in PHP for an introduction to arrays in PHP. You may also want to read Sorting Arrays in PHP…Some Sorting Functions and Other Ways of Initializing Arrays in PHP. shuffle() Function This function can shuffle or randomly arrange the elements of an arrays. It takes an arrays as argument and manipulates on the original array passed. $ar=array(1,2,3,4,5,6,7); shuffle($ar); //$ar=array(7) { [0]=> int(5) [1]=> //int(4) [2]=> int(3) [ 3]=> int(1) //[4]=> int(6) [5]=>  ;int(7) [6]=> int(2) } //will be different for&nb sp;each run As you can see the elements of the array ‘$ar’ passed have been re-ordered randomly by the shuffle() function. This function can be useful when we’d like to have a random element from an array and the order of the elements is not im... More About: Manipulation
Designing a Simple ?Shout Box? Script in PHP
2008-05-09 08:43:00 Shout Boxes are quite a popular widget that people put on their web site or blog. It gives peoples a very easy way to interact with each other. In case you don’t know what shout box is, you can think of it as a chat script that you can put on a part of an existing web page. People can leave their messages (usually short) there and can interact with each other and with the webmaster. One special feature is that anybody can leave their message without any registrations. That’s why it’s the perfect way to interact with your visitors and let the visitors interact with each other. Designing a simple shout box script is actually not difficult. In this post we’re going to design one. A shout box usually needs the user to fill name (optional), URL (optional) and message to be posted. When an URL is provided ‘name’ becomes a hyperlink. As is obvious, the messages that people post must be saved somewhere. Again Database is the ... More About: Script , Simple , Shout
Other Ways of Initializing Arrays in PHP
2008-05-08 12:04:00 We had discussed about Arrays in PHP in the post Arrays and Array Processing in PHP. We learnt how we can simply initialize an array by giving values to different indices or using the ‘array’ construct. Besides those methods there are a few others that you should know, which is the topic of this post. Creating Sequential Arrays There are times when you want to store sequence or pattern of values in an array. For that task range() function would prove to be very helpful. Suppose if we want to have an array having ten elements from 1 to 10. We can use the following tedious method: $ar[0]=1; $ar[1]=2; $ar[2]=3; ? ? $ar[9]=10; or the range function like: $ar=range(1,10); the above line of code creates an arrays same as the one above. This function can also be use with the optional third argument to set the step between values. Eg. $ar=range(1,10,2); //$ar={1,3,5,7,9} range() function also works with character, s...
All About User-Defined Functions in PHP
2008-05-07 12:13:00 So far we’ve been using PHPs built-in functions to do our tasks; we also declared and used user-defined function in the post Creating a Simple Visitor Counter in PHP. However, we didn’t discuss much about them. For those of you who are curious to know more about user-defined functions or those who’d doubts, I’ve written this post. Read along to know more about user-defined functions. Declaring functions In PHP functions are declared using the keyword ‘function’ as below: function func-name(arg-list) { ? } Here ‘arg-list’ may be empty if you don’t want the function to take any arguments. If you remember from the post Creating a Simple Visitor Counter in PHP, we’d stated that no matter whether the function returns a value or not, no return type has to be specified for the function (unlike C/C++). a. Functions having parameters function myfunc($a) ... More About: User , Defined
Sorting Arrays in PHP?Some Sorting Functions
2008-05-07 11:59:00 Oh, so you want to learn how to sort arrays in PHP. But, did I tell you that PHP has inbuilt sorting functions. Well, I’ve now! So this post will not be anything but discussion on those sorting functions. Let’s look at them without wasting any more time. sort() Function This function, as the name suggest can be used to sort single dimensional arrays in ascending order. PHP is intelligent enough to also sort string arrays very well besides numerical arrays. $ar=array(1,11,38,65,2,99); sort($ar); //gives array(6) { [0]=> ; int(1) [1]=> int(11)& nbsp;[2]=> // int(38)&n bsp;[3]=> int(56) [4]=>&n bsp;int(65) [5]=> int(99)&nb sp;} The optional second argument if specified can be SORT_REGULAR (default), SORT_NUMERIC or SORT_STRING. It can be used to explicitly tell the sort function the way you want the arrays to be sorted. Most commonly num... More About: Functions , Sorting , Arrays
Creating a Simple Visitor Counter in PHP
2008-05-06 09:05:00 Visitor counter is an easy way to track number of visitors coming to a web site, while sophisticated trackers can give detailed statistics of visitors, a simple visitor counter like the one we’re going tot create, would only track the number of times a web page (or site) has been requested. A simple visitor counter which only tracks number of pageloads is very easy to create. It is obvious that the number of pageloads has to be preserved across different runs so we need to store that data in a Database or file. To ease things we’ll use a file. Every time the page (with the script) is requested the value (number of pageloads) will be read from and incremented in the file. The initial value in the file should be 0. Here is the code: <?php //function function showCounter () { //open the&n bsp;file in read &&nbs p;write mode $fp=fopen("counte r.txt",... More About: Creating , Visitor , Simple
String Manipulation Functions in PHP II
More articles from this author:2008-05-05 11:18:00 PHP has got wide range of in-built functions for manipulating strings, in the post String Manipulation functions in PHP we discussed some of them which are frequently used/needed. Here we’ll be discussing about the rest of the functions, but again not all of them. implode() or join() function Prototype: string implode(string separator , array arr); This function does the opposite of explode() function. Explode divides a string into array of strings on a separator, this one joins an array of strings with a separator to form a string. join() and implode() functions are identical. $str="PHP is a web pr ogramming language."; $ar=explode(' ',$str); //now $ar contains each&nb sp;word of the strings&nbs p;as //different elements $str2=implode(' ',$ar); //again $str2 is joined&nb sp;using ' ' (spaces) //now $str=$str2 strstr() function... More About: Functions , Strings 1, 2, 3, 4, 5, 6 |



