<? mysql_connection("$server","$username","$password"); mysql_select_db(db_coba); function nopeserta(){ $sql=mysql_query("SELECT no_peserta FROM peserta ORDER BY no_peserta DESC LIMIT 1 ") or die (mysql_error()); $d=mysql_num_rows($sql); if($d>0){ $r=mysql_fetch_array($sql); $d=$r['no_peserta']; $str=substr($d,1,4); $No_Urut =(int)$str; }else{ $No_Urut = 0; } $No_Urut = $No_Urut + 1; $Nol=""; $nilai=4-strlen($No_Urut); for ($i=1;$i<=$nilai;$i++){ $Nol= $Nol."0"; } $Kode =$Nol.$No_Urut; return $Kode; } ?>
sebelumnya buat database dengan nama “db_coba”. dan buat tabel ”peserta” serta fieldsnya “no_peserta”;
Selesai….
Download sourcecode disini