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";

?>

 


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