Tuesday, August 4, 2009

Port forwarding in Airtel Wireless Lan:

When you want computers outside your LAN to access your computer. Some examples can be when u set up a ftp server on my machine for other people to access, or you are running a bittorrent client or DC++.

Steps:
• Step 1 : Put an internal static IP on your machine.

o Open Network Connections.
o Right click on Local Area Connection and select Properties.
o Now select TCP-IP and click on Properties
o Put values as shown. The DNS Server values I have put are that opendns You can also put 192.168.1.1 to use the airtel dns






• Configuring the Airtel Modem.

o Open http://192.168.1.1/main.html in IE
o Go to Advanced Setup -> NAT -> Virtual Servers
o Server IP will be same as the IP Address as mentioned above, which is 192.168.1.10 i this case







o If you want to play Network Game, you can just select this from the dropdown. Click on Save/Apply and you are done.
o In case the service isn't pre-configured, such as Bittorrent. Click on Custom Service. Put the name bittorrent
o You would need to put which port to forward. bittorren typically runs on 19556, so put 19556 in all the four text fields i.e. External Port Start, External Port End, Internal Port Start and Internal Port End.
o Choose the protocol. In case of bittorrent, and in most other cases, it would be TCP. Click on Save/Apply.
o That’s it, enjoy playing games, using torrent and DC++
Please note that these steps are applicable only if you are not using any other routers (wireless or otherwise) with your modem.

Monday, August 3, 2009

exeute javascript expilictly in response

We were lucky enough to get this script, which is the most powerful script which can be used to execute the java script tags in the div tags in case of Ajax supersedes them. we used that for Cobol-CGI project

You can get response and execute script in that through this script

/*
Purpose : This function is used to execute the java script code which reside inside div tag because java script inside div tag doesn't executed.
*/
function executeTEmbeddedScripts(node,theResponse)
{
/// Damco //if(document.getElementById('contentdiv').document.getElementById("pageNavPosition")){
if(document.getElementById("pageNavPosition")){
/// Damco //node = document.getElementById('contentdiv').document.getElementById("pageNavPosition");
node = document.getElementById("pageNavPosition");
var bSaf = (myBindexOf(navigator.userAgent,'Safari',0) != -1);
var bOpera = (myBindexOf(navigator.userAgent,'Opera',0) != -1);
var bMoz = (navigator.appName == 'Netscape');
if (!node) return;
var myHead=document.getElementsByTagName('head');
if ((!bSaf)&&(!bOpera)&&(!bMoz)) node.innerHTML="  "+theResponse; // World's most retarded IE fix
var st = node.getElementsByTagName('script');
var strExec;
var scripts = st.length;
var i = 0;
for(j = 0; j < scripts; j++){
var scriptsAtStart = st.length;
if (bSaf) {strExec = st[i].innerHTML; st[i].innerHTML = "";}
else if (bOpera) {strExec = st[i].text; st[i].text = "";}
else if (bMoz) {strExec = st[i].textContent; st[i].textContent = "";}
else {strExec = st[i].text; st[i].text = "";}
try {
var myScript = document.createElement("script");
myHead.item(0).appendChild(myScript);
myScript.type = "text/javascript";
myScript.text=strExec;
var html = node.innerHTML;
// jscript would prematurely eject if it encountered the full closing tag, even encapsulated in a string object
var endScriptL = '<' + '/script>';
var endScriptU = '<' + '/script>';
var pos2 = myBindexOf(html,strExec,0);
var pos = myBindexOf(html,endScriptL,pos2) >= 0 ? myBindexOf(html,endScriptL,pos2) : myBindexOf(html,endScriptU,pos2);
pos += String(endScriptL).length;
html = html.substr(0,pos) + html.substr(pos);
node.innerHTML = html;
if(scriptsAtStart==st.length) i++;
} catch(e) {
alert("Script execution error: "+e);
}
}
}
}


Parameters :

node : Pass the id of the Div or Span in which script needs to be executed
theResponse : Pass the response that you are writing in the Div or Span



if anyone need help, pls contact me
and if it wrks for you do add you comments .

Client Side Paging

If anyone of you lukin out for a Client Side implemetation of paging using javascript , try out this.....


Add this in Script section of your page....


function Pager(tableName, itemsPerPage) {
this.tableName = tableName;
this.itemsPerPage = itemsPerPage;
this.currentPage = 1;
this.pages = 0;
this.inited = false;

this.showRecords = function(from, to) {
var rows = document.getElementById(tableName).rows;
// i starts from 1 to skip table header row
for (var i = 1; i < rows.length; i++) {
if (i < from || i > to)
rows[i].style.display = 'none';
else
rows[i].style.display = '';
}
}

this.showPage = function(pageNumber) {

if (! this.inited) {
alert("not inited");
return;
}

if (document.getElementById('next') && pageNumber ==1)
{
var oldPageAnchor = document.getElementById('next');
oldPageAnchor.className = 'pg-normal';
var oldPageAnchor = document.getElementById('prev');
oldPageAnchor.className = '';
}
else if (document.getElementById('prev') && pager.pages > pageNumber)
{
var oldPageAnchor = document.getElementById('prev');
oldPageAnchor.className = 'pg-normal';
var oldPageAnchor = document.getElementById('next');
oldPageAnchor.className = 'pg-normal';

}
if (document.getElementById('next') && pageNumber == pager.pages)
{
var oldPageAnchor = document.getElementById('prev');
oldPageAnchor.className = 'pg-normal';
var oldPageAnchor = document.getElementById('next');
oldPageAnchor.className = '';
}

var oldPageAnchor = document.getElementById('pg'+this.currentPage);
oldPageAnchor.className = 'pg-normal';

this.currentPage = pageNumber;

var newPageAnchor = document.getElementById('pg'+this.currentPage);
newPageAnchor.className = 'pg-selected';

var from = (pageNumber - 1) * itemsPerPage + 1;
var to = from + itemsPerPage - 1;
this.showRecords(from, to);
}

this.prev = function() {
if (this.currentPage > 1)
this.showPage(eval(this.currentPage) - 1);
}

this.next = function() {
if (this.currentPage < this.pages) {
this.showPage(eval(this.currentPage) + 1);
}
}

this.init = function() {
var rows = document.getElementById(tableName).rows;
var records = (rows.length - 1);
this.pages = Math.ceil(records / itemsPerPage);
this.inited = true;
}

this.showPageNav = function(pagerName, positionId) {
if (! this.inited) {
alert("not inited");
return;
}

var element = document.getElementById(positionId);
var pagerHtml;

if (pagerName == "none")
{
pagerHtml = '';
}
else
{
pagerHtml = ' « Prev | ';
for (var page = 1; page <= this.pages; page++)
pagerHtml += '' + page + ' | ';
pagerHtml += ' Next »';
}

element.innerHTML = pagerHtml;
}


}


var pager = new Pager('t1', 15);
pager.init();
if(pager.pages > 1)
{

pager.showPageNav('pager', 'pageNavPosition');
if (pager.pages != 0)
{
if(document.getElementById("pgnum") && document.getElementById("pgnum").value!="")
{
if(document.getElementById("pgnum").value > pager.pages)
{
pager.showPage(pager.pages);
}
else
{
pager.showPage(document.getElementById("pgnum").value);
}
}

else
{
pager.showPage(1);
}
}
}
else
{
pager.showPageNav('none', 'pageNavPosition');
}

and 2 use this in your page add the before div to the end of HTML section of you page, you should create a table for that so that its alignment is proper:






If you like this script, please post ...!

Monday, August 18, 2008

PHP, MySQL and Javascript : Create a folder tree structure using database














I recently had a problem of creating a folder tree structure.
 
I took the help of the CoolJsTree package and with the help of Ultratree Algoritham. I am thankful to Aitor Solozabal Merino
 

To learn how to dinamically build a tree from a MySql Table I write this utility PHP script code, for anyone like me that need it.








<?php

/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation in any version
of the License.This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

==============================================================================

Application  : Mysql DataFile Treeview using PARTIALLY

            Ultratree 1.1 FREE Server-Side Tree Builder


            (Algorithm with only ONE SQL query required)

            PHP dynamic tree node generation process to use with

            COOLjsTree (Free/Standard/Pro) 2.7.0 Javascript Client-Side


            management

Name Program : Mysql_DataFile_Treeview_Builder.php

Author       : Aitor Solozabal Merino (Spain)


Email        : aitor-3@euskalnet.net

Date         : 26-08-2005


Type         : php program utility

Description  : This utility build a data tree structure from a datafile in a

               MySql database


******************************************************************************

* Based partially on the FREE algorithm of UltraTree v.1.1 by Mike Baikov    *

* with only one query from the database and sorting the result               *


* Full original source script can be downloaded from http://tourbase.ru/zink *

******************************************************************************

* With the management of the FREE/Standard/Pro COOLjsTree javascript utility *

* Can be downloaded from  http://javascript.cooldev.com/scripts/cooltree     *

* Can be used the professional versión of this utility choosing the indicated*


* lines in the script code                                                   *


******************************************************************************

* To run this script you must follow the next steps                          *


******************************************************************************

* 1º Download the appropiate files to a subdir under your web root           *

*    You need the files : COOLjsTree.js or ( COOLjsTreepro.js )              *


*                         Tree_Styles.css                                    *


*    to the same directory of this script                                    *


*    Create "img" subdir with the files: b.gif (blank),                      *


*                                        e.gif (expanded),                   *


*                                        c.gif (collapsed),                  *


*                                        d.gif (document)                    *


******************************************************************************

* 2º you must create a MySql database to insert the new table and use it     *

*    with the SQL code below  or any other of your invention                 *


*    Take care that the code for the parent always have a value >=0          *

******************************************************************************

//SQL CODE TO CREATE AND POPULATE THE SAMPLE TABLE


use YOURDATABASE

drop table if exists `tab_treeview`;



CREATE TABLE `tab_treeview` (

   `treeview_cod` smallint(5) unsigned NOT NULL auto_increment,

   `treeview_name` char(25) NOT NULL default '',


   `treeview_desc` char(25) NOT NULL default '',

   `treeview_parent_cod` smallint(5) unsigned NOT NULL default '0',

   PRIMARY KEY  (`treeview_cod`)


) ENGINE=MyISAM ;

INSERT INTO `tab_treeview` VALUES (1,'root','folder',0);

INSERT INTO `tab_treeview` VALUES (2,'parent1','folder',1);

INSERT INTO `tab_treeview` VALUES (3,'parent2','document',1);

INSERT INTO `tab_treeview` VALUES (4,'parent3','folder',1);


INSERT INTO `tab_treeview` VALUES (5,'child1','folder',2);

INSERT INTO `tab_treeview` VALUES (6,'child2','document',2);

INSERT INTO `tab_treeview` VALUES (7,'grandchild1','document',5);

INSERT INTO `tab_treeview` VALUES (8,'grandchild2','document',5);


INSERT INTO `tab_treeview` VALUES (9,'child3','folder',4);

INSERT INTO `tab_treeview` VALUES (10,'child4','document',4);

INSERT INTO `tab_treeview` VALUES (11,'grandchild3','folder',9);

INSERT INTO `tab_treeview` VALUES (12,'grandgrandchild1','document',11);

INSERT INTO `tab_treeview` VALUES (13,'grandgrandchild2','document',11);


******************************************************************************

* 3º you must change some lines of the main code to set your MySql variables *

******************************************************************************

W A R N I N G S

Due to storing the full information of the tree directory in GLOBAL arrays, the perfomance

slow down when the number of nodes in the treeview grows up - is acceptable up to 3.000 nodes.

The purpose of this utility is learning about how to dinamically build a tree structure from

the SERVER-SIDE and manage it with a javascript utility in the CLIENT_SIDE.


*/

//******************************************************************************

function init_mysql_php_tree_process($host,$user,$userpass,$database,$sql_string){

    //sql string with only id, parent, name SELECT FIELDS FROM

    //mysql connection

    $mysql_connection = mysql_connect($host, $user, $userpass);


    mysql_select_db($database,$mysql_connection);

    //only this query is required

    $mysql_query_result=mysql_query($sql_string,$mysql_connection);


    //--------------------------------------------------------------------------

    build_data_tree($mysql_query_result);

    //--------------------------------------------------------------------------

    mysql_free_result($mysql_query_result);


    mysql_close($mysql_connection);

}

function build_data_tree($mysql_query_result) {

    //this is based on Ultratree algorithm


    while ( list($id_cod, $parent_cod, $id_name) = mysql_fetch_array($mysql_query_result) ) {

        $table[$parent_cod][$id_cod] = $id_name;


    } ;

    make_branch_tree(0, $table, 0);

    RETURN;

}


function make_branch_tree($parent_cod, $table, $level) {

    //recursive function based on Ultratree algorithm

    $list = $table[$parent_cod];

    asort($list); // the sorting


    while (list($key, $val) = each($list)) {

      $GLOBALS['tree_node_counter']++;


      $GLOBALS['tree_node_id'][$GLOBALS['tree_node_counter']]=$key;

      $GLOBALS['tree_node_name'][$GLOBALS['tree_node_counter']]=$val;


      $GLOBALS['tree_node_level'][$GLOBALS['tree_node_counter']]=$level;

        $GLOBALS['tree_node_parent'][$GLOBALS['tree_node_counter']]=$parent_cod;


        if ((isset($table[$key]))) {make_branch_tree($key, $table, $level + 1);} ;

    } ;


    RETURN;

}

function show_data_tree(){

    //show the data array ordered

   //start table


   echo "<p><a href='javascript: tree1.expandAll();'>open all</a> | <a href='javascript: tree1.collapseAll();'>close all</a> | <a href=".$_SERVER['PHP_SELF']."><Img SRC='img/tree_refresh1.gif' BORDER=0 alt='rebuild tree'> refresh</a></p>";


    echo "<table border=2 align=CENTER cellpadding=2 cellspacing=2>";

    ECHO "<tr><font size=3><b>

   Mysql DataFile Treeview using PARTIALLY<br>


   Ultratree 1.1 FREE Server-Side Tree Builder<br>

   (Algorithm with only ONE SQL QUERY required)<br>

   PHP dynamic tree node generation process to use with<br>

   COOLjsTree (FREE/Standard/Pro) 2.7.0 Javascript Client-Side management<br>


   <br>

   </b>

   </font>

    ";


   //first row table header

    echo "<tr>";

    echo "<td align=center>TREE</td>";


    echo "<td></td>";

    echo "<td align=right>DATABASE</td>";

    echo "<td align=right>" . $GLOBALS['database'] . "</td>";


    echo "<td align=right>TABLE</td>";

    echo "<td align=right>" . $GLOBALS['tablename'] . "</td>";


   //second row table subheader

    echo "</tr>";

    echo "<td align=center>ORDER</td>";


   echo "<td align=center>LEVEL</td><td></td>";

   echo "<td align=center>".$GLOBALS['cod_id']."</td>";


   echo "<td align=center>".$GLOBALS['cod_parent']."</td>";

   echo "<td align=center>".$GLOBALS['cod_name']."</td>";


   echo "</tr>";

   //rest of the rows table body

    for ($j=1;$j<=$GLOBALS['tree_node_counter'];$j++){


        echo "<tr >";

        echo "<td align=CENTER><b>".$j."</b></td>";


        echo "<td align=CENTER><b>".$GLOBALS['tree_node_level'][$j]."</b></td>";

        echo "<td></td>";


        echo "<td align=CENTER><b>".$GLOBALS['tree_node_id'][$j]."</b></td>";

        echo "<td align=CENTER><b>".$GLOBALS['tree_node_parent'][$j]."</b></td>";


        echo "<td align=CENTER><b>".$GLOBALS['tree_node_name'][$j]."</b></td>";

        echo "</tr>";


    }

   //end table

    echo "</table>";

}

function php_process_data_tree(){


    // this function build the node structure following the rules of the COOLjsTree utility

    $indent_spaces_of_level = chr(9);

    $var_tree= "var TREE1_NODES = [".chr(13); //naming convention


    $level=0;

    //loop starting in the begining of the tree ( root )

    for ($j = 1;$j <= $GLOBALS['tree_node_counter'];$j++){


       if ($level==0) {

            $level=1;


        }else{

            if ($level < ($GLOBALS['tree_node_level'][$j]+1)){


                $var_tree.=chr(13); //node end line without any special character only the carry return 13

                $level++; // increment the level variable only it is possible in one by one way


            } else {

                If ($level == ($GLOBALS['tree_node_level'][$j]+1)) {


                    $var_tree.= "],".chr(13); //node end line with the special characters "],"


                } else {

                    $var_tree.= "]".chr(13); // node end line with the special character "]" only. No comma


                    For ($i=($level-1);$i>($GLOBALS['tree_node_level'][$j]+1);$i--){


                   //closing levels until are equals

                        $var_tree.= str_repeat ($indent_spaces_of_level, ($i)) . "]".chr(13); // end of nodel levels with the special character "]" without comma


                    }

                    $level=$i;


                    if ($level>0) {


                        $var_tree.= str_repeat ($indent_spaces_of_level, ($level)) . "],".chr(13);  // if there are more levels the node end line with "]," characters


                    }

                }


            }

        }

        $var_tree.= str_repeat ($indent_spaces_of_level, ($level)) . "[{id:".$GLOBALS['tree_node_id'][$j]."},'" . $GLOBALS['tree_node_name'][$j] . "',null,null,";


    }

    If ($level>0){

        $var_tree.= "]".chr(13);


    }

    For ($i=($level);$i>1;$i--){

    //closing levels until levels are equal with end node level "]" character


        $var_tree.= str_repeat ($indent_spaces_of_level, ($i-1)) . "]".chr(13);

    }


    $var_tree.= "];".chr(13);

    // end of the php process tree node structure

    RETURN $var_tree; //returning the string with the full tree


}

function build_cooltree(){

//you can alter the commented parts of this code in case of

//cooljstree free/pro versions

//you can alter this function using a tree_format.js file in place of the

//code written for the tree_format variable

//you can test the output tree1_nodes.js file in place of the code written

?>


<html>

   <head>

      <title>Mysql DataFile Treeview</title>

      <!-- CSS FILE for treeview -->


      <link rel='stylesheet' type='text/css' href='tree_styles.css' />

      <!-- JS JAVASCRIPT FILE for treeview -->

      <!-- COOLjsTree PROFESSIONAL -->

      <!--<script type='text/javascript' src='cooltreepro.js'></script>-->


      <!-- COOLjsTree FREE -->

      <script type="text/javascript" src="cooltree.js"></script>-->


        <!-- JS FORMAT FILE for treeview -->

      <!-- <script type='text/javascript' src='tree_format.js'></script>-->

      <script type='text/javascript'>


      // Tree format definition

      var TREE_FORMAT = [

         //0. left position

         55,


         //1. top position

         70,

         //2. show +/- buttons

         true,


         //3. couple of button images (collapsed/expanded/blank)

         ["img/c.gif", "img/e.gif", "img/b.gif"],


         //4. size of images (width, height,ident for nodes w/o children)

         [16,16,0],

         //5. show folder image


         true,

         //6. folder images (closed/opened/document)

         ["img/fc.gif", "img/fe.gif", "img/d.gif"],


         //7. size of images (width, height)

         [16,16],

         //8. identation for each level [0/*first level*/, 16/*second*/, 32/*third*/,...]


         [0,16,32,48,64,80,96,112,128,144,160,176,192,208,224,240,256,272],

         //9. tree background color ("" - transparent)

         "",


         //10. default style for all nodes

         "clsNode",

         //11. styles for each level of menu (default style will be used for undefined levels)


         [],

         //12. true if only one branch can be opened at same time

         false,

         //13. item pagging and spacing


         [0,0],

      ];

      </script>   


      <!--<script type="text/javascript" src="tree1_nodes.js"></script>-->    

      <script type='text/javascript'>


         <!-- HTML CODE for my tree-view menu -->

         <?php echo $GLOBALS['tree']//you can comment this line and use tree1_nodes.js file;?>

      </script>   


   </head>

   <body>

      <script type='text/javascript'>

            <!-- COOLjsTree FREE -->


         var tree1 = new COOLjsTree('tree1', TREE1_NODES, TREE_FORMAT);

         <!-- COOLjsTree PROFESSIONAL -->

         <!-- var tree1 = new COOLjsTreePRO('Tree1', TREE1_NODES, TREE_FORMAT);-->


         <!--tree1.init();-->

         tree1.caption_onclick = function (_node) { 

            alert("Cod_ID= "+_node.nodeID); 


         }

         <!-- COOLjsTree FREE/STANDARD/PROFESSIONAL to expand/collapse the tree at the beginning -->

         tree1.collapseAll(true);


         <!--tree1.expandAll(true);-->

      </script>

      <!-- COOLjsTree PROFESSIONAL -->


      <!--<script type='text/javascript'>RedrawAllTrees()</script>-->

   </body>

</html>


<?

}//end build_cooltree function

//==============================================================================

//===========================  MAIN PROGRAM  ===================================

//==============================================================================



//MYSQL VARIABLES ( MUST BE CHANGED "Ad Hoc"  BY YOU )


$host     ="localhost";   // your mysql server

$user     ="root";        // your mysql username


$userpass ="lbsb2vb";     // your mysql user password to access to databases

$database ="test";        // your mysql database name


//one sample table

$tablename="tab_treeview";        // your mysql table name

$cod_id="treeview_cod";           //your mysql table fields


$cod_parent="treeview_parent_cod";//your mysql table fields

$cod_name="treeview_name";        //your mysql table fields

//another sample table

//$tablename="treetest";// your mysql table name


//$cod_id="catid";      //your mysql table fields

//$cod_parent="parcat"; //your mysql table fields

//$cod_name="name";     //your mysql table fields




//==============================================================================

//===========================  NOTHING TO CHANGE ===============================

//==============================================================================



//init globals process variables

unset($tree_node_counter);


unset($tree_node_id);

unset($tree_node_parent);

unset($tree_node_name);

unset($tree_node_level);


$tree_node_counter=0;



//string to query the table : must have a SELECT node id, node parent, node name

//FROM any tree table structure ORDER by node_parent  (very simple)

$sql="SELECT `".$cod_id ."`,`".$cod_parent ."`,`".$cod_name."` FROM `".$database."`.`".$tablename."`  order by `".$cod_parent."`";




// time benchamrk (optional): end time count, get benchmark result

$time_start = microtime(1);

//start procedure



init_mysql_php_tree_process($host,$user,$userpass,$database,$sql);




// time benchmark end

$time_end = microtime(1);

$time_elapsed = $time_end - $time_start;



show_data_tree();



printf("<br>UltraTree built in  %f seconds ", $time_elapsed);

// begin process to build the node structure for COOLjsTree javascript

// (free and commercial versions)

$time_start = microtime(1);


//start procedure



$tree=php_process_data_tree();



// time benchmark end

$time_end = microtime(1);

$time_elapsed = $time_end - $time_start;


//echo "<br>".$tree."<br>";

printf("<br>PHP process data tree in  %f seconds ", $time_elapsed);

//the tree structure can be written to disk and shown with a text editor


$connection_file=fopen("tree1_nodes.js","w+");

fwrite($connection_file,$tree);

fclose($connection_file);

//finally the structure must be passed to COOLjsTree to manage the tree

$time_start = microtime(1);


//start procedure



build_cooltree();



// time benchmark end

$time_end = microtime(1);

$time_elapsed = $time_end - $time_start;


printf("<br>finally COOLjsTree show data tree in %f seconds ", $time_elapsed);

//=============================================================================

//===========================  END PROGRAM  ===================================

//=============================================================================


?>







Anyone of you facing any problem implementing this pls post i ll try to help u to the maximum i can...