Belajar Online 31 Cara murah dan mudah dalam belajar

17 Desember 2010

Belajar Online Membuat Galeri Foto dengan Data Text

Abis dapet job suruh buat galeri foto dimana data tidak berupa database tapi berdasar data bertipe Text. Dimana data tersebut kita baca dulu dengan menggunkan script php fread kemudian digabung menggunakan implode supaya data array tersebut berubah menjadi string. Kita pecah data tersebut dengan explode guna memecah nama file dan deskripsinya berdasarkan karakter tertentu. contoh scriptnya ->

----------------------------------------
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<style type="text/css">
body {background-color: #f6f6f6; font-family: Arial, Helvetica, sans-serif; font-size: 12px}
#field_gbr {width: 650px;height:500px;border:1px solid #CCCCCC;background:#f5f5f5;padding:10px;}
#boks{height:190px;padding:5px;border:1px solid #dddddd;background:#ffffff;padding-left:20px;padding-top:10px;padding-right:20px;float:left;margin:10px;
#judul{font-family: Arial, Helvetica, sans-serif; font-size: 15px;}
#gbr{margin-bottom:15px;}
#field {width: 500px;border:1px solid #CCCCCC;background:white;padding:10px;float:left;}


</style>

</HEAD>

<BODY>
<div>
<?php
$file = "image.txt";
$contents = file($file);
$string = implode($contents);
$array_1="|";
$array_2="#";
$data = explode($array_2, $string);
echo"<div id='field_gbr'>";

$bagi_1=6;
$jumlah_data=count($data)-1;
if($bagi_1 > $jumlah_data){
$bagi=$jumlah_data;
} else {
$bagi=$bagi_1;
}

if($jumlah_data=="0"){
echo"";
$hal=0;
} else {
$hal=ceil($jumlah_data/$bagi);
}

$awal=$_REQUEST['awal'];
if(!$awal){
$awal_1="0";
} else {
$awal_1=$awal;
}
$akhir=$_REQUEST['akhir'];
if(!$akhir){
$akhir_1=$bagi;
} else {
$akhir_1=$akhir;
}

for($i = $awal_1; $i< $akhir_1; $i++){
$gambar=explode($array_1,$data[$i]);
echo"<div id=boks>";
echo"<div id=gbr><img src='$gambar[0]' height=150 width=150></div>";
echo"<div id=judul><center>$gambar[1]</center></div>";
echo"</div>";
}
echo"</div>";
echo"<div><br>&nbsp;&nbsp;<a href='index.php?awal=0&&akhir=$bagi'>First</a>&nbsp;&nbsp;";

for ($h = 1;$h <= $hal;$h++){
$start=($h-1)*$bagi;
if($h==$hal){
$finish=$jumlah_data;
} else {

$finish=$start+$bagi;
}

echo"<a href='index.php?awal=$start&&akhir=$finish'>$h&nbsp;&nbsp;</a>";
}
echo"&nbsp;&nbsp;<a href=index.php?awal=$start&&akhir=$jumlah_data>Last</a></div>";

?>


<form method="post" action="<?php echo"$PHP_SELF"; ?>" enctype="multipart/form-data">
<div id="field">
<table border=0>
<tr>
<td colspan=2>Picture Upload </td>
</tr>
<tr>
<td>Caption</td><td><input type="text" size="40" name="caption"></td>
</tr>
<tr>
<td>Picture</td><td><input type="file" size="40" name="picture"></td>
</tr>
<tr>
<td colspan=2 align=right><input type="submit" name="submit" value="Upload Picture"></td>
</tr>
</table>
</div>
</form>

<?php
if ($_POST['submit']) {
$caption=$_POST['caption'];
$file_name = $_FILES['picture']['name'];

$file_type = $_FILES['picture']['type'];
$source = $_FILES['picture']['tmp_name'];
$direktori = "$file_name";

if( file_exists ($direktori)) {
echo "<SCRIPT>alert(\"File sudah ada\");history.go(-1);</SCRIPT>";
return false;
exit();
} else {

move_uploaded_file($source,$direktori);

$myFile = "image.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = "$file_name|$caption#\r\n";
fwrite($fh, $stringData);
fclose($fh);
$upload=1;
}
if($upload=="1"){
?>
<script language="JavaScript">alert('Upload Data succes');document.location='index.php'</script>
<?php
}

}
?>

</div>
</BODY>
</HTML>

Tidak ada komentar: