您的位置:首页 > 移动开发 > Swift

[全国首发]Swift视频教程

2015-05-15 14:50 141 查看
保存信息到文件里
<?php
$time = date('Y-m',time());
if(!is_dir($time))
{
mkdir($time,0700);
}
$nick = '彩霞飞飞';
$str = '今天天气好好啊!';
$path = $time.'/'.$time.'.txt';
$t = date('Y-m-d h:i:s',time());
//$msg = "$nick ".$t.'
// '.$str.'
// ';
$msg .= "\r\n"; //保存记录到文本中 实现换行
$t = array();
$t['u_nick'] = $nick;
$t['time'] = $tim;
$t['cont'] = $str;
$msg = json_encode($t);
$msg .= '
';
file_put_contents($path,$msg,FILE_APPEND);//在文件末尾追家
?>

分页读取固定条数的信息

<?php
$time = date('Y-m-d',time());
$path = $time.'/'.$time.'.txt';
$perpage = 2;
$currentPage = 3;
$total = count(file($path));
$totalPage = 1;
$totalPage = ceil($total / 2);
$start = ($currentPage - 1)*$perpage;
$end = $currentPage * $perpage;
if($total < $perPage || $currentPage == $totalPage)
{
$end = $total;
}
if(file_exists($path))
{
$file = file($path);
$file = array_reverse($file); // 数组倒序
$cont = array();
for($i = $end-1;$i >= $start;$i--)
{
$cont[] = json_decode($file[$i]);
var_dump($cont);
}
$t = array();
$t['k'] = 'chatrecord';
$t['totalPage'] = $totalPage;
$t['currentPage'] = $currentPage;
$t['content'] = $cont;
$ms = json_encode($t);
var_dump($ms);exit;
}
?>
经过自己测试fgets()方法读取只能从第一行按顺序一行一行的读取,如果做分页,他就不行,无论怎样循环,都是从第一行开始往后读的,所以不能用
本文出自 “彩霞飞飞” 博客,请务必保留此出处http://caixia.blog.51cto.com/2266345/692123
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: