Friday, March 16, 2012

PHP: Upload DBF File using Temporary Table


On this occasion I would like to share the writing of PHP programming script to import DBF file solutions. The method applied in this case is to import DBF file to a temporary table and then  stored into the next main or transactional table. The following more detailed explanation:

1. Prepare koneksi.php which serves as a database connection module and put into the LIB folder
2. Prepare the dbf_class PHP class that can be downloaded at and put into the LIB folder
3. Here is the script for the DBF file upload browse file method

<form name="form1" action="save.php" method="post" enctype="multipart/form-data" >
<?

class timerClass
    {
    var $startTime;
    var $started;
    function timerClass($start=true)
        {
        $this->started = false;
        if ($start) $this->start();
        }
  
    function start()
        {
        $startMtime = explode(' ',microtime());
        $this->startTime = (double)($startMtime[0])+(double)($startMtime[1]);
        $this->started = true;
        }
  
    function end($iterations=1)
        {
        $endMtime = explode(' ',microtime());
        if ($this->started)
            {
            $endTime = (double)($endMtime[0])+(double)($endMtime[1]);
            $dur = $endTime - $this->startTime;
            $avg = 1000*$dur/$iterations;
            $avg = round(1000*$avg)/1000;
            return "$avg milliseconds";
            }
        else
            {
            return "timer not started";
            }
        }
    }

if (isset($_POST['X']))
if ($_FILES['filenya']['size']>150000)
    {
    echo "<script>alert('File to big [max150Kb]')</script>";
    }
else
    {
    $thefile = '';
    $name = $_FILES['filenya']['name'];
  
    if (move_uploaded_file($_FILES['filenya']['tmp_name'],"dbf/$name")) $thefile = 'dbf/'.$name;
    include('./lib/koneksi.php');
    include('./lib/dbf_class.php');
    $timer = new timerClass();
    $timer ->start();
    $dbf = new dbf_class($dir.$thefile);
    $num_rec=$dbf->dbf_num_rec;
    $field_num=$dbf->dbf_num_field;
    $endexct = $timer->end();

    for($i=0; $i<$num_rec; $i++)
        {
        if ($row = $dbf->getRow($i))
            {
            $sql_sintax="";
            for($j=0; $j<$field_num; $j++)
                {
               
                switch ($j) {
                    case 0 : $kd_unit_tmp = substr($row[0],0,6);break;
                    case 1 : $tgl_order_tmp = $row[1];break;
                }
                   
               
                if ($dbf->dbf_names[$j]['type']=='N')
                    {
                    $sql_sintax = $sql_sintax . $row[$j] . ",";
                    }
                else if ($dbf->dbf_names[$j]['type']=='C')
                    {
                    $sql_sintax = $sql_sintax . "'" . $row[$j] . "',";
                    }
                else if ($dbf->dbf_names[$j]['type']=='D')
                    {
                    $sql_sintax = $sql_sintax . "date('" . $row[$j] . "'),";
                    }
                }
            $pjg_query= strlen($sql_sintax);
            $sql_sintax = "INSERT INTO temp_order VALUES (" . substr($sql_sintax, 0, $pjg_query-1) . ")";
           
            $sql_out=mysql_query($sql_sintax) or die ("<script>alert('data doble!')</script>");
            }
        }
    unlink($thefile);
  
  
    // output from  MySQL table
    $header=1;
    $kecuali="";
    $no=1;
    $sql_sintax = "select *    from temp_order where left(kd_order,6)='".$kd_unit_tmp."' and tgl_order=date('".$tgl_order_tmp."')";
    $sql_out=mysql_query($sql_sintax) or die ($sql_sintax);
    while ($row=mysql_fetch_array($sql_out))
        {
        $kd_order=$row["kd_order"];
        }
       
        if ($header)
            {
                echo "For HEADER display Table";

            }
                echo "detail row";
           
?>

  <div align="right">
    <input type="hidden" name="kd_unit_tmp" value="<?=$kd_unit_tmp?>">
    <input type="hidden" name="kecuali" value="<?=$kecuali?>">
    <input type="reset"  name="Cancel" value="Batal">
    <input type="submit" name="Submit" value="Konfirmasi">
  </div>
</form>

 4. Here is the script for save into main or transactional table and clear the temporary table
<?
include('./lib/koneksi.php');

$kd_unit=$_POST['kd_unit_tmp'];
$kecuali = $_POST['kecuali'];
echo "submit : " . $_POST['Submit'];
echo "kd_unit : " . $kd_unit;

if (isset($_POST['Submit']))
                {
                $perintah="insert into .......( main table )";
                $hasil=mysql_query($perintah) or die ($perintah);
                }
                
                // delete temporary
                $perintah="delete from temp_order where left(kd_order,6)='$kd_unit'";
                $hasil=mysql_query($perintah);

?>

<html>
<head>
<title>Save</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body onLoad="document.location='index.php'">

</body>
</html>

hope this article useful for you to see you at the other posts

1 comment:

Unknown said...

A very lengthy article.In which you explained DBF file using temporary table.But it is totally confusing.In your article its look like easy but when we do it then some loops are there.
electronic signatures