Sunday, December 27, 2009

PHP: Send information through URL

This lesson deals with a very important part of PHP and that is using the URL to send information between pages. I will be using very basic examples to show how things work. But it is up to you how well you can use these things for advance use.

To start with, there are different ways to pass information between pages. One very crude method will be to send the information through URL. For example you may have seen a lot of URL’s like this: http://www.domainname.com/index.php?id=1&name=xyz

This is what we call sending information through the URL. In the above example, two separate values are being passed, i.e. the “id” with a value of 1 and also the “name” with a value xyz.

We can fetch these values in another page and then use them for anything. So, to do that, let’s look at some code.

The plan is to make a link on one page which will pass the information to the second page. We will not be using a very complicated HTML code, so anyone can understand this.

So, the name of my first page is "tutorialPage01.php" and here is the code for the same:

<?php
echo '<a href="tutorialPage02.php?id=1&username=John">Click here to pass the information to the next page</a>';
?>

This is where I am sending the information with different variables. Now, we will set up the next page which is “tutorialPage02.php”. This is the page which we have linked to in the link tag. This page will take the information from the URL and will display the same on the page.

<?php
echo $_GET ['id'];
echo "<br />";
echo $_GET ['username'];
?>

Here we are doing very simple things. As the variable's values are already in the URL which is passed, we are directly displaying the values of the same on the page. First is the printing of the values stored in ‘id’, then we used a break operator to go to the next line and then we are printing the value stored in 'username'.

Saturday, December 5, 2009

Simple calculator using Javascript

The basic idea behind this tutorial is to use javascript to get a simple calculator which will take inputs from user like the two numbers and the operation that the user intends to perform and show the result.

Here, I have used a nested loop to show how they work. That is the reason this program will be actually difference rather than subtraction. Meaning, it will never give you a negative number. It will automatically subtract the smaller number from the larger number. And the same holds true for division. But if you understand the program, you can easily tweak the nested loop to get that thing solved. After all, you also need to work on something.

Ok. Lets start with the HTML code for the page. I have not included the javascript file instead of using it on the page. It is a good practice to keep all scripts in one file and then include them instead of having them on the HTML page.



The html file is here

Not able to embed the html code here, so its better to have the page physically uploaded and then sending a link. Anyways, check the link and you will get the HTMl code for the page.

Now we will look at the javascript and understand what is happening there.
// this function will take two numbers, an operator and then multiply the numbers as per the selected operator and return the result.
function doCalculation (input1, input2, operator){
//handling addition
if (operator == "Addition") {
return input1 + input2;
}
//handling multiplication
if (operator == "Multiply") {
return input1 * input2;
}
//handling division
if (operator == "Division") {
if (input1 > input2){
return input1 / input2;
}
else {
return input2 / input1;
}
}


//handling subtraction
if (operator == "Subtraction") {
if (input1 > input2){
return input1 - input2;
}
else {
return input2 - input1;
}
}


}

// this function takes the values from the form and shows the result as an alert and also in the text box.
function doSimpleCalc() {
number1 = parseFloat(document.calculator.number1.value);
number2 = parseFloat(document.calculator.number2.value);
//result = number1 + number2;
selectedOption = document.calculator.operator.value;
//alert (selectedOption);
result = doCalculation(number1, number2, selectedOption);


alert (result);
document.calculator.result_text.value = result;
}

Note:

If you see there are two functions in the java script. I could have done the whole thing using one function, but then the function would have been form dependent. Meaning, if my function is calculating and also displaying the result then it is interacting with the HTML of the page, which means it knows about the page and depends on them. If we change something on the page later on, our script might need to change. And also, we may have to write same code for different pages. So, its best to make on function which will do the main function. Here the main function is doCalculation. It takes the two numbers and the operator and will return the result. This way I can use this function where ever I want to and I know it will work without any problem. Try to make the functions as less complicated as possible so that you can use them anywhere.

Ok, so the doCalculation function is simple to understand. It takes three parameters input1, input2 and operator. The first two are the numbers which we will get from the user through the text box. And the third one is the operator which the operator will select from the drop down select option.

Tuesday, October 13, 2009

Reseting the auto increment value n an mySql database field

I came across this problem that I wanted to reset my auto increment number for the id field which was my primary key. I deleted a few records and so it was now difficult to get the number to start from 1 again and get a nice sequence. One obvious thing to do was manually edit it as my content was not huge. But still I found a correct and automated way to do that:

I made it possible by using PHP's while loop. [NOTE: I will just give the main structure.]

Select ID from
order by ID

then use a while/for loop to iterate through the results, and update each row with a new number for the ID.. you could use a PHP variable to track the current ID number, and increment it with each loop...

Update
set ID=???

Tuesday, July 28, 2009

Refining the trace statements to get selected tags from XML




In this tutorial we will try and get going with the xml file that we have just loaded in to our flash environment and do some combinations to know how things go.

You can always refer the first part of this series to know how we loaded the xml file and traced out the xml content to the output window. Now we are getting the raw data in the output window in xml format. Now we will try to get a few specific tags being listed and also try out some conditions.

The first part code is here:

var xmlLoader:URLLoader = new URLLoader();

var urlRequest:URLRequest = new URLRequest("schoolXML.xml");
xmlLoader.addEventListener(Event.COMPLETE, xmlLoadedFunc);

xmlLoader.load(urlRequest);

var schoolXML:XML = new XML();
schoolXML.ignoreWhitespace = true;

function xmlLoadedFunc (evt:Event):void{

schoolXML = XML(xmlLoader.data);
trace (schoolXML);

}

Now we will play around with the previous code.

function xmlLoadedFunc (evt:Event):void{
schoolXML = XML(xmlLoader.data);
trace (schoolXML.student.name);
}

Here, I have just changed the trace statement a little. I have added ".student.name” which now tells the compiler that we are not looking for the complete data but just the data in name which is under student tag which is actually inside the xml file. So if we do this, we can see the list of students. But even now we can see the data in xml tags.

If we like to look at any specific item number then even we can do that. We just have to remember that in the variable all the xml data is stored in the form of array. And so if we have to call any particular data from xml then we have to use the syntax that we use to call data in arrays. And always the numbering starts from 0. So let’s say I want to see the details of the student in the 4th tag. To do this, I have to understand that the record should be stored in array number 3. So I will modify the last statement to something like this

function xmlLoadedFunc (evt:Event):void{
schoolXML = XML(xmlLoader.data);
trace (schoolXML.student.name[3]);
}

And we can get the desired output.

Now let’s get into more realistic situations. Like want to see how many students scored marks less than 40. May be they will need extra care and so I want to list out the names. So for this, we have to use conditions to get the desired output. Well it may sound a bit difficult, but let me tell you its very easy. As if you are talking to the computer and it will understand the same instruction.

trace (schoolXML.student.(percentage < 50));

yes, this is the trace statement in the function which will give us the desired output. Like if you are using the same xml that I am using then you will get a list of only two students who scored less than 50.

In this trace statement the instruction is very simple, we are asking to trace the records in schoolXML.student and then in the percentage we are giving the condition and that is why we have it inside the brackets.

Saturday, July 25, 2009

Loading XML data into Flash through AS 3.0

Hi, this is my first tutorial and so I will start with a very basic tutorial on how to load xml data into flash using action script 3.0. with E4X, it is very easy to deal with xml data if we compare to the previous version.

So without wasting time, lets get started. I am using an xml file which I have created for a dummy school and I have entered data for 10 students.

As we are only going to load the xml data in to the flash environment, I am not explaining too much about the xml structure and the details cause I want to keep this one real quick and simple.

var xmlLoader:URLLoader = new URLLoader();
var urlRequest:URLRequest = new URLRequest("schoolXML.xml");

Here we declare two variables. The first one is xmlLoader which is an “URLLoader”. And the second is urlRequest which is where we will store the url for the xml file. Var is a keyword used to tell the compiler that we are declaring a variable. Then we write the name of the variable which in this case is xmlLoader and urlRequest. URLLoader and URLRequest are classes in flash with specific function. URLLoader loads data from the url, and URLRequest is used to pass a url path.

The next part is where we define an event listener. This is where flash understands that the data in the variable is loaded and it acts as instructed when the event listener is triggered. In this case, we are instructing the event listener to run a function which will do a certain set if instruction.

xmlLoader.addEventListener(Event.COMPLETE, xmlLoadedFunc);

But the event will not be triggered until the load is complete. And loading will not start until we give the command to load the data. So we instruct the complier to load the data.

xmlLoader.load(urlRequest);

before going to the function we do write a piece of code to declare a variable where we would like to keep all the xml data which will be loaded and also we set one property of the xml variable called ignoreWhitespace to true. This is active by default. But it is a good habbit to define that mannualy so that we are sure of the same. This is done because when we make the XML file, we tend to format them using a lot of tabs and spaces. But those tabs and spaces are just for visual reference and they are not important from the data point of view. So when we set the ignoreWhitespace property to true, all those spaces are taken care of automatically.

var schoolXML:XML = new XML();
schoolXML.ignoreWhitespace = true;

and then finally we write the function. The name of the function is already declared when we were defining the event listener.

function xmlLoadedFunc (evt:Event):void{
schoolXML = XML(xmlLoader.data);
trace (schoolXML);
}

In this function we define that the variable schoolXML will have XML kind of data from will come from the variable xmlLoader’s data property. And then finally we trace out schoolXML to see that all the xml data is beign traced out in the output window.

Get the xml file here