Pages

Tuesday, January 25, 2011

Finding Minimum And Maximum Value

/**
 * Finding maximum value over an array of integer.
 * 
 * @param count int
 *     size of elements of data
 * @param data array
 *     data in an array format
 *
 * @return data[index] int
 *     max value of the data
 */
int findMax( int count, int data[]){
    int maxIndex = 0;
    int i;
    for( i=0; i<count; i++){
        if(data[i] > data[maxIndex])
            maxIndex = i;
    }

    return data[maxIndex];
}


/**
 * Finding minimum value over an array of integer.
 * 
 * @param count int
 *     size of elements of data
 * @param data array
 *     data in an array format
 *
 * @return data[index] int
 *     min value of the data
 */
int findMin(int count, int data[]){
    int minIndex = 0;
    int i;
    for( i=0; i<count; i++){
        if(data[i]<data[minIndex])
            minIndex = i;
    }

    return data[minIndex];
}


Wednesday, January 19, 2011

Breadcum

<p>
    <a href="javascript:getCountry()">Home</a>
    <span id="country"></span> 
    <span id="state"></span>
    <span id="city"></span>
    <span id="company"></span>
</p>
<div id="list"></div>
<script>

var countries = ["usa","indonesia","japan"];
var states = [
  ["california", "new york", "texas"],
  ["east java", "south sumatra", "west java"],
  ["honsu", "kyusu"]
];
var cities = [
  [
    ["mountain view", "palo alto", "san jose"],
    ["nyc", "columbia", "rokefeller"],
    ["dallas", "austin"]
  ],
  [
    ["surabaya", "malang", "jember"],
    ["medan", "natal", "batak"],
    ["bandung", "cirebon", "bogor"]
  ],
  [
    ["kyoto","osaka"],
    ["tokyo"]
  ]
];
var companies = [
  [
    [
      ["<h1>HeadQuarter of Google</h1>"], 
      ["<h1>Yahoo Headquarter</h1>"],
      ["<h1>Adobe Base Camp</h1>"]
    ],
    [
      ["un is here","bloomberg is here"],
      ["univ of columbia"],
      ["rockefeller center"]
    ],
    [
      ["dell may be hee","large airport is here"],
      ["like name of you"]
    ]
  ],
  [
    [
      ["unair is here","its also"],
      ["unibraw is here","umm also"],
      ["just for unej"]
    ],
    [
      ["usu"],
      ["none"],
      ["none also"]
    ],
    [
      ["itb","unpad"],
      ["none"],
      ["ipb"]
    ]
  ],
  [
    [
      ["none"],
      ["none also"]
    ],
    [
      ["tokyo univ"]
    ]
  ]
];

function getCountry(){
    var list = document.getElementById('list');
    list.innerHTML = "";
    for(i=0;i<countries.length;i++){
        list.innerHTML = list.innerHTML +
          "<a href='javascript:getStates("+i+")'>"+ 
          countries[i] + "</a><br/>";
    }
    var country = document.getElementById('country');
    country.innerHTML = "";

    var state = document.getElementById('state');
    state.innerHTML = "";

    var city = document.getElementById('city');
    city.innerHTML = "";
}

function getStates(k){
    var list = document.getElementById('list');
    list.innerHTML = "";
    for(i=0;i<states[k].length;i++){
        list.innerHTML = list.innerHTML + 
          "<a href='javascript:getCities(" +k+ "," +i+ ")'>" +
          states[k][i] + "<br/>";
    }
    var country = document.getElementById('country');
    country.innerHTML = ">> <a  href='javascript:getCountry()'>"+
       "Country</a>";

    var state = document.getElementById('state');
    state.innerHTML = "";

    var city = document.getElementById('city');
    city.innerHTML = "";
}

function getCities(k,l){
    var list = document.getElementById('list');
    list.innerHTML = "";
    for(i=0;i<cities[k][l].length;i++){
    list.innerHTML = list.innerHTML + 
      "<a href='javascript:getCompanies(" +k+ "," +l+ ","
      +i+ ")'>"+ cities[k][l][i] + "<br/>";
}
    var state = document.getElementById('state');
    state.innerHTML = ">> <a href='javascript:getStates(" +k+ 
      ")'>States</a>";

    var city = document.getElementById('city');
    city.innerHTML = "";
}

function getCompanies(k,l,m){
    var list = document.getElementById('list');
    list.innerHTML = "";
    for(i=0;i<companies[k][l][m].length;i++){
        list.innerHTML = list.innerHTML + "<h1>" + 
        companies[k][l][m][i] + "</h1><br/>";
    }
    var state = document.getElementById('city');
    city.innerHTML = ">> <a href='javascript:getCities(" +k+ 
        "," +l+ ")'>Cities</a>";
}

getCountry()
</script>    

<style>
    a { text-decoration: none; text-transform: capitalize;}
    h1 { text-transform: capitalize; }
</style>   

Tuesday, January 11, 2011

Writing XML Document

/**
 * This class provide framework on how to write to xml file.
 * <?xml version="1.0"?>
 * <data>
 *    <entry>
 *        <name>...</name>
 *        <city>...</city>
 *    </entry>
 * </data>
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : january 11, 2010
 * @version : 1.0
 */


class XMLWrite{
   
    /** 
     * constructor service
     * @param filename of the xml document
     */
    public function __construct($file){
        $this->file = $file;
        $this->contents = file_get_contents($this->file);

    }

    /**
     * a method to insert the data to the xml document
     * @param the first text element
     * @param the second text element
     */
    public function insert($name,$city){
        $this->contents = str_replace("</data>","",
                                      $this->contents);
        $new_contents = $this->contents
                        ."<entry><name>".$name."</name>"
                        ."<city>".$city."</city></entry></data>";
        file_put_contents($this->file,$new_contents);
    }
}

Thursday, January 6, 2011

Pagination Using Javascript

function Pagination(d){
 this.data = d;
 this.item = 10;
 this.numPage = Math.ceil(this.data.length/this.item);
}

Pagination.prototype.setFirstPage = function(){
 var arr = new Array();
 for(i=0;i<this.item;i++){
  arr[i] = this.data[i];
 }
 return arr;
}

Pagination.prototype.setLastPage = function(){
 var arr = new Array();
 var startLastIndex = (this.numPage-1) * this.item;
 for(i=0;i<(this.data.length-startLastIndex);i++){
  arr[i] = this.data[startLastIndex];
  startLastIndex++;
 }
 return arr;
}

Pagination.prototype.next = function(){
 
}

Pagination.prototype.previous = function(){

}

Pagination.prototype.displayAtPage = function(n){
var arr = new Array();
 n = n-1;
 var startIndex = n * this.item;
 for(i=0;i<(this.item);i++){
  arr[i] = this.data[startIndex];
  startIndex++;
 }
 return arr;
}

Wednesday, January 5, 2011

Ajax Presentation

<div id="container">
    <div id="one">
        <a href="javascript:lol(0)">Home</a> |
        <a href="javascript:lol(1)">Page</a> |
        <a href="javascript:lol(2)">Setting</a>
        <div id="logout" style="display:inline; float: right; ">
            <a href="javascript:">Log Out</a>
        </div>
    </div>
    <div id="two">

    </div>
    <div id="three" style="width: 100%; height: 50px;">

    </div>

</div>

<style>
    div { margin: 0px; }
    body { font-family: arial; font-size: 12px; }
    a { text-decoration: none; }
    a:hover { text-decoration: underline; }
    #one { border-bottom: 1px solid #af5; padding: 0 0 3 0px;
    margin: 0 0 5 0px; }

</style>

<script>

var home = [
    "<a href=\"javascript:enter('fuchsia')\">Indonesia</a> | "
    +"<a href=\"javascript:enter('red')\">Malaysia</a> | "
    +"<a href=\"javascript:enter('cornsilk')\">Filipine</a>",

    "<a href=\"javascript:enter('pink')\">New Page</a> | "
    +"<a href=\"javascript:enter('yellow')\">Edit Page</a>",

    "<a href=\"javascript:enter('brown')\">Template</a> | "
    +"<a href=\"javascript:enter('lime')\">Layout</a>"
    ];

function lol(p){
    for(i=0;i<home.length;i++){
        if(i==p)
            document.getElementById('two').innerHTML = home[i];
    }



function enter(dd){
    document.body.style.backgroundColor = dd;
}

</script>

Sunday, January 2, 2011

Traffic Animation Using JS

/**
 * This framework provide simple traffic animation using
 * javascript style programming.
 * 
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @date    : January 2, 2011
 * @version : 1.0
 */

<h1>Traffic Animation</h1>
<div id='satu'></div>


<script>

function TA(w,h){
    this.ta = document.createElement('div');
    this.ta.setAttribute('style','width:'+w+'; height:'+h+'; 
     padding: 2px; border:1px solid #aaa; position:relative;
     margin:0 auto;overflow: hidden;');
}

TA.prototype.create = function(){
    return this.ta;
};

TA.prototype.setMobil = function(lol){
    this.ta.appendChild(lol);
};


function Mobil(c,w,h){
    this.ta = document.createElement('div');
    this.ta.setAttribute('style','width:'+w+'; height: '+h+';
      background: '+c+'; position: relative; margin: 2px; ');
    this.i = 0;
}

Mobil.prototype.create = function(){
    return this.ta;
};

Mobil.prototype.run = function(s){
    this.ta.style.left = this.i;
    this.i = this.i + 1;
    var lol =  this;
    setTimeout(function(){lol.run(s);},1000/s);
};

var satu = document.getElementById('satu');
var lol = new TA('80%');
var a = new Mobil('red',30,10);
var b = new Mobil('blue',20,10);
var c = new Mobil('green',25,10);
var d = new Mobil('brown',25,10);
a.run(50);
b.run(70);
c.run(260);
d.run(40);                                                                        
lol.setMobil(a.create());
lol.setMobil(b.create());
lol.setMobil(c.create());
lol.setMobil(d.create());
satu.appendChild(lol.create());

</script>