Tech Guide Blog

Tech Guide Programming Tutorial Tips Tricks

Archive for the ‘Programming’ Category

AJAX Introduction for Beginners

Posted by Admin On July - 3 - 2010

Asynchronous JavaScript and XML or AJAX in brief, is a method we use to access data from server asynchronously, utilize JavaScript object, XMLHttpRequest. It called asynchronous because we in client-side, don’t have to refresh the page when do request to the server, make the web page more interactive, dynamic and faster than old way we create a web page.

Comparasion between Classic and Ajax

Comparison between Classic and Ajax

Comparison between Classic and Ajax

Comparison between Classic and Ajax

So how we can use it? The example below needs a server to try, you can use XAMPP or your web-server to give it a try. Create an Ajax Script first, I put it in head tag.

<html>
<head>
<script language="javascript" type="text/javascript">
num=1;
function ajaxFunction(){
	// Create an AJAX variable, name doesn't have to be same
	var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				// Error from browser
				alert("ERROR: Reinstall your browser.");
				return false;
			}
		}
	}

	ajaxRequest.onreadystatechange = function(){
		// ready state = 4, the request has been processed, ready to get the data
		if (ajaxRequest.readyState == 4)
		{
			//we put the data requested to id = idn in your page
			document.getElementById("idn").innerHTML = ajaxRequest.responseText;
		}
	}
        // we send a variable to server , a var variable is filled with num value
	ajaxRequest.open("GET", "a.php?var="+num, true);
	ajaxRequest.send(null);

	num = num + 1;
	if (num == 5)
	num = 1;
}
</script>
</head>

How we call the function? Just create a button or something to trigger the function. For example when we press a button in that page.

<body>
<input type="button" value="press" onclick="ajaxFunction()" />
<br/>
<div id="idn">
This is where the request will be placed.
</div>
</body
</html>

I’ve mentioned php file “a.php” to receive the request, then we should create a php file with same filename mentioned above.

<?
echo $_GET['var'];
	if ($_GET['var'] == 1)
		echo "hahaha";
	else if ($_GET['var'] == 2)
		echo "hihihi";
	else if ($_GET['var'] == 3)
		echo "huhuhu";
        else if ($_GET['var'] == 4)
		echo "hehehe";
        else if ($_GET['var'] == 5)
		echo "hohoho";
?>

The output is depend on how many times you click the button. The first time you click the button, it should say “hahaha”, because the initial value of num variable is 1. However, if var value is 5, it will reset to 1 after the server processed the request, so the “hohoho” will never showed up.

Popularity: 2% [?]

Conditional in PL/SQL* Oracle 10g

Posted by Admin On January - 16 - 2010

In PL/SQL* Oracle, we can use conditional statement like other programming language, such as CASE and IF statement. How to do it in PL/SQL? Here’s the syntax for IF statement.

IF condition THEN
  statements;
[ELSIF condition THEN
  statements;]
[ELSE
  statements;]
END IF;

Just remember, I write ELSIF,  it’s on purpose and it’s not mistyping, in this case we write “else if” in PL/SQL as ELSIF. There is an example for IF statement usage in PL/SQL. This statement will ask your age and show output based on your age input.

SET SERVEROUTPUT ON
SET VERIFY OFF
ACCEPT age PROMPT 'Insert your age'
DECLARE
  age NUMBER := &age;
BEGIN
  IF age < 17 THEN
    DBMS_OUTPUT.PUT_LINE('You are teen');
  ELSIF age < 56 THEN
    DBMS_OUTPUT.PUT_LINE('You are adult');
  ELSE
    DBMS_OUTPUT.PUT_LINE('You are old');
END;
/

Just a little different than IF, CASE expression selects a result and returns it. The values returned by CASE is used to select one of several alternatives. The CASE syntax is shown below.

CASE selector
  WHEN axpression1 THEN result1
  WHEN expression2 THEN result2
  ...
  WHEN expressionN THEN resultN
  [ELSE resultN+1]
END;
/

For the example of CASE expression, we can use IF statement example above, just change it to CASE expression. We add text variable to store the return values from CASE expression since CASE return values.

SET SERVEROUTPUT ON
SET VERIFY OFF
ACCEPT age PROMPT 'Insert your age'
DECLARE
  age NUMBER := &age;
  text VARCHAR2(20);
BEGIN
  text:=
    CASE
      WHEN age < 17 THEN 'You are teen'
      WHEN age < 56 THEN 'You are adult'
      ELSE 'You are old'
    END;
  DBMS_OUTPUT.PUT_LINE(text);
END;
/

Example above didn’t use selector after CASE expression, you can use selector as alternatives way. I don’t really use CASE with selector anyway, but I will give you an example with selector below, I hope this can help.

SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
  grade CHAR(1) := UPPER('&grade');
  text VARCHAR2(20);
BEGIN
  text :=
    CASE grade
      WHEN 'A' THEN 'Excellent'
      WHEN 'B' THEN 'Very Good'
      WHEN 'C' THEN 'Good'
      WHEN 'D' THEN 'Bad'
      WHEN 'E' THEN 'Poor'
      ELSE 'Not valid grade'
    END;
  DBMS_OUTPUT.PUT_LINE(text);
END;
/

I think it’s enough for Conditional in PL/SQL Oracle tutorial, maybe this can help you little, feel free to give comment to improve my blog.

Popularity: 37% [?]

Make Sequence in PL/SQL Oracle

Posted by Admin On October - 11 - 2009

After a long sleep, I’m back and I want to start with a new topic, database. Okay, the database I’m talking about is Oracle, with using PL/SQL commands. The first time of this topic, I will talk about making sequence in Oracle. What is sequence? We can imagine sequence as a table that keep, of course, sequence numbers, this will be useful when you need to make sequence of numbers automatically, so you don’t have to make a new column that save those numbers or insert it manually.

To make a sequence we can use some options:

  1. START WITH n. When we first create the sequence, that sequence will be started with n.
  2. INCREMENT BY n. Define the increment value of your sequence, if it isn’t declared, it will set to 1.
  3. MINVALUE n. Define the minimum value of the sequence can generate, if it isn’t declared, the sequence is assumed have no minimal value (NOMINVALUE).
  4. MAXVALUE n. Define the maximum value of the sequence can generate, if it isn’t declared, the sequence is assumed have no maximum value (NOMAXVALUE).
  5. CYCLE. Allow sequence to back with MINVALUE if sequence has reached its MAXVALUE. If it isn’t declared, sequence will not back to MINVALUE (NOCYCLE).
  6. CACHE. Allow object sequence to do caching from the values that will be generated to increase performance. If it isn’t declared, sequence will not use cache (NOCACHE).

How to declare it

CREATE SEQUENCE sequence_name
START WITH 2
INCREMENT BY 3
MAXVALUE 98
CYCLE;

To use a sequence, we have to know that sequence is similar to struct variable. It have two virtual variable, CURVAL and NEXTVAL.  When we read the value of NEXTVAL, the CURVAL field will be filled with NEXTVAL, and NEXTVAL value will increase automatically by 1 or your setting with INCREMENT BY n. This is a example how to do it

INSERT INTO table_name values(sequence_name.NEXTVAL);

To change the settings, you can use alter command

ALTER SEQUENCE sequence_name
INCREMENT BY 1
START WITH 1;

To delete a sequence you can use drop command

DROP SEQUENCE sequence_name;

Well, that’s it for now, hope this tutorial can help you. Feel free to comment here if you have any questions.

Popularity: 82% [?]