Thursday, February 19, 2009

dbfile

ini dulu gw pakai untuk ujian akhir kuliah perancangan dan pemrograman web
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>";
}

?>

Wednesday, February 18, 2009

htaccess mod rewrite yg bisa jalan

buat code igniter nyoba2 akhirnya bisa
dari pada ilang terus repot nyari lagi
mending gw taro disini


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ ci_plain/index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders,
and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ ci_plain/index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

--
Abdul Arfan
http://arfan86.blogspot.com


tambahan..
tarun file ini di direktori ci_plain

Monday, February 02, 2009

mengganti nama file di linux

ubah semua nama file html menjadi php

rename 's/\.html/\.php/' *.html



mantapnya ini adalah regex sehingga tranformasi file yg bisa dilakukan
lebih rumit dari sekedar mengganti extensi file