目录结构:
效果图:
1.sql脚本
-- phpMyAdmin SQL Dump -- version 4.4.15.5 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: 2016-07-12 23:05:40 -- 服务器版本: 5.7.12-log -- PHP Version: 7.0.7 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `thinkphp5` -- -- -------------------------------------------------------- -- -- 表的结构 `think_user` -- CREATE TABLE IF NOT EXISTS `think_user` ( `user_id` int(11) NOT NULL, `user_name` varchar(255) NOT NULL, `user_sex` int(11) DEFAULT NULL, `user_tel` varchar(255) DEFAULT NULL, `user_email` varchar(255) DEFAULT NULL, `user_address` varchar(255) DEFAULT NULL, `user_birth` varchar(255) DEFAULT NULL, `user_jointime` varchar(255) DEFAULT NULL, `user_passwd` varchar(255) DEFAULT NULL, `user_signature` varchar(255) DEFAULT NULL, `user_hobby` varchar(255) DEFAULT NULL, `status` int(11) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4; -- -- 转存表中的数据 `think_user` -- INSERT INTO `think_user` (`user_id`, `user_name`, `user_sex`, `user_tel`, `user_email`, `user_address`, `user_birth`, `user_jointime`, `user_passwd`, `user_signature`, `user_hobby`, `status`) VALUES (1, 'thinkphp', 1, '15700000000', 'emial@email.com', '山东省济南市****路', '1111111', '111111', 'qqq', NULL, NULL, 1), (2, 'pangPython', 1, '15700000000', 'email@emial.com', '山东省济南市', '201607096', NULL, '123456', 'nihao', '上速度速度', 1), (3, 'test', 0, '15722222222', 'pangPython@163.com', '北京中关村', '19931212', '12999922', '21232f297a57a5a743894a0e4a801fc3', '这个人特别懒,什么都没写', '抽烟喝酒烫头', 1), (4, '你是什么鬼', 0, '15888889999', 'admin@haha.com', '美国硅谷', '19801002', '213123', '63a9f0ea7bb98050796b649e85481845', '个性前景', '啪啪啪', 1); -- -- Indexes for dumped tables -- -- -- Indexes for table `think_user` -- ALTER TABLE `think_user` ADD PRIMARY KEY (`user_id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `think_user` -- ALTER TABLE `think_user` MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
2 .写 控制器
user\controller\Members.php
<?phpnamespace app\user\controller;use app\index\model\User;use think\View;class Members{ public function index(){ //查询出用户列表 $list = User::all(function($query){ $query->where('status',1)->limit(5)->order('user_id','asc'); }); $view = new View; //设置变量输出 $view->assign('list',$list); return $view->fetch('index'); } }
3 .写视图
user\view\members\index.html
<!DOCTYPE html> <html> <head> <title>成员</title> <style> dl{ text-align:center; border:2px solid #00CC99; margin-top:100px; margin-bottom:100px; margin-right:400px; margin-left:400px; } </style> </head> <body> <dl> <h1>成员</h1> {volist name="list" id="vo"} <dt><h3>{$vo.user_id} 姓名:{$vo.user_name}</h3></dt> {/volist} </dl> </body> </html>
大体思路:
前端提交查询请求给控制器,控制器中根据条件查询出数据,变量替换到视图,并渲染返回…
以上是ThinkPHP5开发(四)查询用户列表(图文)的详细内容。更多信息请关注PHP中文网其他相关文章!