后台php查找字段,并且处理数据,之后封装到一个对象,交给模板来调用:
/**
*
* @param int oid 订单ID
*/
function invoice(){
//获取oid
$oid = $_REQUEST['oid'];
//判断oid是否为空
if(empty($oid)){
$this->error('Order is not exist!');
}
//根据oid获取order
$order = M('order') -> where(array('oid' => $oid)) -> find();
//封装时间
$order['state_modifytime'] = date('Y.m.d',strtotime($order['state_modifytime']));
//根据order中的uid获取user
$users = M('users') -> where(array('user_id' => $order['user_id'])) -> find();
//判断公司名和公司地址是否为空
$company = $users['company'];
$address = $users['company_address'];
if (empty($company) || empty($address)) {
$this -> redirect('/Home/Index/personalInformation');
}
//根据oid获取orderdata
$orderdataList = M('orderdata') -> where(array('order_id' => $oid)) -> select();
//获取订单商品的类型
if($order['is_renew'] == 0){
$type = 'Neworder';
}else{
$type = 'Renew';
}
//获取订单的订单号
$ordernum = $order['o_num'];
//循环遍历出groupsid对应的所有单条订单
foreach ($orderdataList as $key => $orderdata) {
//获取单价,判断是否打折,并作出处理
if ($orderdata['sale_type'] == 1) {
$unitprice = $orderdata['discount_price'];
} else {
$unitprice = $orderdata['market_price'];
}
$num = $orderdata['good_nums'];
//封装订单单条记录
$orderUnit[] = array(
'ordernum' => $ordernum,
'unitprice' => $unitprice,
'good_nums' => $num
);
}
//封装invoice,包括order对象,user对象,还有其他的一些自己的属性
$taxmoney = sprintf('%.2f', $order['sum_money']*C('TAX_RATE')/100);//税收
$totalmoney = sprintf('%.2f', $order['sum_money']+$taxmoney);//总价(包括税收)
$invoice = array(
'invoiceid' => $order['oid'],//invoice id
'order' => $order,
'users' => $users,
'time' => time(),//issue date
'taxRate' => C('TAX_RATE'),//tax税率
'taxmoney' => $taxmoney,//税收
'type' => $type,//类型
'totalmoney' => $totalmoney, //总价(包括税收)
'orderUnit' => $orderUnit //总价(包括税收)
);
//将invoice对象assign到页面
$this -> assign('invoice', $invoice);
$this->display();
}
1、使用find()来查找,找到的是最后一条记录,如果要遍历出需要的所有记录,要使用select()来遍历
2、遍历的结果要全部拿到,需要使用一个相当于“二维数组”的变量来接收:$res[] = array(),如果使用$res = array(),拿到的只是一个对象,不是“对象数组”
3、在后台将数据处理好了之后在封装起来,不要在模板上处理任何数据,模板只是用来取到数据并显示出来就行
4、避免硬编码,否则以后修改起来困难较大
模板调用字段并且显示到页面上:
<table border="0" cellspacing="0" cellpadding="0" style="padding-left: 100px; width: 930px; margin-left: 40px;">
<span style="white-space:pre"> </span><tr style="height: 24px; line-height: 24px; background-color: #eee;">
<span style="white-space:pre"> </span><th style="text-align: center;">Date of Payment</th>
<th style="text-align: center; ">Order number</th>
<th style="text-align: center;">Type</th>
<th style="text-align: center; ">Unit Price</th>
<th style="text-align: center; ">Amount</th>
</tr>
<volist name='invoice.orderUnit' id='ou'>
<span style="white-space:pre"> </span><tr>
<span style="white-space:pre"> </span><td style="text-align: center;"><{$invoice.order.state_modifytime}></td>
<span style="white-space:pre"> </span><td style="text-align: center; ">NO <{$ou.ordernum}></td>
<span style="white-space:pre"> </span><td style="text-align: center; "><{$invoice.type}></td>
<span style="white-space:pre"> </span><td style="text-align: center; ">$<{$ou.unitprice}></td>
<span style="white-space:pre"> </span><td style="text-align: center; "><{$invoice.order.goodsnum}></td>
<span style="white-space:pre"> </span></tr>
</volist>
</table>
<span style="white-space:pre"> </span><div id="total">
<span style="white-space:pre"> </span><div class="form">
<span style="white-space:pre"> </span><p>Subtotal</p>
<p>tax(<{$invoice.taxRate}>)</p>
<p class="amount">Amount DUe</p>
</div>
<div class="con">
<p>$<{$invoice.order.sum_money}>USD</p>
<p>$<{$invoice['taxmoney']}>USD</p>
<p class="amount">$<{$invoice['totalmoney']}>USD</p>
</div>
</div>