Forum Sementara Putera.com

Would you like to react to this message? Create an account in a few clicks or log in to continue.
Forum Sementara Putera.com

Bersama kita perkemaskan forum ini sementara forum asal dalam pemulihan.

Forum putera dah kembali. Masalah sudah berjaya diselesaikan. Sila lawati http://forum.putera.com/tanya


4 posters

    mcmane nak generate nombor fail?

    avatar
    fifi85
    Ahli Baharu
    Ahli Baharu


    Number of posts : 40
    Registration date : 03/09/2009

    mcmane nak generate nombor fail? Empty mcmane nak generate nombor fail?

    Post by fifi85 Fri Sep 11, 2009 5:28 pm

    salam..ada sesiapa tak leh tlg saya cmane nak generate no.fail utk di insert dalam database?
    contoh no.fail : ABC/2009/001
    ABC-->nama fail yang dah di set siap2
    2009-->tahun semasa
    001-->bilangan fail yang masuk

    saya nak buat camni,
    auto generate bil.fail yang masuk tu sehingga 3 digit shj(cth:999)
    auto generate juga tahun mengikut tahun semasa

    contohnya mcm ni:
    bilangan yang pertama masuk (ABC/2009/001)
    bilangan yang kedua (ABC/2009/002)dan seterusnya....

    bila masuk tahun berikutnya dia automatik jadi (ABC/2010/001) dan seterusnya...

    so ada sesiapa x yg ada coding php nya atau sql statement nya?
    dans kam
    dans kam
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 172
    Age : 43
    Location : Wangsa Maju
    Job/hobbies : PHP
    Registration date : 19/02/2009

    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by dans kam Mon Sep 14, 2009 1:46 pm

    Salam..
    try tgk sample kat bwh nih...

    PHPsimple.net
    avatar
    fifi85
    Ahli Baharu
    Ahli Baharu


    Number of posts : 40
    Registration date : 03/09/2009

    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by fifi85 Mon Sep 14, 2009 2:30 pm

    saya wat camni la kengkawan, tapi ada masalah sket. bil tu tak auto generate bila masuk dalam database. still 001 jgk. camne ek? function tu salah ke? if ade sape2 yg tau bleh btolkan.. Smile Smile

    <?php

    function formatZero($no, $n) {
    return str_pad((int) $no, $n, "0", STR_PAD_LEFT);
    }

    $desc ='ABC';

    $bil = formatZero(1,3);

    $tahun = date('Y');

    $sql = "INSERT INTO tbl_aduan(aduan_nofail)VALUES('$desc $tahun $bil')";

    ?>

    dekat database ABC 2009 001. bila insert lagi fail kedua still ABC 2009 001 jugak
    johnburn
    johnburn
    Moderators
    Moderators


    Gender : Male Number of posts : 755
    Location : Terengganu
    Registration date : 07/03/2009

    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by johnburn Mon Sep 14, 2009 3:27 pm

    fifi85 wrote:saya wat camni la kengkawan, tapi ada masalah sket. bil tu tak auto generate bila masuk dalam database. still 001 jgk. camne ek? function tu salah ke? if ade sape2 yg tau bleh btolkan.. Smile Smile

    <?php

    function formatZero($no, $n) {
    return str_pad((int) $no, $n, "0", STR_PAD_LEFT);
    }

    $desc ='ABC';

    $bil = formatZero(1,3);

    $tahun = date('Y');

    $sql = "INSERT INTO tbl_aduan(aduan_nofail)VALUES('$desc $tahun $bil')";

    ?>

    dekat database ABC 2009 001. bila insert lagi fail kedua still ABC 2009 001 jugak
    mmg la jd gtu
    ko try bt query cek id last yg msuk pastu + la satu kt yg last msk tu
    tp cam x praktikal je bt cmni
    dans kam
    dans kam
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 172
    Age : 43
    Location : Wangsa Maju
    Job/hobbies : PHP
    Registration date : 19/02/2009

    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by dans kam Mon Sep 14, 2009 4:56 pm

    Sy wat camni jadi..

    Ni DB;

    CREATE TABLE IF NOT EXISTS `format_number` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `code` varchar(5) NOT NULL,
    `number` int(11) NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
    )

    Ni code;

    <?php
    $hostname = 'localhost';
    $userdb = 'root';
    $passdb = '';
    $db = 'number';

    $_SESSION['conn'] = mysql_connect($hostname, $userdb, $passdb);

    mysql_select_db($db);

    $sql1 = "select * from format_number order by id desc limit 1";
    $q1 = mysql_query($sql1) or die(mysql_error());
    $r1 = mysql_fetch_array($q1);
    $num = $r1['number'] + 1;

    if(isset($_REQUEST['code'])){
    $sql2 = "insert into format_number(code,number) values('".stripslashes($_REQUEST['code'])."', '".$num."')";
    $q2 = mysql_query($sql2) or die(mysql_error());
    }
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Number</title></head>
    <body>
    <?
    $num = 1;

    // Output number in $num with 3 digits with "0" added.
    printf("%03d", $num);
    ?>
    <hr />
    <form action="" method="post" name="form1">
    Kod: <input name="code" type="text"><br />

    <input name="simpan" type="submit" value="Simpan">
    </form>
    <?
    $sql3 = "select * from format_number order by id asc";
    $q3 = mysql_query($sql3) or die(mysql_error());
    while($r3 = mysql_fetch_array($q3)){
    $num1 = $r3['number'];
    echo $r3['code'];
    printf("%04d",$num1);
    echo "<br />";
    }

    ?>
    </body>
    </html>

    Hasil;

    test 0001
    hahah0002
    dfdsf0003
    fghgg0004
    rrrrr0005
    johnburn
    johnburn
    Moderators
    Moderators


    Gender : Male Number of posts : 755
    Location : Terengganu
    Registration date : 07/03/2009

    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by johnburn Tue Sep 15, 2009 1:10 am

    try bt cmni. untuk sql:
    Code:

    CREATE TABLE IF NOT EXISTS `format_number` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `fail` varchar(3) NOT NULL,
      `tahun` varchar(4) NOT NULL,
      `bil` int(3) NOT NULL,
      PRIMARY KEY (`id`)
    )

    untuk php:
    Code:

    <?php
    require_once "db.php";

    if (isset($_POST['simpan'])) {
       $desc = $_POST['fail'];
       $tahun = date('Y');
       $query = "SELECT * FROM format_number WHERE fail='".$desc."' ORDER BY id DESC LIMIT 1";
       $result = mysql_query($query) or die (mysql_error());   

       $row = mysql_fetch_array($result);
       $bil = $row['bil'] + 1;   
       $query2 = "INSERT INTO format_number (fail, tahun, bil) VALUES ('$desc', '$tahun', $bil)";
       $result2 = mysql_query($query2) or die (mysql_error());
       $str = "Fail disimpan: $desc/$tahun/";
       printf("%s %03d",$str,$bil);
    }
    ?>
    <!doctype html public "-//w3c//dtd html 3.2//en">
    <html>
    <head>
       <title>Contoh Pernomboran Fail</title>
    </head>
    <body>
    <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" name="form1">
    Jenis Fail:
    <select name="fail">
       <option value="ABC">ABC</option>
       <option value="DEF">DEF</option>
       <option value="GHI">GHI</option>
    </select>
    <input name="simpan" type="submit" value="Simpan">
    </form>
    </body>
    </html>

    bahagian require_once "db.php"; tu, db.php merupakan file yg ade setting sambungan ke mysql. lbih kurg cam yg aku bg kat sini.

    code ni xde buat validation pe2, so pndai2 la improvekn code ni Very Happy
    aku just bt je code ni tnpa test sgt, so kl xjd or ade pe2, gtau
    kl xpham leh tnye
    avatar
    fifi85
    Ahli Baharu
    Ahli Baharu


    Number of posts : 40
    Registration date : 03/09/2009

    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by fifi85 Tue Sep 15, 2009 1:59 pm

    ok tq..mmg pandai la korang ni..tapi table tu kan, setiap kali insert bertambah row dia. boleh tak kalau bil tu bertukar kat situ je tanpa menambah row yg lain dlm db?
    johnburn
    johnburn
    Moderators
    Moderators


    Gender : Male Number of posts : 755
    Location : Terengganu
    Registration date : 07/03/2009

    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by johnburn Tue Sep 15, 2009 2:02 pm

    xpham bleh tlg jlaskn x
    avatar
    zolapaloza
    Ahli Baharu
    Ahli Baharu


    Number of posts : 2
    Registration date : 02/07/2009

    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by zolapaloza Tue Sep 15, 2009 2:15 pm

    maksudnye ko xnak tambah rekod masuk db ke?
    johnburn
    johnburn
    Moderators
    Moderators


    Gender : Male Number of posts : 755
    Location : Terengganu
    Registration date : 07/03/2009

    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by johnburn Tue Sep 15, 2009 2:30 pm

    kl xnk tmh, query yg insert tu tkar la jd update
    tp db mmg untk smpan rekod pn, nape xnk simpan?

    Sponsored content


    mcmane nak generate nombor fail? Empty Re: mcmane nak generate nombor fail?

    Post by Sponsored content


      Current date/time is Mon Nov 18, 2024 8:25 am