1.鏈?zhǔn)胶瘮?shù)
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. 回形打印橘券,先打印出坐標(biāo),然后取自己需要的條件為星號(hào)卿吐,不符合條件的為空格
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.千分位取值旁舰,先把數(shù)字轉(zhuǎn)成字符串,取值從后往前取嗡官,輸出值箭窜,從前往后輸出;取符合條件的位置加逗號(hào)
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.抽獎(jiǎng)功能代碼展示Top
<?php
// 生成隨機(jī)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; // 一等獎(jiǎng)個(gè)數(shù)
$two = 30; // 二等獎(jiǎng)個(gè)數(shù)
$three = 50; // 三等獎(jiǎng)個(gè)數(shù)
$no = 1000; // 謝謝參與個(gè)數(shù)
$award = [];
$award = array_merge($award,getArray($one,"一等獎(jiǎng)"));
$award = array_merge($award,getArray($two,"二等獎(jiǎng)"));
$award = array_merge($award,getArray($three,"三等獎(jiǎng)"));
$award = array_merge($award,getArray($no,"感謝參與"));
shuffle($award);// 打亂中獎(jiǎng)順序
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("未到抽獎(jiǎng)時(shí)間");
//}
// 連接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("你已經(jīng)抽過了!");
}
$code = $redis->lpop("list");
if($code){
exit("你抽取的抽獎(jiǎng)碼為:".$code);
}else{
exit("兌換碼已抽完");
}
?>
<?php
$db = new mysqli("localhost","root","123456","jiang");
// 獲取抽獎(jiǎng)碼
$awradCode = $_POST['code'];
// 查詢數(shù)據(jù)庫
$sql = "select * from award where code='".$awradCode."'";
$res = $db->query($sql);
$row = $res->fetch_assoc();
if($row == NULL){
exit("兌獎(jiǎng)碼無效");
}
if($row['status'] == 0){
$s = "update award set status=1 where code='".$awradCode."'";
$r = $db->query($s);// 更新兌獎(jiǎng)狀態(tài)
exit($row['award']);
}elseif($row['status'] == 1){
exit("該兌獎(jiǎng)碼已經(jīng)使用");
}else{
exit("兌獎(jiǎng)碼無效");
}
?>