ThinkPHP中MVC代码范例

本文详细介绍了在ThinkPHP框架中使用MVC模式进行开发的实际操作,包括Controller控制器的编写,Model模型的数据处理,以及View视图的展示,旨在帮助开发者理解并掌握ThinkPHP的MVC架构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Controller


public function productlist(){
        $map = ['p.isdelete'=>0,'u.isdelete'=>0,'p.istrade'=>0];//没有删除
	//获取提交的数据
        $name = I('name');
        $status = I('post.status');
        $begindate = I("post.begindate");
        $enddate = I("post.enddate");
        $audit=I('post.audit');
        $istop=I('post.istop');
        $show_status=I('post.show_status');
	
	//拼接where条件
        if($name)  $map['p.name']  =  ['like','%'.$name.'%'];
        if($status>=0 && is_numeric($status))  $map['p.status'] = $status;
        if($begindate && $enddate) $map['p.addtime'] =  array(array('egt',$begindate),array('elt',date('Y-m-d', strtotime("$enddate+1days"))) );
        // if($begindate) $map['p.addtime'] =  array("egt",$begindate);
        // if($enddate) $map['p.addtime']   =  array('elt', date('Y-m-d', strtotime("$enddate+1days")));
        if($audit>=0 && is_numeric($audit))$map['p.audit']=$audit;
        if($istop>=0 && is_numeric($istop))$map['p.istop']=$istop;
        if($show_status == 1){
            $map['p.status'] = 1;
            $map['p.audit'] = 2;
            $map['p.show_start_time'] = array('elt',date('Y-m-d H:i:s',time()));
            $map['p.show_end_time'] = array('egt',date('Y-m-d H:i:s',time()));
        }


        //分页
        $pagenum =I('param.p');
        if(!$pagenum)$pagenum=1;
        $pagenum=!is_numeric($pagenum)?1:$pagenum;
        $pagesize= C('pagesize',null,10);
//        $pagesize=2;
        $count=0;
	
	
//        $list = D('product')->getproductlist($map,'p.*,u.user_name');
        
	//进入Model调用方法
	$list = D('product')->getproductlist($pagenum,$pagesize,$count,$begindate,$enddate,$map,'p.*,u.user_name');
        //echo M()->_sql();die;
        if($list){
            foreach($list as $k=>$v){
                $bt = strtotime($v['show_start_time']);
                $et = strtotime($v['show_end_time']);

                if($bt<time() && $et>time() &&  $v['audit'] ==2 && $v['isdelete'] == 0){
                    $list[$k]['show_status'] = 1;//前台展示
                }else{
                    $list[$k]['show_status'] = 0;//前台未展示
                }
            }

        }
        $this->assign('list',$list);

        $page = new \Think\Page($count,$pagesize);
        $show = $page->show();
        $this->assign('page',$show);
        $this->display();
    }



Model


public function getproductlist($pagenum,$pagesize,&$count,$begindate,$enddate,$map=[],$field="*")
	{
		$pre = C('DB_PREFIX');//获取表前缀

		// if ($begindate)	$map['p.addtime']= array("egt",$begindate);
		// if ($enddate)$map['p.addtime']=array('elt', date('Y-m-d', strtotime("$enddate+1days")));
		
		//拼接sql 结合多表查询
		$list=M('product')->alias('as p')->field($field)
				->join($pre.'user as u on u.id=p.sponsorid','left')
				->where($map)->page($pagenum.','.$pagesize)->order('p.addtime desc')->select();
		
		//分页数据总数量
		$count=M('product')->alias('as p')
				->join($pre.'user as u on u.id=p.sponsorid','left')
				->where($map)->count();

		return $list;
	}


View

<div class="container-fluid">
	<ol class="breadcrumb default square rsaquo sm">
		<li><a href="{:U('/Manage/Index/home')}" title="首页"><i class="fa fa-home"></i></a></li>
		<li class="text-primary">团购管理</li>
		<li class="active">订单管理</li>
	</ol>
	<div class="the-box less-margin searchbox">
		<form  method="post" action="{:U('Product/porderlist')}">
			<label>关键字</label>
			<select class="form-control" id="keytype" name="keytype">
				<option value="1" <if condition="I('post.keytype') eq '1'">selected="selected"</if>>平台流水号</option>
				<option value="2" <if condition="I('post.keytype') eq '2'">selected="selected"</if>>项目编号</option>
				<option value="3" <if condition="I('post.keytype') eq '3'">selected="selected"</if>>手机</option>
				<option value="4" <if condition="I('post.keytype') eq '4'">selected="selected"</if>>产品名称</option>
			</select>			
			<input type="text" class="form-control input-text" id="keywords" name="keywords" value="{:I('post.keywords')}">
			<select name="status" id="status" class="form-control">
				<option value="-1" <if condition="I('post.status') eq ''">selected=selected</if>>支付状态</option>
				<option value="0" <if condition="I('post.status') eq 0 and I('post.status') neq ''">selected=selected</if>>未支付</option>
				<option value="1" <if condition="I('post.status') eq 1">selected=selected</if>>已支付</option>
				<option value="2" <if condition="I('post.status') eq 2">selected=selected</if>>已关闭</option>
			</select>
			<select name="isprize" id="isprize" class="form-control">
				<option value="-1" <if condition="I('post.isprize') eq ''">selected=selected</if>>档位类型</option>
				<option value="0" <if condition="I('post.isprize') eq 0 and I('post.isprize') neq ''">selected=selected</if>>普通档位</option>
				<option value="1" <if condition="I('post.isprize') eq 1">selected=selected</if>>抽奖档位</option>
			</select>

			<select name="iswinning" id="iswinning" class="form-control">
				<option value="-1" <if condition="I('post.iswinning') eq ''">selected=selected</if>>中奖状态</option>
				<option value="0" <if condition="I('post.iswinning') eq 0 and I('post.iswinning') neq ''">selected=selected</if>>未中奖</option>
				<option value="1" <if condition="I('post.iswinning') eq 1">selected=selected</if>>已中奖</option>
			</select>
			
			<input type="text" class="form-control datepicker input-text" id="enddate" name="enddate" value="{:I('post.enddate')}">
	<input type="submit" class="btn btn-info margin-left" value="搜索">

		</form>
	</div>
	<div class="the-box">
		<div class="table-responsive">
			<table class="table table-condensed table-hover" id="datatable-display1">
				<thead class="bg-primary">
					<tr>						
						<th>编号</th>
						<th>订单编码</th>
						<th>产品名称</th>
						<th>档位类型</th>
						<th>手机</th>
					    <th>实付金额</th>
					    <th>消费积分</th>
						<th>支付时间</th>
						<th>支付状态</th>
						<th>是否邮寄</th>
						<!-- <th>收货地址</th>
						<th>收货人姓名</th>
						<th>联系电话</th> -->
						<!-- <th>添加时间</th> -->
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<volist name="list" id="vo" key="k">
						<tr>							
							<td>{$k}</td>
							<td>{$vo.code}</td>
							<td>{$vo.name}</td>
							<td><if condition="$vo['isprize'] eq '1'">抽奖档位<else/>普通档位</if></td>
							<!-- <td>{$vo.user_name}</td> -->
							<td>{$vo.phone}</td>
							<td>{$vo.actual}</td>
							<td>{$vo.integral}</td>
							<td>{$vo.paytime}</td>
						
							<td>
								<if condition="$vo['status'] eq 0">
									<span class="badge">未付</span>
								
								<elseif condition="$vo['status'] eq 1"/>
									<span class="badge badge-success">已付</span>
								<else/>
									<span class="badge ">已关闭</span>
								</if>
							</td>
					    	<td>
								<if condition="$vo['ispost'] eq 1">
									<span class="badge badge-success">需要</span>
								<else/>
									<span class="badge ">不需要</span>
								</if>
							</td>
							<!-- 	<td>{$vo.areaname}{$vo.address}</td>
								<td>{$vo.nickname}</td>
								<td>{$vo.phone}</td> -->
							<td class="navbar-right">
						
							<a href="{:U('Product/porderinfo',array('id'=>$vo['id']))}" >查看</a>
								<if condition="stripos($rightslist,'productorg') nheq false">
								<if condition="($vo['ispost'] eq 1) and ($vo['post_status'] eq 0) and ($vo['status'] eq 1)">
									<a data-toggle="modal" data-target="#myModal" data-id="{$vo.id}" class="dysh" id="stick"  style="cursor:pointer" >发货</a>
								<elseif condition="($vo['ispost'] eq 1) and ($vo['status'] eq 1)"/>
									<a data-toggle="modal" data-target="#myModal" data-id="{$vo.id}" class="dysh" id="stick"  style="cursor:pointer" >修改单号</a>
								</if>
								</if>
							</td>
						</tr>
					</volist>
				</tbody>
				<tfoot>
					<tr>
					<if condition="$list">
					<td colspan="9">
					</td>
					<td colspan="9">
					<form  method="post" action="{:U('Export/ExportPorderlist')}">
					<input type="hidden" name="keytype" value="{:I('post.keytype')}"/>
					<input type="hidden" name="keywords" value="{:I('post.keywords')}"/>
					<input type="hidden" name="status" value="{:I('post.status')}"/>
					<input type="hidden" name="isprize" value="{:I('post.isprize')}"/>
					<input type="hidden" name="iswinning" value="{:I('post.iswinning')}"/>
					<if condition="stripos($rightslist,'productorg') nheq false">
					<input type="submit" class="btn btn-info margin-left" value="导出EXCEL">
					</if>
					</form>
						</td>	
				 </if>
					</tr>
	
						
					</tr>
				</tfoot>
			</table>
			{$page}
		</div>
	</div>
</div>
<!-- 模态框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">确认发货</h4>
            </div>
            <div class="modal-body">
				<form method="POST" action="{:U('Product/sendorder')}" name="form_login" >
					<div >
						<input type="hidden" value="" name="id" id="expressid">
						物流公司
						<br/>
						<select name="expressid" id="exid" >
						     <option value="0">选择物流公司</option>
				             <volist name="express" id="v">

				                 <option value="{$v.id}">{$v.name}</optio>
				             </volist>
			            </select>
			            <br/>
			             <br/>
			            收货地址   <span id="readr"></span>
			            <br/>
			            <br/>
			            收 货 人   <span id="rename"></span>
			            <br/>
			            <br/>
			            联系电话   <span id="rephone"></span>
			            <br/>
			            <br/>
			      
						物流单号
						<input type='text' id ="test" name="express_no" class="form-control"/>
						
					</div>
				</form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal" class="close">关闭</button>
                <button type="button" id="sub" class="btn btn-primary affirm">确认</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值