di sana diperlukan pengaksesan file tapi karena gw udah terbiasa dengan
pengaksesan database jadi gw bikin aja supaya mirip
<?
/////////////////////////////////////////////////////
// Abdul Arfan 2007 Dec
// db file
// akses file jadi mirip database
////////////////////////////////////////////////////
//insert(array("arfan", "1204556658", "Abdul Arfano"));
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>";
}
?>