Wednesday, 2 November 2016

Form with rounded Border

Hi All,

See this code to put rounded border to forms.

---------------------------------------------------------------------
<html>
<head>
<style>
fieldset {
  -moz-border-radius:5px;
  border-radius: 5px;
  -webkit-border-radius: 5px; //edit :D
 
}
</style>
</head>
<body>
<fieldset  style="width:250px">
<form action=HUPModify.php method=post>
<pre>
New Reference Number <input name=nrefno>
Total References     <input name=tref>
<input type=submit value="Change file">
</pre>
</form>
</fieldset>
</body>
</html>

---------------------------------------------------------------------

Sunday, 30 October 2016

Hi All,

If you want to find the difference between two dates (Eg. current date and another date) use this simple code

<?php

$current = time(); // Current date and time
$testdate= strtotime("2016-10-20");
$daydiff = $current - $testdate;
echo floor($daydiff / (60 * 60 * 24));

?>

Using this code you can find the fine by calculating day difference between current date and book issue date.

Saturday, 11 June 2016

Execute a Java program from PHP

Dear friends,

In data mining works we usually used to call java class files. Sometimes from PHP.

Try this code to invoke a Java program or some other jar executables from your PHP

Sample 1.

<?php

exec("java Apriori hup.dat 0.4 > hupa.txt");

?>

The above code will run Apriory.class with hup.dat as input file and the output will be saved in hupa.txt which can be further processed by PHP data mining program.

Sample 2.

<?php

exec("java -jar LDA/jar/jLDADMM.jar -model LDA -corpus LDA/test/corpus.txt -name testLDA -ntopics 5 -twords 5 > huplda.txt");

?>

The above code will run a jar file for LDA topic modeling algorithm stored in LDA/jar/ folder in www directory of LAMP or WAMP or XAMPP with lot of switches and output can be redirected to huplda.txt file.

Show Time difference in milliseconds using PHP

Dear Friends,
We might have come across showing results by google with search time. The same can be implemented in our program as well.

<?php

$currentTime = microtime(true);


// Write some code which consume your CPU time
//...
// Find time difference in milliseconds

$timediff= round(microtime(true) - $currentTime,3)*1000;
echo "<h4>in ". $timediff. " Milli seconds </h4><br>";

?>

Friday, 27 May 2016

Make a copy of a template file and Change a word by another word in the whole file using PHP

Dear Friends
While dealing with template file, it is too common to make copies of that file and replace one word by another word.

In the code attached below one file is created dynamically based on table name and then a local copy of template file is created in the name of table in a folder HUP. Then a word HHHUUUPPP will be replaced with the name of table. Once the entire file is replaced then a fresh copy the new file will be taken and old file will be deleted.

//Code starts here.

<?php

$table=$_POST['tablename'];
$viewfile1='HUP/view'.$table.'1.php';
$viewfile='HUP/view'.$table.'.php';
copy('HUPViewtable.php',$viewfile1);

$myfilemain=fopen($viewfile, "w") or die("Unable to open file!"); //Open table structure file for
$myfiletemp = fopen($viewfile1, "r") or die("Unable to open file!"); //Open table structure file for
while(!feof($myfiletemp)) // Reading the table structure file
{
  $line= fgets($myfiletemp) ;
  $rtext=str_replace("HHHUUUPPP",$table,$line);
  fwrite($myfilemain,$rtext);
}
fclose($myfilemain);
fclose($myfiletemp);
unlink($viewfile1);
?>

Delete all files from a folder using PHP

Dear friends,
In some cases we will be forced to clear the contents of some server folders. Use the code given below.

//Code starts here

<?php

$files = glob('HUP/*'); // get all file names
foreach($files as $file)
{ // iterate through the files in the folder
  if(is_file($file))
    unlink($file); // delete file
}
?>

Create Zip file out of your Folder using PHP

Dear Friends,
Use this code to generate and download zip files. NB: no echo statements should be there in your code. If so should comment it.

//Code starts below


<?php

$file_names=array();

$dir="HUP/";
if (is_dir($dir)) {
        if ($handle = opendir($dir)) {
            $file_names = array();
            while (false !== ($file = readdir($handle))) {
                if (is_file($dir.$file) && $file != basename($_SERVER['PHP_SELF'])) $file_names[] = $file;
            }
            closedir($handle);
            if (is_array($file_names)) sort($file_names);
          
        }
    }

$error = "";      //error holder
$file_path="HUP/";
//echo "entered 1";
if(extension_loaded('zip'))
{ // Checking ZIP extension is available
//echo "entered 2";
    if(count($file_names) > 0)
    {  
    //echo "entered 3";
        // Checking files are selected
        $zip = new ZipArchive();            // Load zip library
        $zip_name = time().".zip";          // Zip name
        if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){       // Opening zip file to load files
            $error .=  "* Sorry ZIP creation failed at this time<br/>";
            //echo $error;
        }
        foreach($file_names as $file){ 

            $zip->addFile($file_path.$file);            // Adding files into zip
        }
        $zip->close(); 
        //echo $zip_name;
        if(file_exists($zip_name))
       {
        //echo "entered 4";
            // push to download the zip
            header('Content-type: application/zip');
            header('Content-Disposition: attachment; filename="'.$zip_name.'"');
            readfile($zip_name);

            // remove zip file is exists in temp path
            unlink($zip_name);
       }

    }
    else
    {
        $error .= "* Please select file to zip <br/>";
    }
}
else
{
    $error .= "* You dont have ZIP extension<br/>";
}
?>