Thursday, August 19, 2010

Add a new record in existing XML file

Add a new record in existing XML file



The xml file having the columns as listed below:
ID,author,title,url



full xml file's format is listed below:

blogs.xml
=====================================================
<?xml version="1.0"?>
<blogs>
    <article>
        <ID>1</ID>
      <author>Amit</author>
      <title>Auto Resume Broken Progress Bar File Uploader Applet</title>
      <url>http://amitkgaur.blogspot.com/2010/10/file-upload-applet-with-auto-resume.html</url>
  </article>
    <article>
        <ID>2</ID>
      <author>Amit</author>
      <title>Screen Recorder</title>
      <url>http://amitkgaur.blogspot.com/2010/09/desktop-screen-recorder.html</url>
  </article>
</blogs>
=====================================================


and you want to add a new record(s), then use the listed below my php code.
You need to change it according your requirements.

addNew.php
=====================================================
<?php
$fullXmlFile="blogs.xml";

$ID="3";
$author="Amit Gaur";
$title="How to get clipboard data from webpage.";
$url="http://amitkgaur.blogspot.com/2011/06/how-to-get-clipboard-data-from-webpage.html";

$xmlObj = simplexml_load_file($fullXmlFile);
$sxeObj = new SimpleXMLElement($xmlObj->asXML());
$articleObj = $sxeObj->addChild("article");
$articleObj->addChild("ID", $ID);
$articleObj->addChild("author", $author);
$articleObj->addChild("title", $title);
$articleObj->addChild("url", $url);
$sxeObj->asXML($fullXmlFile);

The above code will add the new record at the last position in the xml file.
=====================================================





Wednesday, August 18, 2010

Google chart api 3d

Here is the simple example of Google 3d chart. just use listed below code.
I have everything explained in commented.

Google 3D chart
Google 3D Chart.















<html>
<head>
<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type="text/javascript">
    // Load Google Visualization API with piechart package.
    google.load('visualization', '1', {'packages':['piechart', 'imagepiechart', 'barchart','imageBarChart','linechart']});
    // load API.
    google.setOnLoadCallback(initChart);
    // Populates datas and draw it.
    function initChart() {
        var dTable = new google.visualization.DataTable();
        // Add two columns
        dTable.addColumn('string', 'Names');
        dTable.addColumn('number', 'Percentage');
       
        //Add fie rows
        dTable.addRows(5);
       
        //set the 5 values
        dTable.setValue(0, 0, 'Java');
        dTable.setValue(0, 1, 50);
       
        dTable.setValue(1, 0, 'ASP.Net');
        dTable.setValue(1, 1, 30);
       
        dTable.setValue(2, 0, 'PHP');
        dTable.setValue(2, 1,10);
       
        dTable.setValue(3, 0, 'ASP');
        dTable.setValue(3, 1, 7);
       
        dTable.setValue(4, 0, 'Java Applet');
        dTable.setValue(4, 1, 3);
        //draw it   
        var pChart = new google.visualization.PieChart(document.getElementById('programmingLanguages3DChart'));
        pChart.draw(dTable, {width: 400, height: 240, is3D: true, title: 'Programming Languages 3D Chart'});
    }
</script>
</head>
<body>
    <div id='programmingLanguages3DChart' ></div>
</body>
</html>

Thursday, August 12, 2010

Search any word in entire tables of mysql database

Search any word in entire tables of mysql database

Suppose there are lot of tables in the database. and you want to search a word or sentence in the tables.
For this you need to look into each table, which having varchar or text or any character based type fields.
It is very lazy, tough and time killing task to look into each table for those fields manually and then generate it's sql manually.

I have recently worked on this type of task,so I have created this PHP code. It will show the sql in where it will find the search term. You can convert it in any other sql for example sql server, oracle etc.



 <?php

class DBUtil {
    public $utilMySqlServerIP;
    public $utilDB ;
    public $utilUser;
    public $utilPass;
    public $link,$isDBConnect;
    public function __construct() {
        $this->utilMySqlServerIP = "localhost"; $this->utilUser = "root"; $this->utilPass = "";
        $this->utilDB = "realated_se4"; $this->utilAns=false;
        try {
            $this->link = mysql_connect($this->utilMySqlServerIP, $this->utilUser, $this->utilPass);
            if (!is_resource($this->link)) { $this->isDBConnect=false; }
            else { $this->isDBConnect=true; }
            mysql_select_db($this->utilDB,$this->link);
        } catch(Exception $ee) { $this->link=false; }
    }
  
    public function showTableNames() {
        try    {
            $result = mysql_query("show tables", $this->link);
            if(mysql_num_rows($result)>0) { return $result; } else { return false; }  
        } catch(Exception $ee) { return (false); }
    }
  
    public function showTableColumn($tblName) {
        try    {
            $result = mysql_query("SHOW COLUMNS FROM ".$tblName, $this->link);
            if(mysql_num_rows($result)>0) { return $result; } else { return false; }  
        } catch(Exception $ee) { return (false); }
    }
    public function exeQuery($query, $isPrint=0) {
        try    {
            if($isPrint==1) { echo $query; }
            $result = mysql_query($query, $this->link);
            if(mysql_num_rows($result)>0) { return $result; } else { return false; }  
        } catch(Exception $ee){ return (false); }
    }
    public function __destruct() { }
}

////////////////////////////////////////////////////////
$db=new DBUtil();
$tbRs=$db->showTableNames();
$term="search term";
$number=0;
$tmpSql=" select [COLS] from [TABLENAME] where [WHERES]";
while($tbName = mysql_fetch_array($tbRs, MYSQL_BOTH)) {
    $colRs=$db->showTableColumn($tbName[0]);
    $startSql=$tmpSql; $fnd=0; $comma=",";     $cols=""; $where="";
    while($col = mysql_fetch_array($colRs,MYSQL_BOTH)) {
         $vFound=stripos($col[1], "varchar");
        $tFound=stripos($col[1], "text");
        $charFound=stripos($col[1], "char");
        $longFound=stripos($col[1], "longtext");
        $mediumFound=stripos($col[1], "mediumtext");
        $tinyFound=stripos($col[1], "tinytext");
      
        if($vFound!== false || $tFound!== false || $charFound!== false || $longFound!== false || $mediumFound!== false || $tinyFound!== false ) {
            if($fnd==0) {
                $fnd=1;      $cols.="`".$col[0]."`";  $where.= "`".$col[0]."`"." like '%".$term."%' ";
            } else {
                $cols.=","."`".$col[0]."`";      $where.=" or "."`". $col[0] ."`"." like '%".$term."%' ";
            }
        }
    }
    $startSql=str_replace("[TABLENAME]", $tbName[0], str_replace("[WHERES]", $where, str_replace("[COLS]", $cols, $startSql)));
    if(stripos($startSql,"like '%")!== false) {
        $rs=$db->exeQuery($startSql, 0);
        if($rs) { echo "<p></p>[".(++$number)."]== ".$startSql."<hr>";    }
    }
}
?>

Post data in Facebook wall

Post data in Facebook wall


 Just wait for the PHP page, that will post your message(s) in facebook wall.....




 

Get mac address ip address in jsp java

Get MAC address (Physical Address) and IP Address of client machine in jsp java

How to get MAC address (Physical Address) and IP Address of client machine.
listed below code that is show client machine's MAC address
(Physical Address) and IP Address.

use listed below code and you will find something like that:

100.202.202.201
11-26-7Z-50-89-9B

 The "100.202.202.201" is the IP Address and "11-26-7Z-50-89-9B" is the
Mac Address of the client machine.


import these packages:

<%@ page import="java.net.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>


<%
InetAddress inetAddress;
StringBuilder sb = new StringBuilder();
String ipAddress="",macAddress="";
int i=0;
try {
    inetAddress=InetAddress.getLocalHost();
    ipAddress=inetAddress.getHostAddress();
    NetworkInterface network=NetworkInterface.getByInetAddress(inetAddress);
     byte[] hw=network.getHardwareAddress();
    for(i=0; i<hw.length; i++)
        sb.append(String.format("%02X%s", hw[i], (i < hw.length - 1) ? "-" : ""));   
    macAddress=sb.toString();
} catch(Exception e) {
    out.print("<br/>"+e.toString());
    macAddress="-";
}
out.print("<br/>"+ipAddress);
out.print("<br/>"+macAddress);
%>

Wednesday, August 11, 2010

Check if an element exists in jQuery

Check if an element exists in jQuery


Some times you need to check element in jQuery that is it exists or not

you can use listed below code:


if ($('#txtID).length) { 
        // put here your code 
    }