当前位置:首页 > CMS教程 > Thinkphp > 列表

thinkphp 3.2框架视图模型 实例视图查询结果的二维数组合并操作示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-19 09:24:03 浏览: 评论:0 

本文实例讲述了thinkphp 3.2框架视图模型 实例视图查询结果的二维数组合并操作,分享给大家供大家参考,具体如下:

使用视图模型查询的时候 结果是这样的

  1. array(6) { 
  2.  [0] => array(5) { 
  3.   ["picTitle"] => string(7) "标题2" 
  4.   ["picCategroy"] => string(6) "海报" 
  5.   ["picAuthor"] => string(12) "星耀学园" 
  6.   ["picPostTime"] => string(19) "2014-11-26 11:59:50" 
  7.   ["pictureurl"] => string(55) "attachment/picture/uploadify/20141126/547550278b7db.jpg" 
  8.  } 
  9.  [1] => array(5) { 
  10.   ["picTitle"] => string(7) "标题2" 
  11.   ["picCategroy"] => string(6) "海报" 
  12.   ["picAuthor"] => string(12) "星耀学园" 
  13.   ["picPostTime"] => string(19) "2014-11-26 11:59:50" 
  14.   ["pictureurl"] => string(55) "attachment/picture/uploadify/20141126/54755027ab89b.jpg" 
  15.  } 
  16.  [2] => array(5) { 
  17.   ["picTitle"] => string(7) "标题2" 
  18.   ["picCategroy"] => string(6) "海报" 
  19.   ["picAuthor"] => string(12) "星耀学园" 
  20.   ["picPostTime"] => string(19) "2014-11-26 11:59:50" 
  21.   ["pictureurl"] => string(55) "attachment/picture/uploadify/20141126/547550273b753.jpg" 
  22.  } 
  23.  [3] => array(5) { 
  24.   ["picTitle"] => string(7) "标题2" 
  25.   ["picCategroy"] => string(6) "海报" 
  26.   ["picAuthor"] => string(12) "星耀学园" 
  27.   ["picPostTime"] => string(19) "2014-11-26 11:59:50" 
  28.   ["pictureurl"] => string(55) "attachment/picture/uploadify/20141126/54755027d8488.jpg" 
  29.  } 
  30.  [4] => array(5) { 
  31.   ["picTitle"] => string(33) "同步写入信息和附件表里" 
  32.   ["picCategroy"] => string(6) "海报" 
  33.   ["picAuthor"] => string(12) "星耀学园" 
  34.   ["picPostTime"] => string(19) "2014-11-20 16:05:16" 
  35.   ["pictureurl"] => string(55) "attachment/picture/uploadify/20141120/546da0746edb8.png" 
  36.  } 
  37.  [5] => array(5) { 
  38.   ["picTitle"] => string(33) "同步写入信息和附件表里" 
  39.   ["picCategroy"] => string(6) "海报" 
  40.   ["picAuthor"] => string(12) "星耀学园" 
  41.   ["picPostTime"] => string(19) "2014-11-20 16:05:16" 
  42.   ["pictureurl"] => string(55) "attachment/picture/uploadify/20141120/546da0784831c.png" 
  43.  } 

想要的结果是这样

  1. array(2) { 
  2.  ["0"] => array(5) { 
  3.   ["picTitle"] => string(7) "标题2" 
  4.   ["picCategroy"] => string(6) "海报" 
  5.   ["picAuthor"] => string(12) "星耀学园" 
  6.   ["picPostTime"] => string(19) "2014-11-26 11:59:50" 
  7.   ["pictureurl"] => string(223) "attachment/picture/uploadify/20141126/547550278b7db.jpg,attachment/picture/uploadify/20141126/54755027ab89b.jpg,attachment/picture/uploadify/20141126/547550273b753.jpg,attachment/picture/uploadify/20141126/54755027d8488.jpg" 
  8.  } 
  9.  ["1"] => array(5) { 
  10.   ["picTitle"] => string(33) "同步写入信息和附件表里" 
  11.   ["picCategroy"] => string(6) "海报" 
  12.   ["picAuthor"] => string(12) "星耀学园" 
  13.   ["picPostTime"] => string(19) "2014-11-20 16:05:16" 
  14.   ["pictureurl"] => string(111) "attachment/picture/uploadify/20141120/546da0746edb8.png,attachment/picture/uploadify/20141120/546da0784831c.png" 
  15.  } 

完整实例开始

控制器 里 根据生成的SQL 语句在phpmyadmin里运行 出现排序规则不一致的情况 两个表的字段 排序规则不一致 整表排序规则一样的情况下

PictureController.class.php

  1. public function Pic(){ 
  2.    $PicModel =D('PicView'); 
  3.  
  4.   /* dump($PicModel);*/ 
  5.  
  6.   $s1$PicModel->field('picTitle,picCategroy,picAuthor,picPostTime,pictureurl')->order('picPostTime desc')->select(); 
  7.  /* dump($PicModel->getLastSql()); 查询最后的sql 语句*/ 
  8.  /* dump($s1);*/ 
  9.   $zhengli = $this->mergePictureURL($s1); 
  10.  /* dump( $zhengli );*/ 
  11.  
  12.   $this->assign("content",$zhengli); 
  13.  
  14.   $this->display(); 
  15.  

视图模型里

PicViewModel.class.php

  1. <?php 
  2. namespace Home\Model; 
  3. use Think\Model\ViewModel; 
  4. class PicViewModel extends ViewModel{ 
  5.   public $viewFields =array
  6.     'Picture'=> array('picTitle','picToken','picCategroy','picAuthor','picPostTime','_as'=>'Picture'), 
  7.     'PictureAttachment' =>array('pictureid','pictureurl','creattime','_on'=>'Picture.picToken = PictureAttachment.pictureid','_as'=>'PictureAttachment'), 
  8.  
  9.   ); 

视图里

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head lang="en"
  4.   <meta charset="UTF-8"
  5.   <title></title> 
  6. </head> 
  7. <body> 
  8. <volist name="content" id="data"
  9.  <!-- {$data.picTitle} {$data.picAuthor}发布时间:{$data.picPostTime}--> 
  10.   {$data.picTitle} 
  11. </volist> 
  12. </body> 
  13. </html>

Tags: thinkphp3.2二维数组合并

分享到: