查看: 233|回复: 0

[PHP代码] thinkphp数据的查询和截取

发表于 2017-1-11 19:14:48
  1. public function NewsList(){
  2. $this->assign('title','news');
  3. $p = I('page',1);
  4. $listRows = 6;
  5. $News = M('news');
  6. $info = $News->field('id,title,subtitle,publish_date,img,detail,typename')->where(array('type'=>'news','status'=>'1'))->order('flag desc,sort_no desc')->page($p,$listRows)->select();
  7. if(!empty($info)){
  8. foreach($info as $k=>$v){
  9. $info[$k]['detail']=mb_substr($v['detail'],0,130,'utf-8');
  10. }
  11. }
  12. $this->assign('news',$info);
  13. $count = $News->where(array('type'=>'news','status'=>'1'))->count();
  14. $Page = new Page($count,$listRows);
  15. $show = $Page->show();
  16. $this->assign('page',$show);
  17. $this->display();
  18. }
复制代码

前端获取数据:

  1. <td style="width:85%;padding-left:15px;line-height:30px;">
  2. {$new.detail}......
  3. </td>
复制代码

thinkphp读取数据库数据:

  1. <?php
  2. //读取一行数据
  3. $user=M("user");
  4. $data=$user->where("status=1 AND name="thinkphp"")->find();
  5. dump($data);
  6. //读取数据集合
  7. $User=M("user");
  8. $list = $user->where('status=1')->order("create_time")->limit(10)->select();
  9. //读取字段值
  10. $user=M("user");
  11. $nickname=$user->where("id=3")->getFild('nickname');
  12. //如果传入多个字段
  13. $user=M("user");
  14. $list=$user->getFild('id,nickname');
  15. //两个字段的情况下返回的是array(`id`=>`nickname`)的关联数组,以id的值为key,nickname字段值为value。
复制代码



回复

使用道具 举报