Posts Tagged ‘Javascript’

Unlimited select chain AJAX

This item was filled under [ Code Snippet, Javascript, PHP ]

Berikut adalah script AJAX standar dan simpel untuk mengaplikasikan unlimited select chain, yg artinya bermaksud untuk bisa memilih suatu Form dengan bertingkat, yang biasanya sering kita jumpakan pada form pengisian lokasi.

Berikut dibawah ini adalah contoh tampilan dan juga alamat demo dan script.

Alamat Demo Llive: http://www.nusansifor.com/kasus/unlimit_chain_select.php
Script Source: http://www.nusansifor.com/kasus/unlimit_chain_select.phps

Tentunya Anda sesuaikan real data dengan data yang ada pada kebutuhan Anda.
Semoga membantu :mrgreen:

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading ... Loading ...
Popularity: 4,997 views
Tagged with: [ , , , , ]

Date format ‘xx time ago’

This item was filled under [ Code Snippet, Javascript, PHP ]

Kita sering melihat pada website menggunakan format “XX times ago”, alternatif dari penampilan tanggal.
Berikut adalah fungsi dan cara penggunaan-nya:

<?php
// FUNGSI
function ago($timestamp){
	$difference = time() - $timestamp;
	$periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
	$lengths = array("60","60","24","7","4.35","12","10");
	for($j = 0; $difference >= $lengths[$j]; $j++) {
		if(isset($lengths[$j]) && $lengths[$j] > 0) {
			$difference /= $lengths[$j];
		}
	}
	$difference = round($difference);
	if($difference != 1) $periods[$j].= "s";
	$text = "$difference $periods[$j] ago";
	return $text;
}
 
// PENGGUNAAN
$tanggal = strtotime("2009-06-01 20:22:00");
echo ago($tanggal); // => sekarang adalah 18 juli 2009, maka hasilnya: "2 months ago"
?>

sumber: http://drupal.org/node/61565

Catatan: Untuk implementasi, lebih baik menggunakan sisi client, yaitu menggunakan Javascript dari jQuery Plugin berikut ini: link nya http://timeago.yarp.com/

Kutipan dari web tersebut:
Isikan script pada head HTML:

<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.timeago.js" type="text/javascript"></script>
 
<script type="text/javascript">
	jQuery(document).ready(function() {
	  jQuery("abbr.timeago").timeago();
	});
</script>

Isi kan pada body HTML seperti ini, contoh:

<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>

Download Plugin Times Ago disini
Download jQuery disini

Semoga bermanfaat :mrgreen:

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading ... Loading ...
Popularity: 3,283 views
Tagged with: [ , , , , , , ]

Mendeteksi browser visitor jika tidak ada flash, jalankan gif

This item was filled under [ Code Snippet, Javascript ]

Mendeteksi browser visitor jika tidak ada flash, jalankan gif menggunakan javascript.

How to detect browser no flash and we give alternate with animation file gif.

Pada contoh code, kita asumsikan file “contoh.swf” dan “contoh.gif” sebagai pengganti jika browser visitor tidak ada flash player nya.

<!-- begin the OBJECT tag, which will be understood by ActiveX-capable browsers -->
<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" CODEBASE="http://active.macromedia.com/flash/cabs/swflash.cab#version=3,0,0,11" width="165" height="164" name="sw" ID="sw">
<param name="movie" value="contoh.swf" />
<param name="quality" value="high">
<param name="Loop" value="true">
<param name="play" value="true">
<param name="wmode" value="opaque">
<param name="allowScriptAccess" value="sameDomain" />
<param name="bgcolor" value="#000000" />

<!-- begin the JavaScript -->
<script type="text/javascript">

//If this browser understands the mimeTypes property and recognizes the MIME Type //"application/x-shockwave-flash"...
if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]){
  //...write out the following <EMBED> tag into the document.
  document.write('<embed src="contoh.swf" quality="high" bgcolor="#000000" width="165" height="164" name="contoh" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" LOOP="true" PLAY="true" wmode="opaque" />');
} else { //Otherwise,...
   //...write out the following <img> tag into the document. The image need
   //not be the same size as the Flash Player movie, but it may help you lay out the
   //page if you can predict the size of the object reliably.
   document.write('<img src="contoh.gif" width="220" height="110" title="Contoh">');
}
</script>
<!-- Close the OBJECT tag. -->
</OBJECT>
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Popularity: 16 views
Tagged with: [ , , ]

CSV2MySQL convert from CSV file into MySQL database

This item was filled under [ Code Snippet, Javascript, MySQL, PHP ]

Bagaimana cara convert dari CSV ke dalam database MySQL menggunakan PHP.
Silahkan lihat langkah2 dibawah ini:

How to convert from CSV file into MySQL database using PHP?
Plus how to create a table and fields into database with PHP?
Here it is..
We need 3 files php, and 2 file jpg (the jpg is just for menu).

index.php

<?php
// UPLOAD CSV CUSTOM
// Ver. 1.0
// by Jawaad
// http://www.nusansifor.com
// File: index.php
 
@session_start();
//@ob_start(); 
//@ob_implicit_flush(0); 
 
@error_reporting(E_ALL ^ E_NOTICE);
@ini_set('display_errors', true);
@ini_set('html_errors', false);
@ini_set('error_reporting', E_ALL ^ E_NOTICE);
 
define('jin', true);
 
// CONECT DATABASE
@mysql_connect("localhost", "root", "") OR die ("NOT CONNECT DATABASE");
@mysql_select_db("nama_database") OR die ("CONNECTED, BUT NO DATABASE");
 
 
 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en:us'>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>UPLOAD CSV CUSTOM</title>
    <style type="text/css" media="screen">
      body {
        background: #fff;
        color: #888;
        font: 100% georgia,times,serif;
      }
      h1 {
        font-weight: normal;
        margin: 0;
        padding: 0 0 .5em 0;
      }
      h2 {
        font-weight: normal;
        margin: 0;
        padding: 0 0 .5em 0;
      }
 
/*- Menu Tabs J--------------------------- */
 
    #tabsJ {
      float:left;
      width:100%;
      background:#fff;
      font-size:93%;
      line-height:normal;
	  border-bottom:1px solid #24618E;
      }
    #tabsJ ul {
	margin:0;
	padding:10px 10px 0 50px;
	list-style:none;
      }
    #tabsJ li {
      display:inline;
      margin:0;
      padding:0;
      }
    #tabsJ a {
      float:left;
      background:url("tableftJ.gif") no-repeat left top;
      margin:0;
      padding:0 0 0 5px;
      text-decoration:none;
      }
    #tabsJ a span {
      float:left;
      display:block;
      background:url("tabrightJ.gif") no-repeat right top;
      padding:5px 15px 4px 6px;
      color:#24618E;
      }
    /* Commented Backslash Hack hides rule from IE5-Mac \*/
    #tabsJ a span {float:none;}
    /* End IE5-Mac hack */
    #tabsJ a:hover span {
      color:#FFF;
      }
    #tabsJ a:hover {
      background-position:0% -42px;
      }
    #tabsJ a:hover span {
      background-position:100% -42px;
      }		
    </style>
    <script type="text/javascript">
function addElement() {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById("theValue").value -1)+ 2;
  numi.value = num;
  var divIdName = "my"+num+"Div";
  var newdiv = document.createElement('div');
  newdiv.setAttribute("id",divIdName); //  + num + 
  newdiv.innerHTML = "<input type=hidden name=\"kode[" + num + "]\" value=\"" + num + "\"><input type=text name=\"field" + num + "\"> <select name=\"tipe" + num + "\"><option>TEXT</option><option>LONG-TEXT</option><option>NUMERIC</option><option>NUMERIC-2-DECIMAL</option><option>DATE</option><option>DATE-TIME</option></select> <a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\">[- del]</a><p>";
  ni.appendChild(newdiv);
}
 
function removeElement(divNum) {
  var d = document.getElementById('myDiv');
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
}
</script>
</head>
 
<body>
      <h1>UPLOAD CUSTOM CSV INSERT TO MYSQL</h1>
<div id="tabsJ">
  <ul>
    <li><a href="?do=upload_csv" title="Upload CSV"><span>Upload CSV</span></a></li>
    <li><a href="?do=create_table" title="Create New Table"><span>Create New Table</span></a></li>
  </ul>
</div>
<?php
switch($_REQUEST["do"]) {
	case "upload_csv":
		include ("upload_csv.php");
	break;
	case "create_table":
		include ("create_table.php");
	break;
 
	default:
		include ("upload_csv.php");
}
 
?>
 
 
	 </body>
</html>

upload_csv.php

<?php
// UPLOAD CSV CUSTOM
// Ver. 1.0
// by Jawaad
// http://www.nusansifor.com
 
if(!defined('jin')) { die('nice try..'); }
 
if($_POST[upload_csv]=="1") {
	$table_name="`".$_POST[table_name]."`";
	if(!$_FILES["file_csv"]["name"]) {
		echo "<script>alert('Please Browse File.. CSV.');</script>";
	} else if(substr($_FILES["file_csv"]["name"], -3) != "csv") {
		echo "<script>alert('File ".substr($_FILES["file_csv"]["name"],-3)." not support. Please use CSV extension');</script>";
	} else if($_FILES["file_csv"]["size"]>=1672864) {
		echo "<script>alert('File size is too big. File max 1.5 MB');</script>";
	} else {
		// READ FIELD FROM TABEL
		$sql_tabel = mysql_query("SELECT * FROM ".$table_name."");
		$numfields = mysql_num_fields($sql_tabel);
		$field_name = "";
		for ($i=0; $i < $numfields; $i++) {
			$get_field = mysql_field_name($sql_tabel, $i);
			if($i == 1) {
				$field_name .= "`".$get_field."`";
			} elseif($i > 1) {
				$field_name .= ", `".$get_field."`";
			}
		}
		copy($_FILES["file_csv"]["tmp_name"], "tmp/".$_FILES["file_csv"]["name"]);
	  	$ifile = fopen("tmp/".$_FILES["file_csv"]["name"],"r");
	  	$no=1;
		$jum_value = $numfields - 1;
	  	while (($file_list = fgetcsv($ifile, 1000, ",")) !== FALSE) {
			if($no > 1) {
				$field_value = "";
				for ($i=0; $i < $jum_value; $i++) {
						if($i == 0) {
							$field_value .= "'".$file_list[$i]."'";
						} else {
							$field_value .= ", '".$file_list[$i]."'";
						}
				}
					$sql = "INSERT INTO ".$table_name." (".$field_name.") VALUES (".$field_value.")";
					$ex = mysql_query($sql) OR die ("<br>ERROR, DATA TIDAK BISA MASUK...<br>");
					if(!$ex) {
						echo "<script>alert(\"Sorry, Error....\");location.href='?do=upload_csv';</script>";
					} else {
						echo "<script>alert(\"Succesed.\");location.href='?do=upload_csv';</script>";
					}
			}
			$no++;
		}
		fclose($ifile);
		unlink("tmp/".$_FILES["file_csv"]["name"]) OR die ("Cannot DEL file tmp/".$_FILES["file_csv"]["name"]);
	}
}
?>
 
<form method="post" enctype="multipart/form-data">
<br><br>
<h2>Upload CSV</h2>
<table border="0" cellspacing="0" cellpadding="3">
<tr>
	<td valign="top">Table Name</td><td valign="top">: <input name="table_name"></td>
</tr>
<tr>
	<td valign="top">File CSV</td><td valign="top">: <input type="file" name="file_csv" value="1"></td>
</tr>
<tr>
	<td valign="top" colspan="2">
		<input type="hidden" name="upload_csv" value="1">
		<input type="submit" name="submit" value="UPLOAD .CSV">
	</td>
</tr>
</table>
</form>

create_table.php

<?php
// UPLOAD CSV CUSTOM
// Ver. 1.0
// by Jawaad
// http://www.nusansifor.com
// file: create table
 
if(!defined('jin')) { die('nice try..'); }
 
?>
 
<form method="post">
<br><br>
<h2>Create Table</h2>
Table Name: <input name="table_name">
<input type="hidden" value="0" id="theValue" /><a href="javascript:;" onclick="addElement();">+ Add field</a>
<p>
<div id="myDiv"> </div>
 
 
<input type="hidden" name="create_table" value="1">
<input type="submit" name="submit" value="Create Table">
</form>
 
 
<?php
if($_POST[create_table]=="1") {
	//$kode = array($_POST[kode]);
	$table_name = $_POST[table_name];
	echo "Check <b>".$table_name."</b><br>";
	//print_r($_POST[kode]);
	if($table_name != "" && $_POST[kode] != "") {
		foreach ($_POST[kode] as $field_arr) {
			$field =  $_POST["field".$field_arr.""];
			$tipe = $_POST["tipe".$field_arr.""];
				 if($tipe == "TEXT") {$db_tipe = "varchar(255),";}
			elseif($tipe == "NUMERIC") {$db_tipe = "bigint(20),";}
			elseif($tipe == "NUMERIC-2-DECIMAL") {$db_tipe = "float(10,2),";}
			elseif($tipe == "LONG-TEXT") {$db_tipe = "text NOT NULL,";}
			elseif($tipe == "DATE") {$db_tipe = "date NOT NULL,";}
			elseif($tipe == "DATE-TIME") {$db_tipe = "datetime NOT NULL,";}
			$the_fields .= "`" . $field . "` " . $db_tipe . "\n";
		}
		$sql = "
					CREATE TABLE IF NOT EXISTS `".$table_name."` (
						`id` bigint(20) NOT NULL auto_increment,
						".$the_fields."
						PRIMARY KEY  (`id`)
					) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
		";
		$ex = mysql_query($sql);
		if(!$ex) {
			echo "Sorry,... Error...<br>";
		} else {
			echo "Table  <b>".$table_name."</b> created.<br>";
		}
	} else {
		echo "Sorry,... Error... Please fill the table name and the fields...<br>";
	}
}
?>

Image files:

Download file lengkap:

  CSV2MySQL.V.1.0.rar (6.4 KiB, 1,857 hits)

semoga membantu :mrgreen:

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading ... Loading ...
Popularity: 6,211 views
Tagged with: [ , , , , ]

Halaman ini di eksekusi dalam waktu 2.209 detik! (mayan lah segini mah...)