Tp5 写随机数(商品货号)

此随机数是添加时就产生了一组随机数,然后通过后台进行传入数据库

1.静态

<input type="hidden" name="product_number" value="{$random}" class="form-control" id="title">
2.控制器内

 public function add(){
$article = new YouproModel();
$result = $article->getProSelect();
$this->assign('result', $result);
//随机号 商品货号
$random = $article->randomStr();
$this->assign('random', $random);
// var_dump($random);
// exit;
if(request()->isAjax()){
$param = input('post.');
unset($param['file']);
$article = new YouproModel();
$flag = $article->insertCategory($param);
// var_dump($flag);exit;
return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
}
return $this->fetch();
}
3.模型内

public function randomStr(){
$arr = array_merge(range(0,9),range('A','Z'));
$str = '';
$arr_len = count($arr);
for($i = 0;$i < 8;$i++){
$rand = mt_rand(0,$arr_len-1);
$str.=$arr[$rand];
}
return $str;
}

随机数产生,点击保存之后,随机数也就传入了数据库,随之产生


原文地址:https://www.cnblogs.com/dennyxiao/p/8611180.html