Home > php > easy tabular file access with sql like command

easy tabular file access with sql like command

<?
    /////////////////////////////////////////////////////
    //    Abdul Arfan 2007 Dec   
    //    db file
    //    akses file jadi mirip database
    ////////////////////////////////////////////////////
    
    //insert(array("arfan", "1204556658", "Abdul Arfan"));
    delete(array(0=>"arfan"));
    display_all();
    
    ///////////////////////////
    // untuk menginsert data //
    ///////////////////////////
    function insert( $array )
    {
        $handle = fopen ( "./db.txt", "a" );
        
        $first =true;
        write_array($handle, $array);
    }
    
    
    
    
    ///////////////////////////
    // untuk searching data  //
    ///////////////////////////
    function select( $array_constrain )
    {
        $result = null;
        $counter=0;
        
        $handle = fopen ( "./db.txt", "r" );
        
        while($line = fgets($handle))
        {
            $array = split(",", $line);
            $masuk = true;
            
            if($array_constrain)
            {
                foreach ($array_constrain as $key => $value)
                {
                    if($array[$key]!=$value)
                    {
                        $masuk = false;
                    }
                }
            }
                
            if($masuk)
            {
                $result[$counter]=$array;
                $counter++;
            }
        }
        return $result;
    }
    
    function delete( $array_constrain )
    {
        $result = null;
        $counter=0;
        
        $handle = fopen ( "./db.txt", "r" );
        $handle2 = fopen ( "./db-temporary.txt", "w" );
        
        while($line = fgets($handle))
        {
            $array = split(",", $line);
            $cocok = true;
            
            if($array_constrain)
            {
                foreach ($array_constrain as $key => $value)
                {
                    if($array[$key]!=$value)
                    {
                        $cocok = false;
                    }
                }
            }
                
            if(!$cocok)
            {
                write_array($handle2, $array);
            }
        }
        
        fclose ($handle );
        fclose ($handle2 );
        
        //hapus db.txt
        $myFile = "db.txt";
        unlink($myFile);
        
        rename  ( "db-temporary.txt", "db.txt" );
        return $result;
    }
    
    function update( $array_constrain , $array_update)
    {
        $result = null;
        $counter=0;
        
        $handle = fopen ( "./db.txt", "r" );
        $handle2 = fopen ( "./db-temporary.txt", "w" );
        
        while($line = fgets($handle))
        {
            $array = split(",", $line);
            $cocok = true;
            
            if($array_constrain)
            {
                foreach ($array_constrain as $key => $value)
                {
                    if($array[$key]!=$value)
                    {
                        $cocok = false;
                    }
                }
            }
            
            if($cocok)
            {
                foreach ($array_update as $key => $value)
                {
                    $array[$key] = $array_update[$key];
                }
            } 
            write_array($handle2, $array);
        }
        
        fclose ($handle );
        fclose ($handle2 );
        
        //hapus db.txt
        $myFile = "db.txt";
        unlink($myFile);
        
        rename  ( "db-temporary.txt", "db.txt" );
        return $result;
    }
    
    /**
      fungsi-fungsi tambahan
    */
    function write_array($handle, $array)
    {
        $first =true;
        foreach($array as $el)
        {
            if($first)
            {
                $total = trim($el);
                $first=false;
            }
            else
            {
                $total = $total.", ".trim($el) ;
            }
        }
        fwrite($handle, $total."\n");
    }
    
    /**
    * return dari ini adalah satu baris saja
    * jadi bukan bentuk tabel
    */
    
    function select_one( $array_constrain )
    {
        $handle = fopen ( "./db.txt", "r" );
        while($line = fgets($handle))
        {
            $array = split(",", $line);
            foreach ($array_constrain as $key => $value)
            {
                if($array[$key]==$value)
                    return $array;
            }
        }
        
        return null;
        
        fclose ($handle );
    }
    
    function display_all()
    {
        $result = select(null);
      
        echo "<table border=\"1\">";
        foreach($result as $row)
        {
            echo "<tr>";
            foreach($row as $element)
            {
                echo "<td>$element</td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    }
    
?>
Advertisements
Categories: php
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: