1.链式函数
function createModel(n){
$ = {
name : n,
get : function(){
return $.name;
},
set : function(val){
$.name = val;
console.log($.name);
return $;
}
}
return $;
}
$=createModel();
$.set('aaa').set('bbb').set('ccc');
2. 回形打印,先打印出坐标,然后取自己需要的条件为星号,不符合条件的为空格
for ($y=1; $y <=10 ; $y++) {
$str = '';
for ($x=1; $x <=10 ; $x++) {
$s = ' ';
if($x==1 || $x==10 || $y==1 || $y==10)
$s = "*";
if($x>=3 && $x<=8 && ($y==3 || $y==8))
$s = "*";
if($y>=3 && $y<=8 && ($x==3 || $x==8))
$s = "*";
$str = $str.$s;
}
echo $str.'<br>';
}
3.千分位取值,先把数字转成字符串,取值从后往前取,输出值,从前往后输出;取符合条件的位置加逗号
function format(num){
var num = num+'';
var str = '';
var len = num.length;
for(var i=len-1; i>=0; i-- ){
str = num[i]+str;
if ((len-i)%3==0 && i!=0 ) {
str = ","+str;
}
}
console.log(str);
}
format(1234567890);
4.抽奖功能代码展示Top
<?php
// 生成随机1000条兑换码
$redis = new Redis();
$redis->connect("localhost",6379);
$db = new mysqli("localhost","root","123456","jiang");
function createCode(){
return md5(uniqid(microtime(true),true));
}
$one = 10; // 一等奖个数
$two = 30; // 二等奖个数
$three = 50; // 三等奖个数
$no = 1000; // 谢谢参与个数
$award = [];
$award = array_merge($award,getArray($one,"一等奖"));
$award = array_merge($award,getArray($two,"二等奖"));
$award = array_merge($award,getArray($three,"三等奖"));
$award = array_merge($award,getArray($no,"感谢参与"));
shuffle($award);// 打乱中奖顺序
for($i=0;$i<count($award);$i++){
$awardCode = createCode();
$redis->lpush("list",$awardCode);
$sql = "insert into award(award,code) value('".$award[$i]."','".$awardCode."')";
$db->query($sql);
}
function getArray($number,$value){
$arr = [];
for($i=0;$i<$number;$i++){
$arr[] = $value;
}
return $arr;
}
?>
<?php
date_default_timezone_set("PRC");
$y=date("Y",time());
$m=date("m",time());
$d=date("d",time());
$now = time();
$startTime = mktime(10, 0, 0, $m, $d ,$y);
//if(($startTime-$now)>0){
// exit("未到抽奖时间");
//}
// 连接redis
$redis = new Redis();
$redis->connect("localhost",6379);
// 生成cookie
function createCode(){
return md5(uniqid(microtime(true),true));
}
$cookie = createCode();
if(empty($_COOKIE['codeid'])){
setcookie("codeid",$cookie,time()+24*60*60*60);
}else{
exit("你已经抽过了!");
}
$code = $redis->lpop("list");
if($code){
exit("你抽取的抽奖码为:".$code);
}else{
exit("兑换码已抽完");
}
?>
<?php
$db = new mysqli("localhost","root","123456","jiang");
// 获取抽奖码
$awradCode = $_POST['code'];
// 查询数据库
$sql = "select * from award where code='".$awradCode."'";
$res = $db->query($sql);
$row = $res->fetch_assoc();
if($row == NULL){
exit("兑奖码无效");
}
if($row['status'] == 0){
$s = "update award set status=1 where code='".$awradCode."'";
$r = $db->query($s);// 更新兑奖状态
exit($row['award']);
}elseif($row['status'] == 1){
exit("该兑奖码已经使用");
}else{
exit("兑奖码无效");
}
?>