Skip to content

Instantly share code, notes, and snippets.

View forecho's full-sized avatar
🎯
Focusing

蔡正海 forecho

🎯
Focusing
View GitHub Profile
@forecho
forecho / Model.php
Last active December 22, 2025 11:10
Yii 2 表单验证JSON 二维数据
/**k
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'subtitle'], 'string', 'max' => 100],
['productInfo', 'vaildateProductInfo'], // 自定义验证
['skus', 'vaildateSkus'], // 自定义验证
];
@forecho
forecho / unlink.php
Last active August 29, 2015 14:26
删除匹配的所有文件
@forecho
forecho / log.php
Last active September 14, 2015 02:05
Yii2 在线查看日志功能 使用方法:放在web目录下。
<?php
/**
* author : forecho <caizhenghai@gmail.com>
* createTime : 2015/7/9 16:32
* description:
*/
if (!isset($_GET['auth']) || $_GET['auth'] != "forecho") {
die();
}
@forecho
forecho / uploadcontroller.php
Last active September 15, 2015 06:00 — forked from appcodr/uploadcontroller.php
REST API controller code in Yii2 to upload photo/file
<?php
class UploadController extends ActiveController
{
public $documentPath = 'documents/';
public function verbs()
{
$verbs = parent::verbs();
$verbs[ "upload" ] = ['POST' ];
@forecho
forecho / controller.php
Last active July 15, 2022 11:20
Yii2 使用 CoreSeek 搜索 Demo 更多信息参考官方文档 http://www.coreseek.cn/products-install/step_by_step/
public function actionSearch()
{
$keyword = Yii::$app->request->get('keyword');
if (empty($keyword)) $this->goHome();
require_once(Yii::getAlias('@common/helpers') . '/sphinxapi.php');
$cl = new \SphinxClient ();
$cl->SetServer('127.0.0.1', 9312);
//以下设置用于返回数组形式的结果
$cl->SetArrayResult(true);
@forecho
forecho / random.php
Created November 8, 2015 05:28
百万数据库某个表「伪」随机显示 N 条数据思路
/**
* 随机
* @param int $rows 数据池
* @return mixed
*/
protected function random($rows = 100)
{
$count = User::find()->select(['itemid'])->where([])->count();
$random = mt_rand($rows, $count - $rows);
$model = User::find()->where(['>=', 'id', $random])->joinWith(['type'])->limit($rows)->all();
@forecho
forecho / array_last.php
Created November 8, 2015 08:24
php获取数组最后一个数组值的三个方法
<?php
$array=array(1,2,3,4,5);
echo $array[count($array)-1];//计算数组长度,然后获取数组最后一个元素,如果数组中最后一个元素含有非数字键名,结果可能跟预期不符合
//适用于键名为数字的数组
echo '<br>';
echo end($array);//将数组的内部指针指向最后一个单元,适用于所有数组
echo '<br>';
rsort($array);//对数组逆向排序,如果数组中含有字母或汉字,结果可能不符合预期,最适用于数字数组
echo $array[0];
@forecho
forecho / httpClient.php
Last active November 18, 2015 10:11
封装 curl 类
<?php
function httpClient($url, $data = FALSE, $header = FALSE, $timeout = 30)
{
if (!strstr($url, 'http://') && !strstr($url, 'https://')) {
$url = 'http://' . $url;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
@forecho
forecho / new_gist_file.php
Created December 7, 2015 08:45
传一个数组,返回一个最小值和最大值。应用场景是获取商品SKU的价格区间
// TFuncProduct::getInterval(array(
// 10 => array('title' => 'a', 'size' => 1),
// 20 => array('title' => 'b', 'size' => 2),
// 30 => array('title' => 'c', 'size' => 3),
// ),
// 'size'
// );
// 输出 array(1, 3)
@forecho
forecho / Pk.php
Last active December 25, 2015 08:38
Primary Id
<?php
static $OUR_EPOCH = 1444647842959;
static $DEFAULT_SHARD_ID = 1;
/**
* 生成 bigint64 位主键
* @return int
*/
public static function generatePk()
{