php 自动生成编号

html代码

<label>责任书编号</label>

<input type="text" name="responsecode" id="responsecode" value="<?php echo isset($responsecode)?$responsecode:''; ?>" readonly />

<span><em>*</em></span>

PHP代码

public function create(){

if($this->input->server('REQUEST_METHOD') == 'GET'){

$data=array();

$data = $this->_getGlobalData($data);

$data['responsecode'] = $this->dball->getNo("SG_".date("Ymd",time()),5);

$this->load->view('project/project/datadetailcreate.php',$this->_beforemethod($data));

}

else if($this->input->server('REQUEST_METHOD') == 'POST'){

            $datain=$_POST;
            if($datain['companyname']==''){
                $result["ret"] = 1;
                $result["success"] = false;
                $result["msg"] = "请输入企业名称!";
                $result["obj"] = '';
                echo json_encode($result);
                exit;
            }

          $sql = "select * from projects where responsecode = '".$datain['responsecode']."'";
            $query = $this->mydb->find($sql);
            if(count($query['obj']) > 0)
            {
                $result["ret"] = 1;
                $result["success"] = false;
                $result["msg"] = "责任书编号已存在,请确认!";
                $result["obj"] = '';

                echo json_encode($result);
                exit;
            }
            $sql = "select * from projects where projectname = '".$datain['projectname']."'";
            $query = $this->mydb->find($sql);
            if(count($query['obj']) > 0)
            {
                $result["ret"] = 1;
                $result["success"] = false;
                $result["msg"] = "项目名称已存在,请确认!";
                $result["obj"] = '';
                echo json_encode($result);
                exit;
            }

           $datainfo = $this->_getRequestModel();
            $datainfo = $this->_setAddModifyData($datainfo);
            $datainfo['status'] = '审核通过';
            $datainfo['mikey'] = $this->dball->getNo("P",7);
            $datainfo['saferid'] = $this->sessioninfo['userId'];
            $datainfo['companyname'] = $_POST['companyname'];
            $result=$this->mydb->addData($this->_beforeMethod($datainfo));
            $result=$this->_afterDBAct($result);

           $code=$this->dball->getNo('PTP'.date('Ymd',time()),6);
            $progressid = '申报开工条件审查新开工';
            $sql = "insert into projectprogresses(id,code,projectcode,projectid,progress,userid,date,createuser,createdatetime,modifyuser,modifydatetime) values('','".$code."','".$datainfo['code']."',".$id.",'".$progressid."','".$this->sessioninfo['userId']."','".date('Y-m-d',time())."','".$this->sessioninfo['fullName']."','".date('Y-m-d',time())."','".$this->sessioninfo['fullName']."','".date('Y-m-d',time())."')";
            $this->mydb->execSql($sql);
            $this->setLogs("新建项目",$datainfo);
            echo $this->_ReturnMsg($result);

}

       else redirect($this->url_module.'/'.$this->url_model);

}

 /**
     * 获取编号
     * @param string $type  编号的固定开始文字
     * @param integer $num  编号的最加流水号长度,默认2位
     * @return array $result
     */
    public function getNo($type,$num=2){
        $CI =& get_instance();
        $sql = "SELECT * FROM dictdatas WHERE type='编号生成' AND name = '".$type."'";  //dictdatas 数据字典
        $query = $this->db->query($sql);
        $result = $query->result_array();
        //print_r($CI);exit();
        if (sizeof($result) == 0){
            $result = $type.''.str_pad('1', $num,'0',STR_PAD_LEFT);
            $data['type'] = '编号生成';
            $data['seq'] = $type;
            $data['value'] = '1';
            $data['name'] = $type;
            $data["createdatetime"] = date('Y-m-d G:i:s');
            $data["createuser"] = $CI->session->userdata['userId'];
            $this->db->insert('dictdatas',$data);
        }
        else {
            $no = $result[0]['value'] + 1;
            $data['value'] = $no;
            $data["modifydatetime"] = date('Y-m-d G:i:s');
            $data["modifyuser"] = $CI->session->userdata['userId'];
            $this->db->where('id',$result[0]['id']);
            $this->db->update('dictdatas',$data);
            $result = $type.''.str_pad($no,$num,'0',STR_PAD_LEFT);
        }
        return $result;
    }

原文地址:https://www.cnblogs.com/lqw4/p/4646108.html