ICC WORLD CUP 2011 WATCH LIVE ONLINE

How to access database using PHP

1:32 PM Posted by: Isuru Jayathissa 0 comments

$db = "Netplexer"; // Here name of the database

$link = mysql_connect("localhost","root","" ); // Setp 1 , connection... .
// here I use default setting of Mysql database

if ( ! $link )

die( "Couldn't connect to MySQL" ); // error massage


mysql_select_db( $db, $link ); // Step 2 , select database
or die ( "Couldn't open : ".mysql_error() );

$query = "SELECT DST_IP,// Step 3 , Mysql query ,
COUNT(DST_IP) AS NumOccurrences
FROM IncomingTraffic
GROUP BY DST_IP
HAVING ( COUNT(DST_IP) > 1 )
ORDER BY NumOccurrences DESC";


// here we can use Select , Insert , drop , etc ..

$result=mysql_query( $query) or die (mysql_error());
$i=0;

while($row=mysql_fetch_array($result)) // separate data using arrays
{

$data []= $row['NumOccurrences'];
$dataIP []= $row['DST_IP'];

}

mysql_close( $link ); // Step 4 , Close

How to get time difference using php

6:09 PM Posted by: Isuru Jayathissa 0 comments

function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}

$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");

$now = time();
$unix_date = strtotime($date);

// check validity of date
if(empty($unix_date)) {
return "Bad date";
}

// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";

} else {
$difference = $unix_date - $now;
$tense = "from now";
}

for($j = 0; $difference >= $lengths[$j] && $j <>
$difference /= $lengths[$j];
}

$difference = round($difference);

if($difference != 1) {
$periods[$j].= "s";
}

return "$difference $periods[$j] {$tense}";
}

$date = "2009-10-07 14:00";
$result = nicetime($date);

echo $result;

How to get line of string from a text file

6:14 PM Posted by: Isuru Jayathissa 0 comments

function getstrline($searchstr, $file){$myfile = file($file);$key = array_search($searchstr, $myfile);
return (
"'$searchstr' at line nomber: $key");
}

Echo
getstrline("This is the String that we want to find", "sometest.txt");?>

How to remove line matching with string "Y" in a file using PHP

4:22 PM Posted by: Isuru Jayathissa 0 comments

//if you are call :
//delete_line
_string("you want to remove text" , "sometxtfile.txt");
//here delete 3rd line in
sometxtfile.txt file

function delete_line_string($find, $file)
{
$contents = array();
$new_contents = "";
$find = trim($find);
$return = false;

if(file_exists($file))
{
if(is_writable($file))
{
// open the file as an array, each line as an element
if($contents = file($file))
{
// remove extra whitespace from each line
$contents = array_map("trim", $contents);

// loop through each line and see if it's the one we want

foreach($contents as $line_number => $line)
{
$lowercase_line = strtolower($line);
$lowercase_find = strtolower($find);
if($lowercase_line == $lowercase_find)
{
// this is where we remove the line
unset($contents[$line_number]);
$return = true;
}
}

// put the lines back together with a \n between each
$new_contents = implode("\n", $contents);

// open the file and delete its old contents
if(!$handle = fopen($file, 'w'))
{
trigger_error("$file could not be opened for writing", E_USER_NOTICE);
return false;
}
else
{

if(fwrite($handle, $new_contents) !== false)
{
// it worked
return $return;
}
}
}
else
{
trigger_error("$file is not readable", E_USER_NOTICE);
return false;
}
}
else
{
trigger_error("$file is not writable", E_USER_NOTICE);
return false;
}
}
else
{
trigger_error("$file does not exist", E_USER_NOTICE);
return false;
}

}

How to DELETE n th line in a file using PHP

4:14 PM Posted by: Isuru Jayathissa 0 comments

function delete_line_by_number($line, $file)
{
$contents = array();
$new_contents = "";
$number_of_lines = 0;

if(file_exists($file))
{
if(is_writable($file))
{
// open the file as an array, each line as an element
if($contents = file($file))
{
$number_of_lines = count($contents);
if($line <= $number_of_lines)
{
// remove extra whitespace from each line
$contents = array_map("trim", $contents);

// remove the line in question
unset($contents[($line-1)]);

// put the lines back together with a \n between each
$new_contents = implode("\n", $contents);

// open the file and delete its old contents
if($handle = fopen($file, 'w'))
{
if(fwrite($handle, $new_contents) !== false)
{
// it worked
return true;
}
}
else
{
trigger_error("$file could not be opened for writing", E_USER_NOTICE);
return false;
}
}
else
{
trigger_error("$file only has $number_of_lines lines, so line $line could not be removed", E_USER_NOTICE);
return false;
}
}
else
{
trigger_error("$file is not readable", E_USER_NOTICE);
return false;
}
}
else
{
trigger_error("$file is not writable", E_USER_NOTICE);
return false;
}
}
else
{
trigger_error("$file does not exist", E_USER_NOTICE);
return false;
}

How to create XML file using PHP

2:57 PM Posted by: Isuru Jayathissa 0 comments

$strXML = "" ; // here insert root or root attribute

// Connect to the DB
$link = connectToDB();


$strQuery = "SELECT DST_IP,
COUNT(DST_IP) AS NumOccurrences
FROM traffic
GROUP BY DST_IP HAVING ( COUNT(DST_IP) > 1 )
ORDER BY NumOccurrences DESC";

$result = mysql_query($strQuery) or die(mysql_error());


if ($result) {
while($ors = mysql_fetch_array($result))
{
$strXML .= ""; // here insert child information
echo "Det GB Accses ";
echo $ors['DST_IP'];

}
}

mysql_close($link);

$strXML .= "
"; // here close the root


$filename = 'artists.xml'; // here put the xml file name
if (is_writable($filename)) {
if (!$handle = fopen($filename, "a+")) {
//fgets($handle);
echo "Cannot open ($filename)";
exit;
}
if (fwrite($handle, $strXML) === FALSE) {
echo "Cannot write to ($filename)";
exit;
}

echo "Success, wrote ($strXML) to ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}

Count the number of lines in a text file using PHP

2:48 PM Posted by: Isuru Jayathissa 0 comments

Extremely simple PHP sample to count and return the number of lines in a text file. The output can be customised to suit your own requirements.

= "somefile.txt";

$lines = count(file($file));

echo
"There are $lines lines in $file";

?>

Using image maps with JpGraph

11:57 AM Posted by: Isuru Jayathissa 0 comments

Image maps, or client side image (CSIM) as they are known is fully supported in JpGraph. It gives you the opportunity to create hot-spots in the graphs which allows you to build a set of "drill-down" graphs.

In the following section is based on the assumption that the reader is familiar with the basic concepts of client side image map in HTML.

To shortly recapitulate. Client side image maps consists of two parts. The first part is the actual image and the second part is a mapping that gives the coordinates for areas in the image which should be marked as hot spots (i.e. click-able by the user). The library can automatically generate these coordinate maps from a given graph.

Through out the manual areas of the graph that may be used as a hotspot is given in conjuction with the general description of that area.


*Since we normally call the graphing script directly in the "IMG" tag how do we get hold of the image map (which is available only in the image script) in this "HTML wrapper" script?

In JpGraph there is actually two ways of solving this.

  1. Use the preferred "builtin" way using the modified Stroke() method Graph::StrokeCSIM() instead of the standard Graph::Stroke() method.
  2. Directly use the Graph::GetHTMLImageMap() which gives you fine control at the expense of more complex coding.

The first (and preferred) way modifies the stroke method so that instead of returning an image (like the standard Stroke() method) StrokeCSIM() actually returns an HTML page containing both the image map specification and the correct "IMG" tag.

This of course means that it is necessary to treat an image map returning image script differently from a non-CSIM image script, for example you can't use it directly as the target for the "src" attribute of the "IMG" tag since it sends back an actual HTML page containing both an image tag together with an image map

What the hotspots represent depends on the type of plot you are doing. The following plot types and graph areas support image maps.

  • Line plots. Markers are hotspots.
  • Scatter plot. Markers are hotspots.
  • Pie Plots and 3D Pie plots. Each slice is a hotspot
  • All types of Bar graphs. Each bar is a hotspot
  • Legends
  • Text strings, for example titles and title of axis

There are two arguments to this method

  1. $aTargets, an array of valid URL targets. One URL per hot spot, for example if you have a 10 values bar plot you need 10 URLs. If the SetCSIMTarget() is applied to, for example, a text then of course only one URL target should be specified.
  2. $aAlts, an array of valid alt-texts. Many browsers (but not all) will show this text string if the mouse hovers over a hotspot.


Create clickable Graph using PHP

10:25 PM Posted by: Isuru Jayathissa 0 comments

There are two methods to do this:

  1. GettingHoldOfImageMap
  2. Using StrokeCSIM
I get 3D-Pie chart as a example ,


include ("../../../../jpgraph/src/jpgraph.php");
include ("../../../../jpgraph/src/jpgraph_pie.php");
include ("../../../../jpgraph/src/jpgraph_pie3d.php");

$graph = new PieGraph(600,600);
$graph->SetShadow();

$graph->title->Set("Incoming Traffic - IP Occurrences");
$graph->title->SetFont(FF_FONT1,FS_BOLD);

/// using this we can click Title///
$graph->title->SetCSIMTarget('#99','Incoming Traffic');

$p1 = new PiePlot3D($data);


///those targets can get using GET method//
$targets = array('?path = 11', '?path = 22','?path = 44','?path = 66','?path = 55');
$alts = $dataIP;

/// Because of Using this SetCSIMTargets method, we can click image///
$p1->SetCSIMTargets($targets,$alts);
$p1->SetLegends($dataIP);
$graph->Add($p1);

$graph->Stroke();
/// Using this STROKE method , get image to the web page ///

/* if we use this Stroke method only, we can not add any other item (ex: text, table,etc...). therefore I used a method to solve that problem .
  1. that is : save image another folder and send that image to the server and get back to the web page.

///this is the first method "1.
GettingHoldOfImageMap"


$graph->Stroke( "/opt/lampp/htdocs/Test/img/image001.png" ); /// Using this Stroke methode we can save image .
echo $graph ->GetHTMLImageMap ("myimagemap001" );

echo "< xxxx ISMAP USEMAP=\"#myimagemap001\" border=0" ; echo "width=\"600\" height=\"600\"> " ;
///note : you have to replace image path with the
xxxx . I can not give image path , because of Publishing problem .///

/// this is the second method
2.Using StrokeCSIM

//~ $graph-> StrokeCSIM( basename( __FILE__)); //using this "basename(_FILE_)" we can save as same as PHP file name or we can use this
//~ $graph-> StrokeCSIM( 'creatImg.php');

?>



Check the My other post also



 


2009 Isuru's Blog. All rights reserved.
.
For the more details..,
keijayathissa@gmail.com